1 Session概述
(1)Session用于记录用户的状态。Session指的是一段时间内,单个客户端与Web服务器的一连串相关的交互过程。
(2)在一个Session中,客户可能会多次请求访问同一个资源,也有可能请求访问各种不同的服务器资源。
(3)Session是由服务器端创建的
2 Session原理
(1)Session会为每一次会话分配一个Session对象
(2)同一个浏览器发起的多次请求,同属于一次会话(Session)
(3)首次使用到Session时,服务器会自动创建Session,并创建Cookie存储SessionId发送回客户端
3 Session使用
Session作用域:拥有存储数据的空间,作用范围是一次会话有效
一次会话是使用同一浏览器发送的多次请求。一旦浏览器关闭,则结束会话
可以将数据存入Session中,在一次会话的任意位置进行获取
可传递任何数据(基本数据类型、对象、集合、数组)
版权声明:改引用为CSDN博主「雨天的木子李」的原创文章
原文链接:https://blog.csdn.net/swy2560666141/article/details/129046563
主页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="login" method="post">
username:<input type="text" name="username" placeholder="请输入用户名"><br>
password:<input type="password" name="password" placeholder="请输入密码">
<input type="submit" value="提交">
<input type="reset">
</form>
</body>
</html>
该系统主要通过html页面的表单结构来完成用户的登录。
用户从选框中输入用户名和密码,提交数据到服务端,
<form action="login" method="post">
根据表单中的action进入到login中,表单中的数据以"post"方法传递。
login
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* 用户名:admin 密码:123456
* 1.拿到表单中的用户名和密码
* 2.和数据库中的用户名和密码对比
* 3.如果对比正确 重定向到个人中心Inner
* 4.如果对比不正确 给用户一个提示 给一个新的链接 跳转到新的登录页面
*/
@WebServlet("/login")
public class Login extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
if (("admin").equals(username) && ("123456").equals(password)){
//如果正确
HttpSession session = req.getSession();
//只有成功登录的用户session中 才有 user = username 的信息
session.setAttribute("user",username);
String contextPath = this.getServletContext().getContextPath();
resp.sendRedirect(contextPath+"/inner");
}else{
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().write("用户密码错误,请"+"<a href = \"login.html\">重新返回登录界面<a>");
}
}
}
首先获得用户输入的两个数据,
String username = req.getParameter("username");
String password = req.getParameter("password");
若账号密码对比正确,
HttpSession session = req.getSession();
session.setAttribute("user",username);
getSession()方法获取一个session对象用户储存当前会话的数据。由于Inner中需要存在欢迎用户的功能,这里只保存了username中的数据来显示用户名,通过session.setAttribute()方法来完成。user为seesion对象的名称。
String contextPath = this.getServletContext().getContextPath();
this.getServletContext().getContextPath()用来获取当前Servlet的绝对路径,也可以说是项目名。contextPath = /项目名
resp.sendRedirect(contextPath+"/inner");
最后组合相对路径重定向到用户Inner。
若账号密码对比不正确,则
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().write("用户密码错误,请"+"<a href = \"login.html\">重新返回登录界面<a>");
通过超链接标签重新回到login.html。
resp.setContentType(“text/html;charset=UTF-8”); 用于防止中文乱码。
Inner
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;
@WebServlet("/inner")
public class Inner extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
HttpSession session = req.getSession();
out.write("欢迎"+session.getAttribute("user")+"!");
out.write("<a href=\"logout\">安全退出<a>");
}
}
首先再次获取session对象,
HttpSession session = req.getSession();
最后获取session中的数据来完成用户欢迎功能。
session.getAttribute("user")
出于对用户的安全性考虑,在退出后进入logout。
logout
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/logout")
public class LoginOut extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.invalidate();
resp.sendRedirect("login.html");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
HttpSession session = req.getSession();
session.invalidate();
获取session对象后,通过invalidate();方法清除掉该session。
最后重定向回主页面。