这个程序主要是有三个java程序和1个html来完成的(后面还有一个web.xml文件),每个文件中都有详细的注解,所以就不再详细介绍具体的步骤了,接下来看一看我的代码吧!
package com.nick.web;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 此为第一个servlet的程序,用于添加商品放入购物车中,用的方法是在客户端添加cookie,存放时间为7天
*
*/
public class Demo1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从点击中获得商品名
String pname = request.getParameter("pname");
//获得cookie(用于判断是否有原来的cookie)
Cookie[] cookies = request.getCookies();
//cookie为空,说明是第一次
if(cookies==null){
//第一次添加cookie
Cookie cookie = new Cookie("cart",URLEncoder.encode(pname,"utf-8"));
cookie.setMaxAge(60*60*24*7);
response.addCookie(cookie);
}else{
//cookie不为空,但有可能不含我们要添加的Cookie,所以遍历数组来判断
for (Cookie cookie2 : cookies) {
if("cart".equals(cookie2.getName())){
//cookie中存在需要添加的cookie,那么这就是第二次添加,两个商品之间用"_"分割(这里cookie键和值的添加因为中文的关系用到了URLEncoder.encoder和URLDecoder.decode的方法)
String new_value = URLDecoder.decode(cookie2.getValue(),"utf-8")+"_"+pname;
Cookie cookie = new Cookie("cart",URLEncoder.encode(new_value,"utf-8"));
//cookie的存放在浏览器的时间为7天
cookie.setMaxAge(60*60*24*7);
response.addCookie(cookie);
}else{
//这里是第一次添加cookie
Cookie cookie = new Cookie("cart",URLEncoder.encode(pname,"utf-8"));
cookie.setMaxAge(60*60*24*7);
response.addCookie(cookie);
}
}
}
//跳转回前端页面
response.sendRedirect(request.getContextPath()+"/1.html");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.nick.web;
import java.io.IOException;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 此为第二个servlet的程序,用于解决查看购物车的详细情况,用到的方法为遍历浏览器中发给我们的cookie
* 在浏览器中显示我们购物车中的商品
*
*
*/
public class Demo2 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决浏览器中显示的中文乱码问题
response.setContentType("text/html;charset=utf-8");
//获取cookie,用于判断是否有值
Cookie[] cookies = request.getCookies();
if(cookies==null){
//没有商品的添加
response.getWriter().write("购物车没有物品!");
}else{
for (Cookie cookie : cookies) {
//判断有商品添加时,就需要在浏览器中写入商品
if("cart".equals(cookie.getName())){
String value = cookie.getValue();
String[] vals = value.split("_");
for (String string : vals) {
//浏览器显示
response.getWriter().write(URLDecoder.decode(string,"utf-8")+"\n");
}
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.nick.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 此为第三个servlet的程序,用于清空购物车,其方法为将浏览器中的cookie的值改为空(或者任意值),再讲存放时间改为关闭浏览器cookie就消失
*
*/
public class Demo3 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//中文乱码问题
response.setContentType("text/html;charset=utf-8");
Cookie[] cookies = request.getCookies();
if(cookies==null){
response.getWriter().write("本来就没有了!");
}
for (Cookie cookie : cookies) {
if("cart".equals(cookie.getName())){
//找到指定的cookie,再删除cookie
Cookie cookie1 = new Cookie("cart","");
cookie1.setMaxAge(0);
response.addCookie(cookie1);
response.getWriter().write("已经清空了!");
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Insert title here
day15_Demo
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
Demo1
Demo1
com.nick.web.Demo1
Demo1
/demo1
Demo2
Demo2
com.nick.web.Demo2
Demo2
/demo2
Demo3
Demo3
com.nick.web.Demo3
Demo3
/demo3
以上就是我要展示的代码,如有错误,请多多指正!