I am having a filter. It should redirect to /b if /a is requested.
public class WebFilter extends GenericFilterBean
{
@Override
public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String path = req.getRequestURI ().toString ();
if (path.equals ("/a") == true)
{
System.out.println ("FILTER: a -> b");
res.reset ();
res.resetBuffer();
res.sendRedirect ("b");
}
chain.doFilter (req, res);
}
}
Here is the handler for the content.
@Component
@Controller
@RequestMapping ("/")
public class WebHandler
{
@RequestMapping ("/a")
public String a ()
{
System.out.println ("a");
return "a"; // point to a.jsp
}
@RequestMapping ("/b")
public String b (HttpSession ses)
{
System.out.println ("b");
return "b"; // point to b.jsp
}
}
If I request /a in a browser, then that is the output.
FILTER: a -> b
IN a
IN b
Why is method a called? I would expect only b because I redirect in doFilter from a to b. How can I make that happen?
So you are actually doing a redirect although it seems you just want to direct the request to a different controller mapping.
res.sendRedirect ("b");
Just changes the response code to 302
and adds a location
field, you will still hit the first controller, which is why you still see a
in the logs.
The browser then acknowledges this redirect directive and will send a second request for /b
.
One way to achieve what you appear to want would be to create a new Request from the submitted one, and simply override the getRequestUri()
method.
@Override
public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String path = req.getRequestURI();
if (path.equals("/a")) {
req = new HttpServletRequestWrapper((HttpServletRequest) request) {
@Override
public String getRequestURI() {
return "/b";
}
};
}
chain.doFilter (req, res);
}
This is then passed to the filter chain and everything continues as though that were the original request
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With