0 Replies Latest reply: Oct 10, 2011 1:34 PM by 893594 RSS

    Tomcat:No redirecting to 401 Error Page

    893594
      Hi.I am taking my first steps with Tomcat and JSP.I have writen a Filter class which should redirect every request to a subfolder "user" to a error page,if in the session are not the variables user and password.But it does not work.Here is the fDeployment Desciptor:

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name>S2DashboardDesktopBackEnd</display-name>
      <filter>
      <filter-name>UserFilter</filter-name>
      <filter-class>filters.UserFilter</filter-class>
      <init-param>
      <param-name>log_category</param-name>
      <param-value>DurationLog</param-value>
      </init-param>
      </filter>
      <filter-mapping>
      <filter-name>UserFilter</filter-name>
      <url-pattern>/user/*</url-pattern>
      </filter-mapping>
      <error-page>
      <error-code>401</error-code>
      <location>/errorPages/401.jsp</location>
      </error-page>
      </web-app>



      And here is the Code of the Filter:

      package filters;


      import java.io.IOException;
      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 javax.servlet.http.HttpServletResponse;
      import javax.servlet.http.HttpSession;



      public class UserFilter implements Filter {


      public void init(FilterConfig config) throws ServletException {


      }
      public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
      HttpServletRequest httpRequest = (HttpServletRequest) request;
      HttpServletResponse httpResponse = ((HttpServletResponse) response);
      HttpSession currentSession = httpRequest.getSession();
      if(currentSession.getAttribute("username") == null || currentSession.getAttribute("password") == null){
      httpResponse.setStatus(401);
      }
      else{
      chain.doFilter(httpRequest, httpResponse);
      }

      }



      public void destroy() {
      }
      }