You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse+Tomcat8.5环境下SqServlets.java中Cookie类型不匹配编译错误求助

Fixing the "Type Mismatch" Error with req.getCookies() in Your Servlet

What’s Causing the Problem?

Let’s cut to the chase: you’ve imported the wrong Cookie class! The line import org.apache.tomcat.util.http.parser.Cookie; is pulling in a Tomcat-internal parser class, but the HttpServletRequest.getCookies() method returns an array of javax.servlet.http.Cookie — these are entirely different types, hence the compiler throwing a type mismatch error.

The Fix (Super Simple)

Just swap out your import statement for the correct one:

  1. Delete this incorrect import:
    import org.apache.tomcat.util.http.parser.Cookie;
    
  2. Add the proper Servlet API Cookie import:
    import javax.servlet.http.Cookie;
    

Corrected Full Code

Here’s your updated servlet code with the fix applied:

package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie; // Correct import here

public class SqServlets extends HttpServlet {
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException {
        Cookie cookie[]= req.getCookies(); // No more type mismatch error!
    }
}

Quick Post-Fix Checks

  • Save the file and let Eclipse recompile it automatically. If you still see errors, try right-clicking your project and selecting Project > Clean to clear old compiled files.
  • Double-check that Tomcat 8.5’s Servlet API library is properly added to your project’s build path (Eclipse usually handles this automatically, but it’s worth verifying if issues persist).

内容的提问来源于stack exchange,提问作者nir doshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:56:50