Eclipse+Tomcat8.5环境下SqServlets.java中Cookie类型不匹配编译错误求助
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:
- Delete this incorrect import:
import org.apache.tomcat.util.http.parser.Cookie; - Add the proper Servlet API
Cookieimport: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 > Cleanto 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
相关产品推荐
相关产品推荐

