打开eclipse建立,建立Dynamic Web Project 工程 命名 shoppingcart,注意需要配置xml文件。
工程目录建立如下图:
ShopController.java
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
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;
//另一种配置方法,该方法配置xml需要注意,该功能不能被禁用
@WebServlet(urlPatterns= {"*.pdo"})
public final class ShopController extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决中文乱码
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
String mn = req.getServletPath();
mn = mn.substring(1);
mn = mn.substring(0, mn.length()-4);
//利用反射
try {
Method method = this.getClass().getDeclaredMethod(mn, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unused")
private void shopping(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pname = req.getParameter("pname");//提交过来商品信息的获取
req.setAttribute("pname", pname);
req.getRequestDispatcher("/productdetails.jsp").forward(req, resp);//转发
}
@SuppressWarnings("unused")
private void addcar(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pname = req.getParameter("pname");
HttpSession session = req.getSession(true);
@SuppressWarnings("unchecked")
List<String> list = (List<String>)session.getAttribute("car");
if(null == list) {
list = new ArrayList<>();
}
//添加到购物车
list.add(pname);
session.setAttribute("car", list);
//resp.setCharacterEncoding("UTF-8");
//resp.getWriter().println("添加成功!");
resp.sendRedirect(req.getContextPath() + "/productcars.jsp");
}
}
注意
//另一种配置方法,该方法配置xml需要注意,该功能不能被禁用
@WebServlet(urlPatterns= {“*.pdo”})
配置更简便
productlist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<br><br>
<!-- 提交到接收pdo的servlet中 -->
<a href="<%=request.getContextPath()%>/shopping.pdo?pname=联想笔记本">联想笔记本</a>
<br><br>
<a href="<%=request.getContextPath()%>/shopping.pdo?pname=华硕主板">华硕主板</a>
<br><br>
<a href="<%=request.getContextPath()%>/shopping.pdo?pname=金士顿优盘">金士顿优盘</a>
<br><br>
<a href="<%=request.getContextPath()%>/shopping.pdo?pname=飞行棋">飞行棋</a>
<br><br>
<a href="<%=request.getContextPath()%>/shopping.pdo?pname=TCL显示屏">TCL显示屏</a>
<br><br>
<a href="<%=request.getContextPath()%>/shopping.pdo?pname=火星土豆">火星土豆</a>
<br><br>
</body>
</html>
productdetails.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<br><br>
该产品的详细信息。
<br><br>
<form action="<%=request.getContextPath()%>/addcar.pdo?pname=<%=request.getAttribute("pname")%>" ><!-- 提交一次 -->
<input type="hidden" name="pname" value="<%=request.getAttribute("pname") %>"> <!-- 再次提交,覆盖之前的 因为都是pname -->
<input style="width: 120px; height: 30px; background: red; color: #fff;" type="submit" value="加入购物车" >
</form>
<br><br><br><br>
<a style="display: block; width: 120px; height: 30px; background: red; color: #fff;">加入购物车</a>
</body>
</html>
注意代码中哟两次提交,我们窒息要提交一次就可以。
productcars.jsp
<%@page import="javafx.scene.control.Alert"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
@SuppressWarnings("unchecked")
List<String> list = (List<String>)session.getAttribute("car");
if(list != null && list.size() > 0) {
for(String str : list) {
out.print("<br><br>" + str);
}
}
%>
</body>
</html>
结果测试
打开你的浏览器,输入网址:
http://localhost:8080/shoppingcart/productlist.jsp
进行测试。