知识点汇总
1.session机制采用的是在服务器端保持HTTP状态信息的方案。
2.session通过SessionID来区分不同的客户,session是以cookie或URL重写为基础的
默认使用cookie来实现,系统会创造一个名为JSESSIONID的输出cookie,session
cookie是存储到浏览器内存中的并不是写到硬盘上。
3.HttpSession的生命周期
page指令的session=’false‘当前页面禁用session隐含变量,但可以使用显示的HttpSession对象
销毁HttpSession对象:①直接调用HttpSession的invalidate()方法使得HTTP Session失效
②服务器卸载当前的WEB应用
③超过HttpSession的过期时间
4.设置过期时间:①秒为单位setMaxInactiveInterval()②在web.xml文件中设置
HttpSession的过期时间单位分钟<session-config><session-timeout>10</session-timeout></session-config>
5.利用URL重写实现Session的跟踪,Servlet中一种补充的会话管理机制,它允许不支持
Cookie的浏览器中也可以与WEB服务器保持连续的会话。将会话标识号以参数形式
附加在超链接的URL地址后面的技术称为URL重写
HttpServletResponse接口中定义了两个用于完成URL重写方法:encodeURL方法,encodeRedirectURL方法。
一:使用举例
1.创建login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
SessionId: <%=session.getId()%><br>
IsNew:<%=session.isNew()%><br>
MaxInactiveInterval:<%=session.getMaxInactiveInterval()%><br>
CreateTime:<%=session.getCreationTime()%><br>
LastAccessTime:<%=session.getLastAccessedTime()%><br>
<%
Object username=session.getAttribute("username");
if(username==null){
username="";
}
%>
<form action="hello.jsp" method="post">
<%--<form action="<%=response.encodeURL(hello.jsp)%>" method="post"> 在禁用cookie的情况下使用--%>
username:<input type="text" name="username" value="<%=username%>"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
2.创建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
SessionId: <%=session.getId()%><br>
IsNew:<%=session.isNew()%><br>
MaxInactiveInterval:<%=session.getMaxInactiveInterval()%><br>
CreateTime:<%=session.getCreationTime()%><br>
LastAccessTime:<%=session.getLastAccessedTime()%><br>
<%
session.setAttribute("username",request.getParameter("username"));
%>
<a href="login.jsp">重新登录</a>
<br>
<a href="logOff.jsp">注销</a>
</form>
</body>
</html>
3.创建logOff.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
SessionId: <%=session.getId()%><br>
IsNew:<%=session.isNew()%><br>
MaxInactiveInterval:<%=session.getMaxInactiveInterval()%><br>
CreateTime:<%=session.getCreationTime()%><br>
LastAccessTime:<%=session.getLastAccessedTime()%><br>
Bye:<%=session.getAttribute("username")%>
<%session.invalidate();%>
<a href="login.jsp">重新登录</a>
</body>
</html>
二.实现简易购物车功能
1.创建step1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>step1</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/processStep1" method="post">
<table border="1"cellpadding="10"cellspacing="0">
<tr>
<td>书名</td>
<td>购买</td>
</tr>
<tr>
<td>java</td>
<td><input type="checkbox"name="book" value="java"/></td>
</tr><tr>
<td>javaWeb</td>
<td><input type="checkbox"name="book" value="javaWeb"/></td>
</tr><tr>
<td>Python</td>
<td><input type="checkbox"name="book" value="Python"/></td>
</tr><tr>
<td>C#</td>
<td><input type="checkbox"name="book" value="C#"/></td>
</tr><tr>
<td>C</td>
<td><input type="checkbox"name="book" value="C"/></td>
</tr>
<tr>
<td><input type="submit"value="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
创建响应的Servlet
public class ProcessStep1Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取图书信息
String [] books=request.getParameterValues("book");
//将图书信息放入HttpSession中
request.getSession().setAttribute("books",books);
//重定向
response.sendRedirect("/javaweb4/step2.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
//并在web.xml中添加相应的配置信息
2.创建step2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>step2</title>
</head>
<body>
<h3>输入地址和信用卡信息</h3>
<form action="/javaweb4/processStep2" method="post">
<table cellpadding="10"cellspacing="10"border="1">
<tr>
<td colspan="2">寄送信息</td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>寄送地址</td>
<td><input type="text"name="address"/></td>
</tr><tr>
<td colspan="2">信用卡信息</td>
</tr>
<tr>
<td>种类:</td>
<td><input type="radio" name="cardType"value="Visa"/>Visa
<input type="radio" name="cardType" value="Master"/>Master</td>
</tr>
<tr>
<td>卡号:</td>
<td><input type="text"name="card"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit"name="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
创建响应的Servlet
public class ProcessStep2Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取所有信息
String name=request.getParameter("name");
String address=request.getParameter("address");
String cardType=request.getParameter("cardType");
String card=request.getParameter("card");
Customer customer=new Customer(name,address,cardType,card);
//放到session中
HttpSession session=request.getSession();
session.setAttribute("customer",customer);
//重定向
response.sendRedirect("/javaweb4/confirm.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
//并在web.xml中添加相应的配置信息
3.创建confirm.jsp
<%@ page import="cn.javaweb4.com.Customer" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>confirm</title>
</head>
<body>
<%
Customer customer=(Customer)session.getAttribute("customer");
String [] books=(String[])session.getAttribute("books");
%>
<table>
<tr>
<td>顾客姓名:</td>
<td><%=customer.getName()%></td>
</tr> <tr>
<td>地址:</td>
<td><%=customer.getAddress()%></td>
</tr> <tr>
<td>卡号:</td>
<td><%=customer.getCard()%></td>
</tr><tr>
<td>卡类型:</td>
<td><%=customer.getCardType()%></td>
</tr>
<tr>
<td>Books:</td>
<td>
<%
for(String book:books){
out.print(book);
}
%>
</td>
</tr>
</table>
</body>
</html>
三.重复提交问题
1.创建index.jsp
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<%
String tokenValue=new Date().getTime()+"";
session.setAttribute("token",tokenValue);
%>
<form action="<%=request.getContextPath()%>/tokenServlet" method="post">
<input type="hidden" name="token"value="<%=tokenValue%>"/>
name:<input type="text"name="name"/>
<input type="submit"value="Submit"/>
</form>
</body>
</html>
2.创建对应的Servlet
public class TokenServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
Object token=session.getAttribute("token");
String tokenValue=request.getParameter("token");
if(token!=null&& token.equals(tokenValue)){
session.removeAttribute("token");
}else {
response.sendRedirect(request.getContextPath()+"/javaweb4/token.jsp");
return;
}
}
}
//在web.xml中加入相关配置
3.创建提示页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>repeat</title>
</head>
<body>
<h3>重复提交</h3>
</body>
</html>