eclipse配置javaweb(会话及其会话技术)

eclipse配置javaweb(会话及其会话技术),第1张

eclipse配置javaweb(会话及其会话技术)

一.cookie对象
显示用户上次访问时间:

package cn.itcast.chapter03.cookie;

import java.io.IOException;
import java.text.SimpleDateFormat;
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")
public class LastAccessServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//指定服务器输出内容的编码方式UTF-8,防止发生乱码
		response.setContentType("text/html;charset=UTF-8");
		String lastAccessTime = null;
		//获取所有的cookie,并将这些cookie存放在数组中
		cookie[] cookies = request.getcookies();
		//遍历cookies数组
		for(int i=0; cookies !=null && i 

二.Session对象
实现购物车:

package cn.itcast.chapter03.cookie;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@WebServlet("/Book")
public class Book extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String id;
	private String name;
	
	public Book() {
		
	}
	public Book(String id, String name) {
		super();
		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;
	}
}

package cn.itcast.chapter03.cookie;

import java.util.Collection;
import java.util.linkedHashMap;
import java.util.Map;
import javax.servlet.annotation.WebServlet;

@WebServlet("/BookDB")
public class BookDB{
private static Mapbooks = new linkedHashMap();
	static {
		books.put("1", new Book("1", "javaweb 开发"));
		books.put("2", new Book("2", "jdbc 开发"));
		books.put("3", new Book("3", "java 基础"));
		books.put("4", new Book("4", "struts 开发"));
		books.put("5", new Book("5", "spring 开发"));
	}
	//获取所有的图书
	public static Collection getAll(){
		return books.values();
	}
	public static Book getBook(String id) {
		return books.get(id);
	}
}

package cn.itcast.chapter03.cookie;

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")
public class CartServlet extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		List cart = null;
		boolean purFlag = true;
		HttpSession session = req.getSession(false);
		if(session == null) {
			purFlag = false;
		}else {
			cart = (List) session.getAttribute("cart");
			if(cart == null) {
				purFlag = false;
			}
		}
		if(!purFlag) {
			out.write("对不起!您还没有购买任何商品!
"); }else { out.write("您购买的图书有:
"); double price = 0; for (Book book : cart) { out.write(book.getName() + "
"); } } } }
package cn.itcast.chapter03.cookie;

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")
public class ListBookServlet extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		Collection books = BookDB.getAll();
		out.write("本站提供的图书有:
"); for(Book book : books) { String url = "/chapert03/PurchaseServlet?id=" +book.getId(); out.write(book.getName() + " + url + "'>点击购买
"); } } }
package cn.itcast.chapter03.cookie;

import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;



@WebServlet("/PurchaseServlet")
public class PurchaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		if(id == null) {
			String url = "/chapter03/ListBookServlet";
			resp.sendRedirect(url);
			return;
		}
		Book book = BookDB.getBook(id);
		HttpSession session = req.getSession();
		List cart = (List) session.getAttribute("cart");
		if(cart == null) {
			cart = new ArrayList();
			session.setAttribute("cart", cart);
		}
		cart.add(book);
		cookie cookie = new cookie("JSESSIONID", session.getId());
		cookie.setMaxAge(60*30);
		cookie.setPath("/chapter03");
		resp.addcookie(cookie);
		String url = "/chapter03/CartServlet";
		resp.sendRedirect(url);
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5563772.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存