JAVAEE中用Session簡(jiǎn)單實(shí)現(xiàn)購物車功能示例代碼
Session簡(jiǎn)單實(shí)現(xiàn)購物車功能
這個(gè)小程序主要就3個(gè)頁面,一個(gè)商品列表頁面(HomeServlet),一個(gè)是提示加入購物車頁面(AddCartTipServlet),一個(gè)是顯示購物車清單頁面(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("人類簡(jiǎn)史<a href='"+request.getContextPath()+"/addCartTip?id=1'>加入購物車</a><br/>");
out.print("未來簡(jiǎn)史<a href='"+request.getContextPath()+"/addCartTip?id=2'>加入購物車</a><br/>");
out.print("世界簡(jiǎn)史<a href='"+request.getContextPath()+"/addCartTip?id=3'>加入購物車</a><br/>");
out.print("時(shí)間簡(jiǎn)史<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:存儲(chǔ)著商品信息
public class DBUtils {
private static Map<String,String> map = new HashMap<>();
static{
map.put("1", "人類簡(jiǎn)史");
map.put("2", "未來簡(jiǎn)史");
map.put("3", "世界簡(jiǎn)史");
map.put("4", "時(shí)間簡(jiǎn)史");
}
public static String findById(String id){
return map.get(id);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaWeb購物車項(xiàng)目開發(fā)實(shí)戰(zhàn)指南
- JavaWeb后臺(tái)購物車類實(shí)現(xiàn)代碼詳解
- eclipse的web項(xiàng)目實(shí)現(xiàn)Javaweb購物車的方法
- javaweb購物車案列學(xué)習(xí)開發(fā)
- java web開發(fā)之購物車功能實(shí)現(xiàn)示例代碼
- javaweb圖書商城設(shè)計(jì)之購物車模塊(3)
- java web開發(fā)之實(shí)現(xiàn)購物車功能
- java商城項(xiàng)目實(shí)戰(zhàn)之購物車功能實(shí)現(xiàn)
- java實(shí)現(xiàn)網(wǎng)上購物車程序
- Java?web實(shí)現(xiàn)購物車案例
相關(guān)文章
Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析
這篇文章主要介紹了Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析,Ingeter是int的包裝類,int的初值為0,Ingeter的初值為null,int和integer(無論new否)比,都為true,因?yàn)闀?huì)把Integer自動(dòng)拆箱為int再去比,需要的朋友可以參考下2023-12-12
加速spring/springboot應(yīng)用啟動(dòng)速度詳解
這篇文章主要介紹了加速spring/springboot應(yīng)用啟動(dòng)速度詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Java采用setAsciiStream方法檢索數(shù)據(jù)庫指定內(nèi)容實(shí)例解析
這篇文章主要介紹了Java采用setAsciiStream方法檢索數(shù)據(jù)庫指定內(nèi)容,是比較實(shí)用的功能,需要的朋友可以參考下2014-08-08
Spring Security學(xué)習(xí)筆記(一)
這篇文章主要介紹了Spring Security的相關(guān)資料,幫助大家開始學(xué)習(xí)Spring Security框架,感興趣的朋友可以了解下2020-09-09

