Java Servlet调度程序到另一个Java文件

时间:2014-09-15 01:19:38

标签: java servlets

爪哇

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>

我在网页上收到了此错误消息:HTTP Status 500 - Error instantiating servlet class examples.GotoHere

我看了这个问题,Java servlet not dispatching to another servlet,这与我的相似,所以我改变了

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

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

但它仍然无效。任何想法?

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

试试这个:

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