登录系统:
用户第一次登录,需要显示,欢迎您首次访问
下次登录时显示上次登录的时间(注意编码)
-----使用cookie
@WebServlet("/cookieDemo4")
public class CookieDemo4 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html,charset=utf-8");
//首先从前端拿到传回的数据
String username = request.getParameter("username");
String password = request.getParameter("password");
//逻辑判断
if ("zhangsan".equals(username) && "123456".equals(password)) {//登录成功代码
//设置一个默认状态为未登录
boolean isFirstVisit = true;
//判断用户是否为第一次登录
Cookie[] cookies = request.getCookies();
for (Cookie cookie:cookies){
//判断是否有我们的cookie
String name = cookie.getName();
if ("lastVisit".equals(name)){//已经登录过
isFirstVisit = false;
//说明我们已经登录过
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatNow = simpleDateFormat.format(new Date());
//创建一个新的cookie
String encode = URLEncoder.encode(formatNow, "utf-8");
Cookie cookie1 = new Cookie("lastVisit",encode);
response.addCookie(cookie1);
//得到value
String value = cookie.getValue();
String decode = URLDecoder.decode(value, "utf-8");
response.getWriter().write("欢迎回来,您上次登录的时间为:"+decode);
}
}
if (isFirstVisit){//未登录过
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatNow = simpleDateFormat.format(new Date());
//创建一个新的cookie
String encode = URLEncoder.encode(formatNow, "utf-8");
Cookie cookie1 = new Cookie("lastVisit",encode);
response.addCookie(cookie1);
//说明我们是第一次登录
response.getWriter().write("欢迎您首次登录");
}
} else {//登录失败的代码
request.setAttribute("msg", "用户名或者密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
-------
@WebFilter("/loginServlet1")
public class loginServlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//从前端拿数据
String username = request.getParameter("username");
System.out.println("username");
String password = request.getParameter("password");
System.out.println("password");
//逻辑判断
if ("karry".equals(username)&&"123456".equals(password)){
request.getSession().setAttribute("isLogin","登录过 ");
//改成重定向
response.sendRedirect("/success.jsp");
}else {
request.getSession().setAttribute("msg","用户名或者密码错误");
//改成重定向
response.sendRedirect("/login.jsp");
}
}
}
------jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/loginServlet1" method="post">
用户名:<input id="username" name="username" type="text" ><br>
密码: <input name="password" type="password" ><br>
</select><br>
<button type="submit">提交</button>
</form>
<%--<span style="color: #ff0000">${requestScope.msg}</span>--%>
<span style="color: red">${sessionScope.msg}</span>
</body>
</html>