How to redirect to current page in Servlet Filter?

I have a page: /myapp/test.jsp?queryString=Y . The filter should redirect to the current page. It should go to /myapp/test.jsp (without the query string). The following shows that this leads to the context root: / myapp . I am running in WAS6.1.

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) req;
        HttpServletResponse httpResp = (HttpServletResponse) resp;
{
   boolean blnNeedToRedirect = true;
   if (blnNeedToRedirect) {
      httpResp.sendRedirect(".");
      return;
   }

   chain.doFilter(req, resp);
}
+3
source share
2 answers

Use HttpServletrequest.getRequestURI . This should work for you:

httpResp.sendRedirect(httpReq.getRequestURI());
+8
source

httpReq.getRequestURI () redirects to the requested page. To redirect to the same page, run the following command:

((HttpServletResponse)response).sendRedirect(".");
0

Source: https://habr.com/ru/post/1719505/


All Articles