参考:https://blog.csdn.net/Archer__13/article/details/123769557
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/sessionServlet")
public class SessionServlet extends BaseServlet{
//Session的创建和获取
protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建和获取Session会话对象
HttpSession session = req.getSession();
//判断当前Session会话是否是新创建的
boolean isNew = session.isNew();
//获取Session会话的唯一标识id
String id = session.getId();
resp.getWriter().write(id + "<br/>");
resp.getWriter().write(isNew + "<br/>");
}
//向Session域中存数据
protected void storeData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().setAttribute("key1","value1");
}
//从Session域中取数据
protected void getData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object key1 = req.getSession().getAttribute("key1");
resp.getWriter().write(key1 + "<br/>");
}
//Session生命周期控制
protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int maxInactiveInterval = req.getSession().getMaxInactiveInterval(); //获取Session默认的超时时长,为30分钟
//想改变Session的默认超时时长,需要在web.xml中配置如下:<session-config>
// <session-timeout>20</session-timeout>
// </session-config>
resp.getWriter().write(maxInactiveInterval + "<br/>");
}
protected void life(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setMaxInactiveInterval(3); //设置当前的Session在3秒后超时,会销毁(超时之后则不能再获取Session域中的值
}
protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.invalidate(); //让session立刻超时
}
}
package servlet;
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.lang.reflect.Method;
public abstract class BaseServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 解决post请求中文乱码问题
req.setCharacterEncoding("UTF-8");
// 解决响应中文乱码问题
resp.setContentType("text/html; charset=UTF-8");
String action = req.getParameter("action");
try {
// 获取action业务鉴别字符串,获取相应的业务 方法反射对象
Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
// System.out.println(method);
// 调用目标业务 方法
method.invoke(this, req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Session</title>
<base href="http://localhost:8080/StudyJSP_war_exploded/">
<style type="text/css">
ul li {
list-style: none;
}
</style>
</head>
<body>
<iframe name="target" width="500" height="500" style="float: left;"></iframe>
<div style="float: left;">
<ul>
<li><a href="sessionServlet?action=createOrGetSession" target="target">Session的创建和获取(id号、是否为新创建)</a></li>
<li><a href="sessionServlet?action=storeData" target="target">Session域数据的存储</a></li>
<li><a href="sessionServlet?action=getData" target="target">Session域数据的获取</a></li>
<li>Session的存活</li>
<li>
<ul>
<li><a href="sessionServlet?action=defaultLife" target="target">Session的默认超时及配置</a></li>
<li><a href="sessionServlet?action=life" target="target">Session3秒超时销毁</a></li>
<li><a href="sessionServlet?action=deleteNow" target="target">Session马上销毁</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>
|