日期:2014-05-17  浏览次数:20729 次

使用过滤器访问页面出现404错误

过滤器只过滤page文件夹下的jsp,一旦没有登陆而访问则跳转回index.jsp

 当我在浏览器输入http://localhost:8080/weibodemo/page/homepage.jsp的时候
页面出现的是
HTTP Status 404 - /weibodemo/page/index.jsp

Filter代码
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException{
HttpServletRequest req=(HttpServletRequest) request;
HttpSession ses=req.getSession();
if(ses.getAttribute("uesrid")!=null){
chain.doFilter(request, response);
}else{
request.getRequestDispatcher("index.jsp").forward(request,response); }
}


xml配置

  <filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>weibodemo.filter</filter-class>
   
  </filter>
  <filter-mapping>
  <filter-name>LoginFilter</filter-name>
  <url-pattern>/page/*</url-pattern>
  </filter-mapping>


------解决方案--------------------
request.getRequestDispatcher("../index.jsp").forward(request,response);
或者
request.getRequestDispatcher(request.getContextPath() + "index.jsp").forward(request,response);
还有我觉得这里用重定向是不是好点?