gpt4 book ai didi

java - jsp:forward 删除jsp页面中的所有html

转载 作者:行者123 更新时间:2023-11-30 07:41:47 24 4
gpt4 key购买 nike

我对使用 jsp 和 servlet 很陌生,所以请耐心等待。我正在构建的网站有一个登录表单来访问其余页面。当您登录时,它会使用您的用户名设置一个 cookie,其目的是让其他页面在您尝试访问它们时检查此 cookie。

登录表单在操作中运行此登录 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页面上,我有一段代码要在开始正文标记之后运行:

<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"/> 的任何页面上。在里面。有什么问题的想法吗?

最佳答案

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

关于java - jsp:forward 删除jsp页面中的所有html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34569209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com