今天玩cookie时,导入javax.servlet.http.cookie包时出错:
<%@ page import="javax.servlet.http.cookie" %>
//IDE报错:The import javax.servlet.http.cookie cannot be resolved
//控制台报错:
?? 03, 2014 10:48:27 ?? org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/News] threw exception [Unable to compile class for JSP:
An error occurred at line: [15] in the generated java file: [/Applications/Tomcat/apache-tomcat-7.0.54/work/Catalina/localhost/News/org/apache/jsp/index_jsp.java]
Only a type can be imported. javax.servlet.http.cookie resolves to a package//注意这里@@!
Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [15] in the generated java file: [/Applications/Tomcat/apache-tomcat-7.0.54/work/Catalina/localhost/News/org/apache/jsp/index_jsp.java]
Only a type can be imported. javax.servlet.http.cookie resolves to a package
错误描述具体说的是,编译器把javax.servlet.http.cookie这个类名解析成了包名,傻鸟!
后经百度,查到以下解决办法:
从tomcat的lib文件夹中,复制servlet-api.jar到项目的WEB-INF的lib中。结果不仅无效,还引发了jar包的重复,系统不知道要加载tomcat的jar还是WEB-INF的jar,报了一对错。(哼哼,早就知道这样不行)
把<%@ page import="javax.servlet.http.cookie“ pageEncoding="utf-8"%>改成<%@ page import="javax.servlet.http.*" pageEncoding="utf-8"%>使用此方法后,终于解决了!
重启tomcat,出现该问题的原因是加入的jar未被识别
删除work目录下的该项目的文件,然后重启tomcat重新编译项目
classes目录下的包名和src目录下的包名不同,比如大小写(不知为何会不同...........)