توضح هذه المقالة طريقة تنفيذ متجر التسوق المستند إلى الجلسة في تطوير Java Web. شاركها مع الجميع للرجوع إليها، التفاصيل كالتالي:
حزمة cn.com.shopping;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http .HttpServletRequest;استيراد javax.servlet.http.HttpServletResponse;استيراد javax.servlet.http.HttpSession;// أكمل الشراء public class BuyServlet Extends HttpServlet { Private static Final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse Response) throws ServletException, IOException { String id=request.getParameter(" معرف ")؛ كتاب book=(Book)Db.getAll().get(id); // أضف حل الجلسة عند إيقاف تشغيل ملفات تعريف الارتباط // الحل عند حظر الجلسة HttpSession session=request.getSession(false); جميع الكتب المحفوظة من الجلسة (عربة التسوق) List list=(List)session.getAttribute("list"); ArrayList(); session.setAttribute("list", list); } list.add(book); String url=response.encodeRedirectURL("/Session/SessionCountDemo"); طلب HttpServletRequest، استجابة HttpServletResponse) يطرح ServletException، IOException { doGet(request,response }}package cn.com.shopping;import java.io.IOException;import java.io.PrintWriter;import java.util.LinkedHashMap;import java.util.Map;import javax.servlet.ServletException ;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;// عرض الكتاب public class ListBookServlet Extends HttpServlet { Private static Final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request ، استجابة HttpServletResponse) يلقي ServletException, IOException { Response.setCharacterEncoding("UTF-8"); Response.setContentType("text/html;charset=UTF-8"); PrintWriter out=response.getWriter(); out.print("يحتوي متجرنا على المنتجات التالية:<br/>"); Map<String,Book> Map=Db.getAll(); for(Map.Entry<String, Book>entry:map.entrySet()) { Book book=entry.getValue(); String url=response.encodeURL("/Session/BuyServlet?id ="+book.getId()); out.print(book.getName()+"<a href='"+url+"' target='_blank' >شراء</a><br/>"); } } protected void doPost(HttpServletRequest request, HttpServletResponse Response) throws ServletException, IOException { doGet(request,response); }}//Db كفئة قاعدة بيانات Db{ خريطة ثابتة خاصة <String,Book> Map=new LinkedHashMap(); static { Map.put("1", new Book("1"، "تطوير الويب Java"، "WY"، "كتاب جيد"))؛ Map.put("2"، كتاب جديد("2"، "تطوير الويب"، "zt"، "عام" )); Map.put("3"، كتاب جديد("3"، البرمجة"، "df"، "كتاب أفضل"))؛ Book("4"، "مكونات الكمبيوتر"، "as"، "كتب جيدة بشكل عام"))؛ Map.put("5"، كتاب جديد ("5"، "مبادئ التجميع"، "ty"، "جيد جدًا". book")); Map.put("6", new Book("6"، "صيانة الشبكة"، "hj"، "كتاب جيد جدًا"))؛ } public static Map getAll() { return Map; }} // فئة الكتاب Book{ معرف السلسلة الخاصة؛ مؤلف سلسلة خاصة؛ وصف السلسلة الخاصة؛ public Book() { super(); this.name = name; this.author = Author; this.description = description } public String getId() { return id } public void setId(String id) { this.id = } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return Author } public void setAuthor(String Author) { this.author } public String; getDescription() { return description } public void setDescription(String description) { this.description = description }}package cn.com.shopping;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet. http.HttpServletResponse;import javax.servlet.http.HttpSession; public class SessionCountDemo Extends HttpServlet { Private static Final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse Response) throws ServletException, IOException {response.setCharacterEncoding("UTF-8"); Response.setContentType("text/html;charset=UTF-8"); PrintWriter out=response.getWriter(); HttpSession session=request.getSession(); if(session==null) { out.write("لديك لا تشتري أي منتج! "); return; } out.write("لقد اشتريت المنتجات التالية:"); List<Book> list=(List) session.getAttribute("list"); for(Book book:list) { out.write ( book.getName()); } } protected void doPost(HttpServletRequest request, HttpServletResponse Response) throws ServletException, IOException { doGet(request,response }});آمل أن تكون هذه المقالة مفيدة للجميع في برمجة الويب Java.