JAVAEE中用Session簡單實現(xiàn)購物車功能示例代碼
更新時間:2017年03月01日 14:15:40 作者:張行之
本篇文章主要介紹了JAVAEE中用Session簡單實現(xiàn)購物車功能示例代碼,非常具有實用價值,需要的朋友可以參考下。
Session簡單實現(xiàn)購物車功能
這個小程序主要就3個頁面,一個商品列表頁面(HomeServlet),一個是提示加入購物車頁面(AddCartTipServlet),一個是顯示購物車清單頁面(ShowCartServlet)。
HomeServlet頁面:
@WebServlet({ "/HomeServlet", "/home" })
public class HomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HomeServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<h2>書單</h2><hr/><br/>");
out.print("人類簡史<a href='"+request.getContextPath()+"/addCartTip?id=1'>加入購物車</a><br/>");
out.print("未來簡史<a href='"+request.getContextPath()+"/addCartTip?id=2'>加入購物車</a><br/>");
out.print("世界簡史<a href='"+request.getContextPath()+"/addCartTip?id=3'>加入購物車</a><br/>");
out.print("時間簡史<a href='"+request.getContextPath()+"/addCartTip?id=4'>加入購物車</a><br/>");
out.print("<a href='"+request.getContextPath()+"/show/cart'>查看購物車</a><br/>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
AddCartTipServlet頁面:
@WebServlet({ "/AddCartTipsServlet", "/addCartTip" })
public class AddCartTipsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddCartTipsServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
HttpSession session = request.getSession();
List<String> list = (List<String>) session.getAttribute("cart");
if(list==null){
list=new ArrayList<>();
}
String id = request.getParameter("id");
list.add(id);
session.setAttribute("cart", list);
System.out.println(list.toString());
response.getWriter().println("已加入購物車<br/>"
+ "<a href='"+request.getContextPath()+"/home'>繼續(xù)購物</a><br/>"
+ "<a href='"+request.getContextPath()+"/show/cart'>查看購物車</a><br/>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
ShowCartSevlet頁面
@WebServlet({ "/ShowCartServlet", "/show/cart" })
public class ShowCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ShowCartServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
List<String> list = (List<String>)request.getSession().getAttribute("cart");
if(list!=null){
out.print("你的購物清單:<br/>");
for (String string : list) {
out.println(DBUtils.findById(string)+"<br/>");
}
out.println("<br/><a href='"+request.getContextPath()+"/home'>繼續(xù)購物</a><br/>");
}else{
out.println("你還沒有將商品添加到購物車<br/>"
+ "<a href='"+request.getContextPath()+"/home'>返回商品列表</a><br/>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
DBUtils:存儲著商品信息
public class DBUtils {
private static Map<String,String> map = new HashMap<>();
static{
map.put("1", "人類簡史");
map.put("2", "未來簡史");
map.put("3", "世界簡史");
map.put("4", "時間簡史");
}
public static String findById(String id){
return map.get(id);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用socket實現(xiàn)網(wǎng)絡聊天室和私聊功能
這篇文章主要介紹了使用socket實現(xiàn)網(wǎng)絡聊天室和私聊功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
使用RestTemplate 調(diào)用遠程接口上傳文件方式
這篇文章主要介紹了使用RestTemplate 調(diào)用遠程接口上傳文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
攔截Druid數(shù)據(jù)源自動注入帳密解密實現(xiàn)詳解
這篇文章主要為大家介紹了攔截Druid數(shù)據(jù)源自動注入帳密解密實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

