Cookie+Session机制,实现用户登录功能
首先先来看看Cookies的运行机制,代码如下:
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class SetcookiesServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
Cookie usernameCookie = new Cookie("username","java");
//设置Cookie的生命周期
usernameCookie.setMaxAge(60);
//告诉用户存储C哦OkIE信
response.addCookie(usernameCookie);
//告诉用户cookies被保存了
PrintWriter writer = response.getWriter();
writer.println("<h1>Cookie保存成功!</h1>");
}
}
下面是Session的运行机制,代码如下:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
public class SessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
//session和Cookie的区别
/*
* 1.session和cookie的存储机制是不一样的,cookie是浏览器端实现的会话保持,而sessions是服务器中实现的对话
* 2.session是没有内存限制的,而cookie是有存储限制的,一般是4K
* 3.session安全性高(每次访问都有一个密钥,也就是session id),不能被篡改,而cookie信息是会被长篡改的
* */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
PrintWriter writer = response.getWriter();
if(name!=null && pwd!=null&&name.equals("root")&& pwd.equals("root")){
HttpSession session = request.getSession();
String sessionId = session.getId();
writer.println("<h1>欢迎登录<h1>");
writer.println(String.format("<h3>ID;%s</h3>",sessionId));
}else{
writer.println();
}
}
}
利用 cookies 登录代码如下:
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class GetcookiesServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
//得到Cookie
request.getCookies();
Cookie[] cookies = request.getCookies();
PrintWriter writer = response.getWriter();
for(Cookie cookie : cookies){
writer.println(String.format("<h1>Cookie name:%s,Cookie value:%s</h1>",cookie.getName(),cookie.getValue()));
}
}
}
利用Session机制登录,代码如下
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String username = request.getParameter("number1");
String pwd = request.getParameter("number2");
PrintWriter writer = response.getWriter();
if(username.equals("admin")&& pwd.equals("admin")){
writer.println(String.format("<p>登录成功!</p>"));
}else{
writer.println(String.format("<p>用户名或密码错误!请重新登录</p>"));
}
}
}
创建一个登录页面,利用Cookie+Session机制,实现用户登录功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<style>
body{
background-image: url("https://qqpublic.qpic.cn/qq_public/0/0-2732045526-67C005C93544B8D957A49EDB0C590309/900");
}
.text{
color: #f0f8ff;
}
</style>
<script>
function myck(type) {
const username = document.getElementById("n1");
const pwd = document.getElementById("n2");
if(type==1){
if(username.value==''||pwd.value==''){
alert("用户名或密码没有填写");
return false;
}
}
if(type==2){
if(confirm("确认清除?")){
username.value='';
pwd.value='';
}
}
}
</script>>
</head>
<body>
<form method="post" action="login">
<div class="text" style="text-align: center;margin-top: 325px;">
<h2>管理员登录</h2>
用户名:<input id="n1" name="number1" type="text"><p></p>
密   码:<input id="n2" name="number2" type="text"><p></p>
<input value=" 提 交 " type="submit" onclick="myck(1)">
<input value=" 清 除 " type="button" onclick="myck(2)">
</div>
</form>
</body>
</html>
运行截图如下: