@WebServlet("/dlu")
public class Dlu extends HttpServlet {
static int i=0;
static String decode=null;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String pass = request.getParameter("pass");
if ("sensen".equals(username) && "123".equals(pass)) {
i++;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String format = sdf.format(date);
String encode = URLEncoder.encode(format, "utf-8");
Cookie cookie = new Cookie("Date", encode);
response.addCookie(cookie);
if (i == 1) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
System.out.println(cookies.length);
for (Cookie cookie1 : cookies) {
String name = cookie1.getName();
String value = cookie1.getValue();
if ("Date".equals(name)){
decode = URLDecoder.decode(value, "utf-8");
System.out.println("实时登录时间为" + decode);
request.setAttribute("de", decode);
response.addCookie(cookie);
}
}
// for (int i1 = 0; i1 < cookies.length; i1++) {
// decode = URLDecoder.decode(cookies[1].getValue(), "utf-8");
// System.out.println("第一次登录时间为" + decode);
// request.setAttribute("de", decode);
// }
}
else {
System.out.println("cookie数组为空");
}
request.setAttribute("first","第一次登录!欢迎!");
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Mainym.jsp");
requestDispatcher.forward(request, response);
} else{
System.out.println("你上次登录时间为:"+decode);
request.setAttribute("de1", decode);
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie1 : cookies) {
String value = cookie1.getValue();
decode = URLDecoder.decode(value, "utf-8");
System.out.println("实时登录时间为" + decode);
request.setAttribute("de2", decode);
}
}
else {
System.out.println("cookie数组为空");
}
request.setAttribute("after","欢迎回来!");
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Mainym.jsp");
requestDispatcher.forward(request, response);
}
}else {
System.out.println("用户名或密码错误");
request.setAttribute("tishi","用户名或密码错误!请重新登录!");
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Dlu.jsp");
requestDispatcher.forward(request,response);
}
}
}
JSP
登录
<html>
<head>
<title>登录</title>
</head>
<body>
<div>
<form action="/dlu" method="post">
<input type="text" name="username" id="username" placeholder="用户名:">
<br>
<input type="password" name="pass" id="pass" placeholder="密码:">
<button type="submit">登录</button>
</form>
</div>
<span style="color: red">
${requestScope.tishi}
</span>
</body>
</html>
登录成功
<html>
<head>
<title>欢迎!</title>
</head>
<body>
<span style="color: blue;font-size: 40px">
${requestScope.first}
</span>
<span style="color: blue;font-size: 40px">
${ requestScope.after}
</span>
<span style="color: green">
第一次登录的时间为:${requestScope.de}
</span>
<br>
<span style="color: red">
您上次登录的时间为:${requestScope.de1}
</span>
<span style="color: red">
实时登录的时间为:${requestScope.de2}
</span>
</body>
</html>
结果