爪哇
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");
但它仍然无效。任何想法?
答案 0 :(得分:0)
试试这个:
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/GotoHere");
dispatcher.forward(request, response);
答案 1 :(得分:0)
试试这个:
req.setAttribute("option", "forward");
res.sendRedirect("GoToHere");