Web tier: servlets, JSP, Web frameworks: HttpServlet+send redirect

  1. HttpServlet+send redirect (2 messages)

    Hi, As per my requirement in servet, i am redirecting(sendRedirect) the response to another url. I my Servlet Filter , can i able to get the url to which i redirecting to? Thanks, Mahendran..
  2. Yes you can. You need to access the "Location" header of the response ( See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30 ) But it is not that straightforward in servlets since there are no response.getHeader methods available!! ( I dont know why it is so. ) Thankfully, there is a solution using ResponseWrapper. Write your own response wrapper and override the setHeader method. In the over-ridden setHeader catch the location value and store it in a wrapper instance variable for access from filter. Code would be something like this: public class LocationFinderWrapper extends HttpServletResponseWrapper { private String location; //write getter and setter public void setHeader(String name,String value){ if( "location".equalsIgnoreCase(name) ) setLocation(value); super.setHeader(name,value);//dont forget this! } } Instantiate this wrapper around your original response object and then when you call chain.doFilter pass the wrapper object. This needs be done in the doFilter method of your filter.
  3. Hi, Thanks, Very good workaround.