0

I'm trying some URL on browser, it works well on all browsers including IE 10 but when on Microsoft Edge, it fails at a point while doing HttpServletResponse sendRedirect, which expires HttpServletRequest session and the expected page does not appear.
Please help for how we can resolve this browser specific redirect-session issue.

Basic code:

public class MyServlet extends HttpServlet {

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // some code where values are set in session 
        session.setAttribute(myAttribute, value);
        response.sendRedirect("https://qa.sys.com/MainPage.jsf");
 }

 public class MyFilter implements Filter {
     @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;

        // myAttribute below comes null as request.getSession() is null 
        Boolean myAttribute = request.getSession().getAttribute(myAttribute);

 }

In logs, I got this Exception:-

java.lang.IllegalStateException: Response already committed at weblogic.servlet.internal.ServletResponseImpl.objectIfCommitted(ServletResponseImpl.java:1861) at weblogic.servlet.internal.ServletResponseImpl.sendRedirect(ServletResponseImpl.java:961) at weblogic.servlet.internal.ServletResponseImpl.sendRedirect(ServletResponseImpl.java:956) at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78) Truncated. see log file for complete stacktrace

surm
  • 167
  • 2
  • 11

1 Answers1

1

You could also use F12 dev tools to check the Network tab in Edge and see if there's any error in console. This error usually occurs when a java servlet is attempting to write to the output stream (response) after the response has been committed. You could refer to this thread to find out why the response will get committed.

In this blog, it gives the solution and you could also refer to the sample code in the post:

It is always better to ensure that no content is added to the response after the forward or redirect is done to avoid IllegalStateException. It can be done by including a ‘return’ statement immediately next to the forward or redirect statement.

I also find an answer with detailed information about this issue and you could check it.

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
  • Thanks Yu Zhou, is it someway linked to Edge browser? Such as return statement after sendredirect if not given then it causes problem on Edge! – surm Aug 01 '19 at 12:36
  • It might be someway linked to Edge. I'll try to do some research and if there's something found I'll send a feedback. For now, you could try the solution in the answer. – Yu Zhou Aug 02 '19 at 07:22