加了 Filter 之后,出现这个错误,java.lang.LinkageError: Class javax/servlet/ServletRequest violates loader constraints,那位指点一
源文件为:
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class EncodingFilterServlet implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public static String WEB_PATH = " ";
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null){
request.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter( "encoding ");
String value = filterConfig.getInitParameter( "ignore ");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase( "true "))
this.ignore = true;
else if (value.equalsIgnoreCase( "yes "))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
web.xml 为
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app xmlns= "http://java.sun.com/xml/ns/j2ee " xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd " version= "2.4 ">
<display-name> fxwz </display-name>
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> EncodingFilterServlet </filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> GBK </param-value>
</init-param>
<init-param>
<param-name> ignore </param-name>
<param-value> true </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
</web-app>
错误提示为:
javax.servlet.ServletException: Filter execution threw an exception
root cause java.lang.LinkageError: Class javax/servlet/ServletRequest violates loader constraints
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:620)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1629)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:850)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1299)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1181)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
EncodingFilterServlet.doFilter(EncodingFilterServlet.java:31
这是什么问题呢?从网上没有找见答案,却又不知如何排查,那们高人指点一下
------解决方案--------------------
估计你用的是Tomcat5.0,检查一下WEB-INF\lib下面是不是有 servlet-api.jar,jsp-api.jar之类的jar,有的话,就把它们删除掉.
[解决办法]
看来你也是想进行字符编码的处理。
错误信息:EncodingFilterServlet.doFilter(EncodingFilterServlet.java:31
这个31行是什么?
是不是你的某个包没有加入到WEB系统的LIB中去?
还是包之间有冲突?有时候放在TOMCAT中的和WEB系统中的会有一致性的问题。
[解决办法]
第一个错误应该是冲突的问题
java.lang.ClassLoader已经load了servlet-api.jar,jsp-api.jar
而org.apache.catalina.loader.WebappClassLoader也会在你的web-inf/lib里面找到这两个类 在一次load
lz这个问题是tomcat启动时候遇到 还是执行时候遇到?
[解决办法]
那应该是tomcat问题(武断下)
有时候tomcat很不稳定(个人菜鸟意见)
[解决办法]
学习。
[解决办法]
看樓主給出的錯誤像是加载包时的问题,或者 是 Servlet 包的版本问题。