会话及其会话技术
1、Cookie对象 cookie是一种会话技术,它用于将会话过程中的数据保存到用户的浏览器中,从而使浏览器和服务器可以更好的地进行数据交互; (1)通过Cookie技术实现显示用户上次的访问时间功能,退出浏览器在进入时,浏览器会删除cookie对象 实现方法如下:
package cn.com;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet({ "/LastAccessServlet", "/lastAccess" })
public class LastAccessServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
System.out.println("收到前端请求");
Cookie[] cookies=request.getCookies();
String lastTime=null;
for(int i=0;cookies!=null && i<cookies.length;i++) {
String name=cookies[i].getName();
if("lastAccess".equals(name)) {
lastTime=cookies[i].getValue();
}
}
if(lastTime==null) {
response.getWriter().print("你是首次访问本网站");
}
else {
response.getWriter().print("你上次的访问时间"+lastTime);
}
String time=String.format("%tF<tT", new Date());
Cookie cookie=new Cookie("lastAccess",time);
cookie.setMaxAge(60*60*24*7);
response.addCookie(cookie);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
2.Session对象:
Cookie技术可以将用户的信息保存在各自的浏览器中,并且可以在多次请求下实现数据的共享,但是,如果传递的信息比较多,使用Cookie技术显然会增大服务器程序处理的难度,这时,可以使用Session技术;
实现购物车:
package cn.com;
import java.io.Serializable;
public class Book implements Serializable{
private static final long serialVersionUIF=1L;
private String id;
private String name;
public Book() {
}
public Book(String id,String name) {
this.id=id;
this.name=name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id= id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
}
```创建数据库模拟类:
```c
package cn.com;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class BookDB {
private static Map<String,Book> map = new LinkedHashMap<String,Book>();
static{
map.put("1", new Book("1","javaWeb"));
map.put("2", new Book("2","jdbc入门"));
map.put("3", new Book("3","java基础"));
map.put("4", new Book("4","struts框架"));
map.put("5", new Book("5","hibernate框架"));
}
public static Collection<Book> getAll(){
return map.values();
}
public static Book getBook(String id){
return map.get(id);
}
}
创建一个名为ListBookServlet的类,用于操作单击 “购买” 链接,就可以把书加进购物车;
package cn.com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet({ "/ListBookServlet", "/listBook" })
public class ListBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
Collection<Book> books=BookDB.getAll();
out.print("以下是本网站售卖的所有图书:"+"<br/><br/>");
for(Book b:books) {
String name=b.getName();
String id=b.getId();
String url="<a href='/hello3/purcharse?id="+id+"'>点击购买</a>";
out.print("图书名称:"+name+" "+url+"<br/><br/>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
效果如下图所示: 创建一个名为PurchaseServlet的servlet类,其作用是保存到Session对象中
package cn.com;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet({ "/PurchaseServlet", "/purchase" })
public class PurchaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String id=request.getParameter("id");
if(id==null) {
String url="/hello3/ListBookServlet";
response.sendRedirect(url);
return;
}
Book book=BookDB.getBook(id);
HttpSession session=request.getSession();
List<Book> cart=(List) session.getAttribute("cart");
if(cart==null) {
cart=new ArrayList<Book>();
session.setAttribute("cart", cart);
}
cart.add(book);
Cookie cookie=new Cookie("JSESSIONID",session.getId());
cookie.setMaxAge(60*30);
cookie.setPath("/hello3");
response.addCookie(cookie);
response.sendRedirect("/hello3/CartServlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
创建一个名称为CartServlet的Servlet类,显示用户义卖图书列表:
package cn.com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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;
@WebServlet({ "/CartServlet", "/cart" })
public class CartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
List<Book> cart=null;
HttpSession session=request.getSession(false);
boolean cartFlag=true;
if(session==null) {
cartFlag=false;
}
else {
cart=(List) session.getAttribute("cart");
if(cart==null) {
cartFlag=false;
}
}
if(!cartFlag) {
out.write("对不起,你的购物车里面没有图书,请购买!");
}
else {
out.write("购买图书如下:"+"<br/>");
for(Book book:cart) {
out.write("购买的图书:"+book.getName()+"<br/>");
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
选择一个书籍后实现效果如下: 3、使用session技术实现用户登录 1、创建用户封装信息类的类 2、创建indexServlet类用于显示网站的首页面,并显示登录信息,为了判断用户是否登录,该类实现时,获取了保存了用户信息的session对象 3、创建用于显示用户登陆成功的页面的loginservlet类,如果登陆成功,则跳转到网站首页,否则登陆失败 4、通过编写logoutservlet用于完成用户注销功能,当用户点击‘退出’时,该类将session对象的用户信息移除并返回网站首页面 5、创建一个html文件用于填写用户登录表单信息
|