jsp中的页面导航

时间:2014-04-01 01:53:02

标签: java jsp

我是jsp的新手,想要使用servlet从一个页面导航到下一个页面

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

这是我得到的错误:

request cannot be resolved to a variable
response cannot be resolved to a variable

1 个答案:

答案 0 :(得分:0)

您不能使用init()进行请求分派,请执行以下操作。

  @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
    }

GenericServlet的方法 init 将在类HttpServlet的doGet方法之前被调用

GenericServlet不支持请求分派(简单来说就是页面导航)

Designing a generic servlet which handles request dispatching

回复那个

  

如今网络程序员不会自己编写这种代码。这些使用Web框架,如Struts或JSF或Spring(如果您是Java程序员),它们提供调度请求的优雅实现,从请求到实现请求的类的映射。还有更多。