首先创建一个登录页
创建一个current_user值 使用session方法储存
创建一个判断如果不为空这转到index.jsp(登录成功)页面
<%--
Created by IntelliJ IDEA.
User: wyl
Date: 2021/5/25
Time: 9:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登陆页面</title>
</head>
<body>
<%
String current_user = (String)session.getAttribute("current_user");
if (current_user!=null){
response.sendRedirect("index.jsp");
}
//获取所有cookie数字
String username = "";
String password = "";
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies){
out.print(cookie.getName()+"-----"+cookie.getValue());
//cookie通过键值对的形式来储存数据
if (cookie.getName().equals("username")){
//获取到值
username = cookie.getValue();
continue;
}
if (cookie.getName().equals("password")){
password = cookie.getValue();
continue;
}
}
%>
<form action="servlet.jsp" method="get">
<table>
<tr>
<td>用户名</td>
<td><input type="text"name="username" placeholder="请输入用户名"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" placeholder="请输入密码"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
创建一个servlet.jsp()页面
创建一个username 和 password把login的用户名和密码转过来判断
如果正确就保存进current_user 然后进入index.jsp(登录成功)页面
如果不正确则进入error.jsp(登陆失败页面)
<%--
Created by IntelliJ IDEA.
User: wyl
Date: 2021/5/25
Time: 9:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<%
//判断用户信息
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("王雨亮")&&password.equals("123")){
session.setAttribute("current_user",username);
Cookie usernamecookie = new Cookie("username",username);
Cookie userpasswordcookie = new Cookie("password",password);
//设置Cookie存活事件
usernamecookie.setMaxAge(18000);
userpasswordcookie.setMaxAge(18000);
response.addCookie(usernamecookie);
response.addCookie(userpasswordcookie);
response.sendRedirect("index.jsp");
}else {
response.sendRedirect("error.jsp");
}
%>
</body>
</html>
创建一个index.jsp
使用session方法获取其中的信息,判断如果为空的话就是其中没有保存用户名和信息则转到login.jsp
(登陆页)页面
最后超链接是链接clean_session.jsp点击之后用来删除session中储存的数据然后转到login.jsp
<%--
Created by IntelliJ IDEA.
User: wyl
Date: 2021/5/25
Time: 9:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<%
/*获取session中的信息*/
String current_user = (String) session.getAttribute("current_user");
if (current_user == null){
//如果session中没有当前的用户信息,表示用户还没有登陆
response.sendRedirect("login.jsp");
}
%>
<h1>
早上好啊!<%=current_user%> , 今天好高兴美好的一天!!!!!!!!!!!!!!!!!!!!!!
</h1>
<a href="clean_session.jsp">logout</a>
</body>
</html>
创建一个clean_session.jsp页面
用于删除session中的数据,然后挑战到login.jsp(登录页)
<%--
Created by IntelliJ IDEA.
User: wyl
Date: 2021/5/25
Time: 11:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>当前页面主要作用是清空用户登录信息</title>
</head>
<body>
<%
//删除session中的数据
session.removeAttribute("current_user");
//删除后再次跳入首页,判断是否清除成功
response.sendRedirect("index.jsp");
%>
</body>
</html>
最后创建一个error.jsp页面
主要显示失败之后的页面
<%--
Created by IntelliJ IDEA.
User: wyl
Date: 2021/5/25
Time: 9:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1 style="color: red;text-align: center">用户名或密码错误</h1>
</body>
</html>