简体   繁体   English

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

[英]jsp:forward removes all the html from the jsp page

I'm new to using jsp and servlets so bear with me. 我是使用jsp和servlet的新手,所以请多多包涵。 The website I'm building has a login form to access the rest of the pages. 我正在建立的网站上有一个登录表单,可以访问其余页面。 When you login it sets a cookie with your username and the idea is for the other page to check for this cookie when you try to access them. 登录时,它会使用您的用户名设置一个cookie,这个想法是让另一页在您尝试访问它们时检查该cookie。

The login form runs this Login Servlet in the action: 登录表单在操作中运行以下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>

}

On the index.jsp page I have this bit of code to run just after the opening body tag: 在index.jsp页面上,我有以下代码要在开始body标签之后运行:

<jsp:forward page="CookieServlet"/>

This is the 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>

}

The login code works and it creates the cookie for me and the cookieservlet that checks for the cookie also does the check for the cookie but none of the HTML displays on any of the pages with the <jsp:forward page="CookieServlet"/> in it. 登录代码有效,并且为我创建了cookie,并且检查该cookie的cookieservlet也对cookie进行了检查,但是任何<jsp:forward page="CookieServlet"/>的页面上都没有显示HTML。在里面。 Any ideas with what is wrong? 有什么错误的主意吗?

When you forward, you go from one page to another. 转发时,您从一页转到另一页。 Naturally the information on the new page is different from the information on the old page. 自然,新页面上的信息不同于旧页面上的信息。 It would be a bug if you navigate to a page and still have the old page displayed. 如果您导航到页面并仍然显示旧页面,那将是一个错误。

Nothing is wrong. 没有错误。 That's what a forward is for. 那就是前锋的目的。 Forwarding to another resource is like saying: "I've done my part of the job for this request, now transfer the control to this other resource". 转发到另一个资源就像在说:“我已经完成了该请求的工作,现在将控件转移到了另一个资源”。

You should not check the login in the view. 您不应在视图中检查登录名。 You shouldn't even check it in the controller. 您甚至都不应该在控制器中检查它。 You should check it in a central place, before any controller is invoked: a servlet filter . 在调用任何控制器之前,您应该在一个中心位置检查它: servlet过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM