我正在尝试编写一个非常简单的登录应用程序。当用户输入checklogin.do
的url路径时,请求将由CheckLoginStatus
servlet处理。然后,该Servlet将请求传递到相应的页面。如果这不是一个新会话,则请求将转发到form.html
以要求他们登录,否则将显示cookieresult.jsp
页面。该页面可以通过单击CheckCookie
将用户引导到click here
servlet,最后将返回formresult.jsp
。通过单击logout
,该会话将无效。
问题在于,即使我已经注销,每当我尝试返回登录页面时,我仍会被重定向到“ cookieresult.jsp”,这意味着我的cookie和属性仍然存在。有什么办法解决这个问题吗?任何建议将不胜感激。
CheckLoginStatus.java
package com.example.web;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
/**
* Servlet implementation class CheckLoginStatus
*/
@WebServlet(description = "which one container returns, login or welcome page", urlPatterns = { "/CheckLoginStatus" })
public class CheckLoginStatus extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// create session
HttpSession session = request.getSession();
session.setMaxInactiveInterval(10);
if(session.isNew()) {
response.sendRedirect("form.html");
}
else {
Cookie[] cookies = request.getCookies();
for(int i = 0; i < cookies.length; i++) {
if(cookies[i].getName().equals("username")) {
Cookie cookie = cookies[i];
String usr = cookie.getValue();
if(usr != null) {
request.setAttribute("username", usr);
RequestDispatcher view = request.getRequestDispatcher("cookieresult.jsp");
view.forward(request, response);
}
else {
RequestDispatcher view = request.getRequestDispatcher("form.html");
view.forward(request, response);
}
break;
}
}
}
}
}
form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1 align="center">Simple Session and Cookie Practice</h1>
<form method="POST" action="cookietest.do">
<div style="text-align:center;">
Username: <input type="text" name="username"><br>
Passord: <input type="password" name="password"><br>
<input type="submit" value="Submit">
</div>
</form>
</body>
</html>
CookieTest.java
package com.example.web;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
/**
* Servlet implementation class CookieTest
*/
@WebServlet(description = "create and setup cookie", urlPatterns = { "/CookieTest" })
public class CookieTest extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String usr = request.getParameter("username");
String pwd = request.getParameter("password");
request.setAttribute("username", usr);
request.setAttribute("password", pwd);
Cookie cookieUsr = new Cookie("username", usr); // put usr info on cookie
cookieUsr.setMaxAge(60);
response.addCookie(cookieUsr); // add cookie to response
Cookie cookiePwd = new Cookie("password", pwd); // put pwd info on cookie
cookiePwd.setMaxAge(60);
response.addCookie(cookiePwd); // add cookie to response
RequestDispatcher view = request.getRequestDispatcher("cookieresult.jsp");
view.forward(request, response);
}
}
cookieresult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div style="text-align:center">
<%
String usr = (String) request.getAttribute("username");
out.println("<h1 align=center>Hi " + usr + "!</h1><br>");
%>
<a href="checkcookie.do">click here</a>
</div>
</body>
</html>
CheckCookie.java
package com.example.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
/**
* Servlet implementation class CheckCookie
*/
@WebServlet(description = "get usr and return result to client", urlPatterns = { "/CheckCookie" })
public class CheckCookie extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher view = request.getRequestDispatcher("formresult.jsp");
Cookie[] cookies = request.getCookies();
// model
// password is also in cookie, but not being used here
for(int i = 0; i < cookies.length; i++) {
if(cookies[i].getName().equals("username")) {
Cookie cookie = cookies[i];
String usr = cookie.getValue();
request.setAttribute("username", usr);
break;
}
}
// view
view.forward(request, response);
}
}
formresult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<%
String usr = (String) request.getAttribute("username");
out.println("<h1>" + usr + ", here is your information.</h1><br>");
out.println("blablabla<br>");
%>
<div style="text-align:center">
<a href="logout.do">click to logout</a>
</div>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<servlet>
<servlet-name>cookie tester</servlet-name>
<servlet-class>com.example.web.CookieTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cookie tester</servlet-name>
<url-pattern>/cookietest.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>check cookie</servlet-name>
<servlet-class>com.example.web.CheckCookie</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>check cookie</servlet-name>
<url-pattern>/checkcookie.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>check login</servlet-name>
<servlet-class>com.example.web.CheckLoginStatus</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>check login</servlet-name>
<url-pattern>/checklogin.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>logout</servlet-name>
<servlet-class>com.example.web.CheckLogout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logout</servlet-name>
<url-pattern>/logout.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.example.web.MySessionCounter</listener-class>
</listener>
<session-config>
<session-timeout>2</session-timeout>
</session-config>
</web-app>