Search code examples
javaservletsforward

How to forward from one java servlet to another in same session


So rather simple question, with quite few answers online, but I've tried them all and none of them seem to work for me.

        ServletContext servletContext = getServletContext();
        RequestDispatcher rd = servletContext.getRequestDispatcher("Home");
        rd.forward(request, response);

This is my current attempt , which produces java.lang.NullPointerException

I've also tried using RequestDispatcher rd = request.getRequestDispatcher("Home"); Which produces a blank page

response.sendRedirect("Home");

Works but doesn't preserve same session

I am redirecting from my Login servlet to Home servlet, both are stored in same package, both are in web.xml ( even if that is no longer needed)


Solution

  • I sorted my problem by storing username directly in session instead of beans, and used redirect instead of forward, for some reason beans werent properly retaining values after redirect.