jsp:forward从jsp页面中删除所有html

时间:2016-01-02 18:24:48

标签: java jsp servlets cookies

我是使用jsp和servlet的新手,所以请耐心等待。我所建立的网站有一个登录表单来访问其余页面。当您登录时,它会使用您的用户名设置一个cookie,并且当您尝试访问它时,其他页面的想法是检查此cookie。

登录表单在操作中运行此Login Servlet:

public class LoginServlet extends HttpServlet implements Constants {

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {


    String Username = request.getParameter("username");
    String password = request.getParameter("password");

    if (StringUtils.isStringEmpty(Username) || StringUtils.isStringEmpty(password)) {

        RequestDispatcher rd = request.getRequestDispatcher("/index.html");
        rd.forward(request, response);

    } else {
        System.out.println(Username + password);
        UserManager uMgr = new UserManager();
        User user = uMgr.loginUser(Username, password);
        if (user == null) {
            RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
            rd.forward(request, response);
        } else {

            if (user.isIsAdmin() == TRUE) {
                Cookie loginCookie = new Cookie("ADMIN", Username);
                //setting cookie to expiry in 30 mins
                loginCookie.setMaxAge(30*60);
                response.addCookie(loginCookie);
                request.getSession(true).setAttribute(SESSION_ADMIN, user);

                RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                rd.forward(request, response);


            } else {
                RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
                rd.forward(request, response);
            }
        }
    }

}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

在index.jsp页面上,我有一些代码在开始的body标签之后运行:

<jsp:forward page="CookieServlet"/>

这是CookieServlet:

public class CookieServlet extends HttpServlet {

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String userName = null;
    Cookie[] cookies = request.getCookies();
    if(cookies !=null){
    for(Cookie cookie : cookies){
        if(cookie.getName().equals("ADMIN")) userName = cookie.getValue();
    }
    }
    if(userName == null) response.sendRedirect("login.jsp");
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

登录代码有效,它为我创建了cookie,检查cookie的cookieservlet也检查了cookie,但没有任何HTML显示在其中<jsp:forward page="CookieServlet"/>的任何页面上。什么错误的想法?

2 个答案:

答案 0 :(得分:1)

当你前进时,你会从一个页面转到另一个页面。当然,新页面上的信息与旧页面上的信息不同。如果您导航到某个页面但仍然显示旧页面,那将是一个错误。

答案 1 :(得分:0)

没有错。这就是前锋的意义所在。转发到另一个资源就像是说:“我已完成了此请求的部分工作,现在将控制转移到其他资源”。

您不应该在视图中检查登录信息。你甚至不应该在控制器中检查它。在调用任何控制器之前,您应该在中心位置检查它:servlet filter