的Login.jsp
<form action="LoginServlet" method="post">
<label>Email:</label>
<input type="text" name="email"/>
<label>Password:</label>
<input type="password" name="pass"/>
<input type="submit" value="Login"/>
</form>
LoginServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Boom");
String email = request.getParameter("email");
String pass = request.getParameter("pass");
RequestDispatcher dispatcher = getServletConfig().getServletContext().getRequestDispatcher("/view/Success.jsp");
AuthenticationService authService = new AuthenticationServiceImplementation();
boolean isAuthenticated = authService.authenticate(email, pass);
if(isAuthenticated) {
HttpSession session = request.getSession();
session.setAttribute("user", email);
request.setAttribute("user", email);
dispatcher.forward(request, response);
}
}
的success.jsp
<a href="/Sample_app1/LogoutServlet">Logout</a>
<% String user = (String)request.getAttribute("user"); %>
<% if(user != null && session.getAttribute("user").equals("abc@abc.com")) { %>
<p><%=user %> is logged in.</p>
<% } %>
LogoutServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
session.removeAttribute("user");
session.invalidate();
RequestDispatcher dispatcher = getServletConfig().getServletContext().getRequestDispatcher("/view/Login.jsp");
dispatcher.forward(request, response);
}
我在这里做错了什么?我不能让这个为我的生活工作。它只是简单的代码。刷新页面后如何重新发送用户名和密码,即使会话无效,也可以点击重新发送?可能是我对某些概念缺失了。请解释一下!
这是防止缓存的代码:
<% response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setDateHeader("Expires", 0); %>
我已将上述内容放在Success.jsp
中