1

I stuck up with doFilter of HttpServletRequest.

Im trying to replace new URL for that request.

My code is as follows:

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
    throws IOException, ServletException {

     HttpServletRequest httpReq = (HttpServletRequest) req;
     HttpServletResponse httpRes = (HttpServletResponse) res;

     //If request resources ==> Continue
     if(httpReq.getContextPath().startsWith(httpReq.getContextPath()+"/resources")){
         chain.doFilter(req, res);
         return;
     }
     HttpSession session = httpReq.getSession();
     EmployeeDTO currentEmployee =(EmployeeDTO)session.getAttribute("currentEmployee");
    //If dont have session ==> Return login page
     if(currentEmployee == null){
        String requestURI = "";
         requestURI = httpReq.getRequestURI().replace(httpReq.getRequestURI(), httpReq.getContextPath()+ "/login");
         System.out.println(requestURI);
         //httpRes.reset();
         //httpRes.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
         //httpRes.setHeader("Location", requestURI);
         httpRes.sendRedirect(requestURI);
         chain.doFilter(req, res);
        return;
     }
     chain.doFilter(req, res);
     return;
}

But the code above is still not working. How can i do for this?

Thanks in advance!

tommybee
  • 2,409
  • 1
  • 20
  • 23
J. Henry
  • 53
  • 2
  • 7
  • Im trying to redirect to orther uri by two the way.First: User HttpResponse to try to re-write httpRes.reset(); //httpRes.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); //httpRes.setHeader("Location", requestURI); Second: I try to redirect but it still can not work. Who know How can i do? – J. Henry Aug 23 '17 at 02:11
  • Check https://stackoverflow.com/questions/2725102/how-to-use-a-servlet-filter-in-java-to-change-an-incoming-servlet-request-url or https://stackoverflow.com/questions/5308801/how-to-redirect-in-a-servlet-filter – Amit K Bist Aug 23 '17 at 02:28
  • @AmitKBist I want redirect to URL of page login. If try to use sendRedirect that mean I will direct to jsp page – J. Henry Aug 23 '17 at 03:17

1 Answers1

0

Why do you have to do a chain.doFilter() after setting a redirect response?

private FilterConfig filterConfig;

public void init(FilterConfig config) throws ServletException {
        this.filterConfig = config;
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
    throws IOException, ServletException {

     HttpServletRequest httpReq = (HttpServletRequest) req;
     HttpServletResponse httpRes = (HttpServletResponse) res;
         HttpSession session = httpReq.getSession();
         boolean requestResources = false;

     //If request resources ==> Continue
     if(httpReq.getContextPath().startsWith(httpReq.getContextPath()+"/resources")){
        requestResources =  true;
     }     
     if(session != null){
            EmployeeDTO currentEmployee =(EmployeeDTO)session.getAttribute("currentEmployee");
     }
     if(requestResources || currentEmployee != null){
            chain.doFilter();
     }else if(currentEmployee == null){         
         String loginURI = //hardcode the loginURI here. 
         httpRes.sendRedirect(loginURI);
         /*alternatively use the following to do an internal forward rather than a redirect      
         RequestDispatcher rd = filterConfig.getServletContext().getRequestDispatcher(loginURI); //here the loginURI path should not have the context in the url.
                rd.forward(servletReq, response);
         */
     }     

}

I have also refactored your code to remove the redundant and multiple 'return' statements

ramp
  • 1,256
  • 8
  • 14