I have a servlet filter which takes the jsessionid and attaches it to the response. However, it's not working correctly because the application gets stuck in an endless loop. The code looks correct, but how Tomcat deals with request and response is where the confusion lies.
Below is the doFilter method (destroy and init are blank):
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
HttpServletRequest rQ = ((HttpServletRequest)request);
HttpServletResponse rS = ((HttpServletResponse)response);
if ((!request.isSecure())) {
String uRL = rS.getRequestURL().toString();
String qS = (rS.getQueryString() == null) ? "" : rS.getQueryString();
String sID = rS.getSession().getId();
String redirectURL = uRL + ";jsessionid=" + sID + qS;
rS.sendRedirect(redirectURL);
}
else {
chain.doFilter(request, response);
}
}
}
I have applied the filter to all JSP pages. What I see happening is an http JSP page gets called -- let's call it Content.jsp -- and the servlet takes this page and appends the jsessionid. Then, Content.jsp is called again because of the rule I have applied to call all JSP pages and it redirects continuously.
Either I have to change my code or, somehow, intercept it before the servlet accesses the page. How can I resolve this? Perhaps, I can check if the jsessionid was appended to the URL but how could I do that???
Thank you.