Link to home
Start Free TrialLog in
Avatar of rabihy
rabihy

asked on

redirect from servlet Filter

Hello,

I have problem in trying to redirect my incoming requests to an absolute URL from a Servlet Filter.

I have this filer:
  <filter>
    <filter-name>secureconnectionfilter</filter-name>
    <filter-class>cart.security.filter.SecureConnectionFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>secureconnectionfilter</filter-name>
    <servlet-name>securitytest</servlet-name>
  </filter-mapping>

I cannot use the request dispatcher because it forwards to a relative address in the same servlet context only:
RequestDispatcher rd = context.getRequestDispatcher(“/index”);
rd.forward(request, response);



so I want to use sendRedirect, like:

HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect(httpResponse.encodeRedirectURL("http://localhost:8080/cart/index.html"));

But it is not working, it ignores it without throwing any exception!

Any idea? Thank you in advance..

- Rabih
Avatar of kennethxu
kennethxu

don't call chain.doFilter(...) after sendRedirectURL().

if you need further explanation, please let us know and post your filter code here, thanks.
Avatar of rabihy

ASKER

I have already tried to eliminate chain.doFilter(...) and it didnt work.


here is the code:


public class SecureConnectionFilter
    implements Filter {
  private FilterConfig filterConfig = null;

  //Handle the passed-in FilterConfig
  public void init(FilterConfig config) throws ServletException {
    this.filterConfig = config;
  }

  //Process the request/response pair
  public void doFilter(ServletRequest request, ServletResponse response,
                       FilterChain filterChain)
      throws IOException, ServletException {
             
   
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    HttpSession session = httpRequest.getSession();

    ServletContext context = filterConfig.getServletContext();
   
    if (httpRequest.getScheme().equalsIgnoreCase("http")) {
      String origURL = httpRequest.getRequestURL().toString();
      StringBuffer newbURL = new StringBuffer(origURL);
      newbURL.insert(origURL.indexOf(":"), 's');
      String newURL = newbURL.toString();
      if (httpRequest.getQueryString() != null) {
        newURL = newURL + "?" + httpRequest.getQueryString();
      }
     
      //this cannot be written
      // RequestDispatcher rd = context.getRequestDispatcher("http://localhost:8080/cart/index.html");
      // rd.forward(request, response);
     
     
     
      //and this didnt work
        httpResponse.sendRedirect(httpResponse.encodeRedirectURL(
            "http://localhost:8080/cart/index.html"));
     
     
     
     

    } else {
      try {

          session.setAttribute("SECURITY_SIGN", "secured");

          filterChain.doFilter(request, response);
      }
      catch (ServletException sx) {
        filterConfig.getServletContext().log(sx.getMessage());
      }
      catch (IOException iox) {
        filterConfig.getServletContext().log(iox.getMessage());
      }

    }

  }
try this:
public class SecureConnectionFilter
   implements Filter {
 private FilterConfig filterConfig = null;

 //Handle the passed-in FilterConfig
 public void init(FilterConfig config) throws ServletException {
   this.filterConfig = config;
 }

 //Process the request/response pair
 public void doFilter(ServletRequest request, ServletResponse response,
                      FilterChain filterChain)
     throws IOException, ServletException {
           
   
   HttpServletRequest httpRequest = (HttpServletRequest) request;
   HttpServletResponse httpResponse = (HttpServletResponse) response;
   HttpSession session = httpRequest.getSession();

   ServletContext context = filterConfig.getServletContext();
   
   if (httpRequest.getScheme().equalsIgnoreCase("http")) {
     String origURL = httpRequest.getRequestURL().toString();
     StringBuffer newbURL = new StringBuffer(origURL);
     newbURL.insert(origURL.indexOf(":"), 's');
     String newURL = newbURL.toString();
     if (httpRequest.getQueryString() != null) {
       newURL = newURL + "?" + httpRequest.getQueryString();
     }
     
     //this cannot be written
     // RequestDispatcher rd = context.getRequestDispatcher("http://localhost:8080/cart/index.html");
     // rd.forward(request, response);
     
     
     
     //and this didnt work
       response.sendRedirect(httpResponse.encodeRedirectURL(
           "http://localhost:8080/cart/index.html"));
     
     
     
     

   } else {
     try {

         session.setAttribute("SECURITY_SIGN", "secured");

         filterChain.doFilter(request, response);
     }
     catch (ServletException sx) {
       filterConfig.getServletContext().log(sx.getMessage());
     }
     catch (IOException iox) {
       filterConfig.getServletContext().log(iox.getMessage());
     }

   }

 }

use response instead of httpResponse.

CJ
Avatar of rabihy

ASKER

"response" is of type javax.servlet.ServletResponse which has no sendRedirect method.

so your solution does not work.

Thanks anyway..

- Rabih
you are right.  My code I cast it as the same var.

Have you tried not encoding the URL.
 response.sendRedirect("http://localhost:8080/cart/index.html");

CJ
if you have to encode URL params just encode those.

CJ
Avatar of rabihy

ASKER

yes, i have tried both, casting it to response and eliminating the encoding, and it didnt work!

-RAB
ASKER CERTIFIED SOLUTION
Avatar of cheekycj
cheekycj
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that's strange, you code looks fine to me. are you sure that your mapping is correct and your filter is get called?

anyway, can you try out this code, it works for me:

import javax.servlet.*;

public class MyFilter implements Filter {
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
      throws java.io.IOException, ServletException
 {
   System.out.println( "my fileter is called" );
   ((javax.servlet.http.HttpServletResponse)response).sendRedirect("http://localhost:8080/cart/index.html");
 }

 public void init(FilterConfig filterConfig) {}
 public void destroy() {}
}

and make sure you see "my fileter is called" is printed in server log.
Avatar of rabihy

ASKER

Thanks for your help. Actually "return" was the solution, even though i have tried it before, but i eliminated everything between the sendDirect and the return, so it worked fine. Actually the problem was a combination of also other things, because i had, and still, problem when there is many recursive sendRedirect (not in the same chain, but in the same request)... anyway, it is resolved now in some kind of workaround.. thanks... R.
Avatar of rabihy

ASKER

Thanks for all of you..
yeah the return; statement is needed to give control back.  the smallest stmt can cause problems y'know :-)

Glad I could help and Thanx for the "A"

CJ