"找不到符号"在servletContext()。getRequestDispatcher上

时间:2016-02-12 22:32:18

标签: java compiler-errors cannot-find-symbol

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    HttpSession sesion = request.getSession();
    PrintWriter out = response.getWriter();

    String num = request.getParameter("num");
    String password = request.getParameter("password");
    sesion.setAttribute("num", num);

    Queries sql = new Queries();
    if (sql.login(num, password)) {
        response.sendRedirect("private.jsp");
    } else {
        request.setAttribute("error", "Error on login");
        RequestDispatcher dispatcher = 
        ServletContext().getRequestDispatcher("login.jsp"); // This line doesn't working
        dispatcher.forward(request, response);
    }

}

servletContext()出错。根据Netbeans 找不到符号。我想使用参数重定向到 login.jsp

2 个答案:

答案 0 :(得分:0)

假设您正在使用HttpServlet

扩展getServletContext servlet上下文
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("login.jsp");

答案 1 :(得分:0)

  

您可以使用请求范围来代替ServletContext   将它转发到login.jsp下面是一个班轮代码。

request.getRequestDispatcher("login.jsp").forward(request,response);