简体   繁体   中英

Java Servlet dispatcher to another Java file

Java

public class Forward extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        req.setAttribute("option", "forward");

        RequestDispatcher dispatcher = req.getRequestDispatcher("/GotoHere");
        dispatcher.forward(req, res);

    }
}

XML

  <servlet>
        <servlet-name>GotoHere</servlet-name>
        <servlet-class>examples.GotoHere</servlet-class>
  </servlet>
  <servlet-mapping>
        <servlet-name>GotoHere</servlet-name>
        <url-pattern>/GotoHere</url-pattern>
  </servlet-mapping>

And I received this error message on the web page: HTTP Status 500 - Error instantiating servlet class examples.GotoHere

I looked this question, Java servlet not dispatching to another servlet , which is really similar to mine, so I changed

RequestDispatcher dispatcher = req.getRequestDispatcher("/GotoHere"); 

to

ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getNamedDispatcher("GotoHere");

but it's still not working. any ideas?

try this:

 ServletContext context = getServletContext();  
 RequestDispatcher dispatcher = context.getRequestDispatcher("/GotoHere");
 dispatcher.forward(request, response);

Try this:

    req.setAttribute("option", "forward");
    res.sendRedirect("GoToHere");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM