0
votes

I'm new to jsp and want to navigation from one page to the next one using the servlet

public void init(ServletConfig config) throws ServletException {
    String destination = "/WEB-INF/pages/result.jsp";
    RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
    rd.forward(request, response);
}

This is the error that I get:

request cannot be resolved to a variable
response cannot be resolved to a variable
1
What do you think the init method does? What do you think forward does? What are request and response? - Sotirios Delimanolis
read up on the lifecycle of a servlet. Hint doGet - Scary Wombat
you need to use the resp.. and req.. variable that are passed as parameters to ur doGet or doPost /// do try resp.sendRedirect("abc.jsp"); - Srinath Ganesh
thanks @SrinathGanesh - user3479237

1 Answers

0
votes

You cannot use init() for request dispatch , do as follows .

  @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, world");

        resp.sendRedirect("abc.jsp");
// you can replace it with RequestDispatcher also
    }

method init of GenericServlet will get called before the doGet method of class HttpServlet

GenericServlet does not support request dispatch(page navigation in simple terms)

Designing a generic servlet which handles request dispatching

And the reply to that

Nowadays web programmers don't write this kind of code themselves. These use web frameworks like Struts or JSF or Spring (if you're a Java programmer) which provide elegant implementations of dispatching requests, of mapping from requests to classes that implement the requests. And much, much more.