1 Reply Latest reply on Feb 12, 2013 1:56 AM by sivaprasad9394

    page does not display correct when using filter

    mmunir

      I am using following filter to control access to all pages in JSF 2.0 using GlassFish as application server. The problem is that with this code although filter works fine and user are redirected to log.xhtml if they try to acess anyother page directly but the login.xhtml does not look good as it should be which you can see below. However if i remove the sendRedirect statement and replace it with chain.doFilter statement, then the page displays in the same way as it should be looking nice and good however filtering does not work obviously. How can I fix this problem?

       

       

      LoggingFilter.java

       

          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {   

              HttpServletRequest req = (HttpServletRequest) request;

              LoginBean auth = (LoginBean) req.getSession().getAttribute("loginBean");

       

       

       

       

              if ((auth != null && auth.isLoggedIn()) || req.getRequestURI().endsWith("/login.xhtml")) {

                  // User is logged in, so just continue request.

                  HttpServletResponse httpResponse = (HttpServletResponse)response;

                  httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.

                  httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.

                  httpResponse.setDateHeader("Expires", 0); // Proxies.

                  chain.doFilter(request, response);

              } else {

                  // User is not logged in, so redirect to index.

                  HttpServletResponse res = (HttpServletResponse) response;

                  res.sendRedirect(req.getContextPath() + "/faces/login.xhtml");

                  //FacesContext.getCurrentInstance().getExternalContext().dispatch("/login.xhtml");

                  //chain.doFilter(request, response);

              }

          }

       

      Good.jpg

       

      good.jpg

       

      Bad.jpg

       

      bad.jpg

        • 1. Re: page does not display correct when using filter
          sivaprasad9394

          HI Munir,

           

          As per my suggestion look on the css first.

          Here OR condition is not required


          if ((auth != null && auth.isLoggedIn()) || req.getRequestURI().endsWith("/login.xhtml")) {

           

           

          Check here redirect page path is correct or not

          res.sendRedirect(req.getContextPath() + "/faces/login.xhtml");

           

          try to comment these 3 lines for testing

           

                      httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.

                      httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.

                      httpResponse.setDateHeader("Expires", 0); // Proxies.

           

          There is noting wrong in the code.looks ok

           

          Thanks,

          Siva