How to send redirect to JSP page in Servlet

jsp, redirect, servlets

Solution

Look at the `HttpServletResponse#sendRedirect(String location)` method.

Use it as:

response.sendRedirect(request.getContextPath() + "/welcome.jsp")

Alternatively, look at `HttpServletResponse#setHeader(String name, String value)` method.

The redirection is set by adding the location header:

response.setHeader("Location", request.getContextPath() + "/welcome.jsp");

Problem

When I'm done processing in a servlet, and the result is valid, then I need to redirect the response to another JSP page, say `welcome.jsp` in web content folder. How can I do it? For example: ``` protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // Some processing code here ... // How do I redirect to another JSP here when I'm ready? } catch (Exception e) { throw new ServletException(e); } } ```

Original source