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>secureconnect ionfilter< /filter-na me>
<filter-class>cart.securit y.filter.S ecureConne ctionFilte r</filter- class>
</filter>
<filter-mapping>
<filter-name>secureconnect ionfilter< /filter-na me>
<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.getRequestDispatch er(“/index ”);
rd.forward(request, response);
so I want to use sendRedirect, like:
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect( httpRespon se.encodeR edirectURL ("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
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>secureconnect
<filter-class>cart.securit
</filter>
<filter-mapping>
<filter-name>secureconnect
<servlet-name>securitytest
</filter-mapping>
I cannot use the request dispatcher because it forwards to a relative address in the same servlet context only:
RequestDispatcher rd = context.getRequestDispatch
rd.forward(request, response);
so I want to use sendRedirect, like:
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect(
But it is not working, it ignores it without throwing any exception!
Any idea? Thank you in advance..
- Rabih
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.getServletCon text();
if (httpRequest.getScheme().e qualsIgnor eCase("htt p")) {
String origURL = httpRequest.getRequestURL( ).toString ();
StringBuffer newbURL = new StringBuffer(origURL);
newbURL.insert(origURL.ind exOf(":"), 's');
String newURL = newbURL.toString();
if (httpRequest.getQueryStrin g() != null) {
newURL = newURL + "?" + httpRequest.getQueryString ();
}
//this cannot be written
// RequestDispatcher rd = context.getRequestDispatch er("http://localhost:8080/cart/index.html");
// rd.forward(request, response);
//and this didnt work
httpResponse.sendRedirect( httpRespon se.encodeR edirectURL (
"http://localhost:8080/cart/index.html"));
} else {
try {
session.setAttribute("SECU RITY_SIGN" , "secured");
filterChain.doFilter(reque st, response);
}
catch (ServletException sx) {
filterConfig.getServletCon text().log (sx.getMes sage());
}
catch (IOException iox) {
filterConfig.getServletCon text().log (iox.getMe ssage());
}
}
}
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.getServletCon
if (httpRequest.getScheme().e
String origURL = httpRequest.getRequestURL(
StringBuffer newbURL = new StringBuffer(origURL);
newbURL.insert(origURL.ind
String newURL = newbURL.toString();
if (httpRequest.getQueryStrin
newURL = newURL + "?" + httpRequest.getQueryString
}
//this cannot be written
// RequestDispatcher rd = context.getRequestDispatch
// rd.forward(request, response);
//and this didnt work
httpResponse.sendRedirect(
"http://localhost:8080/cart/index.html"));
} else {
try {
session.setAttribute("SECU
filterChain.doFilter(reque
}
catch (ServletException sx) {
filterConfig.getServletCon
}
catch (IOException iox) {
filterConfig.getServletCon
}
}
}
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.getServletCon text();
if (httpRequest.getScheme().e qualsIgnor eCase("htt p")) {
String origURL = httpRequest.getRequestURL( ).toString ();
StringBuffer newbURL = new StringBuffer(origURL);
newbURL.insert(origURL.ind exOf(":"), 's');
String newURL = newbURL.toString();
if (httpRequest.getQueryStrin g() != null) {
newURL = newURL + "?" + httpRequest.getQueryString ();
}
//this cannot be written
// RequestDispatcher rd = context.getRequestDispatch er("http://localhost:8080/cart/index.html");
// rd.forward(request, response);
//and this didnt work
response.sendRedirect(http Response.e ncodeRedir ectURL(
"http://localhost:8080/cart/index.html"));
} else {
try {
session.setAttribute("SECU RITY_SIGN" , "secured");
filterChain.doFilter(reque st, response);
}
catch (ServletException sx) {
filterConfig.getServletCon text().log (sx.getMes sage());
}
catch (IOException iox) {
filterConfig.getServletCon text().log (iox.getMe ssage());
}
}
}
use response instead of httpResponse.
CJ
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.getServletCon
if (httpRequest.getScheme().e
String origURL = httpRequest.getRequestURL(
StringBuffer newbURL = new StringBuffer(origURL);
newbURL.insert(origURL.ind
String newURL = newbURL.toString();
if (httpRequest.getQueryStrin
newURL = newURL + "?" + httpRequest.getQueryString
}
//this cannot be written
// RequestDispatcher rd = context.getRequestDispatch
// rd.forward(request, response);
//and this didnt work
response.sendRedirect(http
"http://localhost:8080/cart/index.html"));
} else {
try {
session.setAttribute("SECU
filterChain.doFilter(reque
}
catch (ServletException sx) {
filterConfig.getServletCon
}
catch (IOException iox) {
filterConfig.getServletCon
}
}
}
use response instead of httpResponse.
CJ
ASKER
"response" is of type javax.servlet.ServletRespo nse which has no sendRedirect method.
so your solution does not work.
Thanks anyway..
- Rabih
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
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
CJ
ASKER
yes, i have tried both, casting it to response and eliminating the encoding, and it didnt work!
-RAB
-RAB
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.HttpS ervletResp onse)respo nse).sendR edirect("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.
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.HttpS
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
and make sure you see "my fileter is called" is printed in server log.
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.
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
Glad I could help and Thanx for the "A"
CJ
if you need further explanation, please let us know and post your filter code here, thanks.