有关doFilter的一个问题
本帖最后由 shishi123456 于 2013-03-22 20:57:29 编辑
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws
IOException,
ServletException {
HttpServletRequest req=(HttpServletRequest) request;
HttpServletResponse res=(HttpServletResponse) response;
//以下代码为什么会报错?
//System.out.println(" :: "+request.getServletContext().getServerInfo());
//使用以下代码就没有报错?request中也是有getServletContext()方法的,但报错
//提示没有这个方法,有点想不明白
System.out.println(" :: "+this.filterconfig.getServletContext()
.getServerInfo());
chain.doFilter(req, res);
}
在服务器后台报错信息如下:
严重: Servlet.service() for servlet jsp threw exception
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Ljavax/servlet/ServletContext;
at cn.itcast.filter.FirstFilter.doFilter(FirstFilter.java:29)
在网页中报错信息如下:
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Filter execution threw an exception
root cause
java.lang.
NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Ljavax/servlet/ServletContext;
cn.itcast.filter.FirstFilter.doFilter(FirstFilter.java:29)
filter
servlet
------解决方案--------------------getServletContext()方法在 servlet 3.0规范里的.你的tomcat是几?下载最新的tomcat就行了。