productlist.jsp页面的实现
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<br />
<br />
<a href="<%=request.getContextPath()%>/shopping.pdo?shoppingname='联想(lenovo)拯救者R7'">联想(lenovo)拯救者R7</a>
<br />
<br />
<a href="<%=request.getContextPath()%>/shopping.pdo?shoppingname='联想(lenovo)拯救者R8'">联想(lenovo)拯救者R8</a>
<br />
<br />
<a href="<%=request.getContextPath()%>/shopping.pdo?shoppingname='联想(lenovo)拯救者R9'">联想(lenovo)拯救者R9</a>
<br />
<br />
<a href="<%=request.getContextPath()%>/shopping.pdo?shoppingname='联想(lenovo)拯救者R10'">联想(lenovo)拯救者R10</a>
</body>
</html>
productdetails.jsp页面的实现
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String pname =(String)request.getAttribute("p");
out.println(pname);
%>
<br />
<br />
拿到其他....该产品的详细参数
<br />
<br />
<br />
<br />
<a
style="display: block; width: 100px; height: 35px; line-height: 35px; text-align: center; color: red;"
href="<%=request.getContextPath()%>/addcart.pdo?pname=<%=pname %>">加入购物车</a>
</body>
</html>
shoppingcart.jsp页面的实现
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<String> products = (List<String>) session.getAttribute("car");
for(String s:products){
out.println(s+"<br/><br/>");
}
%>
</body>
</html>
servlet容器的实现
ShopController
package cn.course.mvcproject.controller;
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;
@WebServlet(urlPatterns = { "*.pdo" })
public 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();
}
}
private void shopping(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pname = req.getParameter("shoppingname");
req.setAttribute("p", pname);
req.getRequestDispatcher("/productdetails.jsp").forward(req, resp);
}
private void addcart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pname = req.getParameter("pname");
// 添加到购物车
HttpSession session = req.getSession(true);
List<String> products = (List<String>) session.getAttribute("car");
if (products == null) {
products = new ArrayList<>();
}
products.add(pname);
session.setAttribute("car", products);
resp.setCharacterEncoding("UTF-8");
resp.sendRedirect(req.getContextPath() + "/shoppingcart.jsp");
}
}