Session example ----- use of simple shopping cart, session -----

Source: Internet
Author: User

Session example ----- use of simple shopping cart, session -----

Session example-use of simple Shopping Cart

As in the previous article, servlet is used for processing requests and displaying pages.

The functions are as follows:

1. display all items on the website

2. After you click "buy", you can remember the items you have selected.

3. multiple sessions are shared with one session.

4. Data Sharing after the browser disables cookies



Home page:

Package cn. itcast. shopping; import java. io. IOException; import java. io. printWriter; import java. io. serializable; import java. util. hashMap; import java. util. map; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; // The homepage that displays all products of the Website: public class IndexServlet extends HttpServlet {public void doGet (H TtpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html; charset = UTF-8"); response. setCharacterEncoding ("UTF-8"); PrintWriter out = response. getWriter (); // For the first access request. getSession (); // 1. show all items on the website. print ("This website has the following books: <br/>"); Map <String, Book> map = DB. getMap (); for (Map. entry <String, Book> entry: map. entrySet () {Book book = entry. GetValue (); String url = "/day07/servlet/BuyServlet? Id = "+ book. getId (); url = response. encodeURL (url); // get the url after rewriting. print (book. getName () + "<a href = '" + url + "'> purchase </a> <br/>") ;}} public void doPost (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {doGet (request, response) ;}} class DB {private static Map <String, Book> map = new HashMap (); static {map. put ("1", new Book ("1", "javaweb Development", "Lao Zhang"); map. put ("2", new Book ("2", "jdbc Development", "Lao Li"); map. put ("3", new Book ("3", "struts2 Development", "Lao Zhang"); map. put ("4", new Book ("4", "spring Development", "Lao Li"); map. put ("5", new Book ("5", "hibernate Development", "Lao Zhang");} public static Map getMap () {return map ;}} class Book implements Serializable {private String id; private String name; private String author; public Book () {super (); // TODO Auto-generated constructor stub} public Book (String id, String name, String author) {super (); this. id = id; this. name = name; this. author = author;} public String getId () {return id;} public void setId (String id) {this. id = 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 = author ;}}

Purchase page:

Package cn. itcast. shopping; import java. io. IOException; import java. util. arrayList; import java. util. list; import javax. servlet. servletException; import javax. servlet. http. cookie; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; // purchase public class BuyServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 1. obtain the desired book String id = request. getParameter ("id"); Book book = (Book) DB. getMap (). get (id); // 2. add the books bought by the user to the session for managing all the books in the collection HttpSession session = request. getSession (); // cookieCookie that overwrites the JSESSIONID of the server. cookie = new Cookie ("JSESSIONID", session. getId (); cookie. setMaxAge (30*60); cookie. setPath ("/day07"); response. addCookie (cookie); List list = (List) session. getAttribute ("list"); if (list = null) {list = new ArrayList (); session. setAttribute ("list", list);} list. add (book); // 3. jump to the shopping cart display list // request. getRequestDispatcher ("/servlet/ListCartServlet "). forward (request, response); // automatically carries the session ID String url = response. encodeRedirectURL ("/day07/servlet/ListCartServlet"); // rewrite response by implementing the url. sendRedirect (url);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}

Shopping Cart list display page:

Package cn. itcast. 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; // display all books bought by the user (the shopping Carlist page) public class ListCartServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html; charset = UTF-8"); response. setCharacterEncoding ("UTF-8"); PrintWriter out = response. getWriter (); out. print ("you have purchased the following products: <br/>"); // first, check whether the browser has brought sessionID in the form of a cookie, if no, check whether the URL address contains sessionIDList <Book> list = (List) request. getSession (). getAttribute ("list"); for (Book book: list) {out. print (book. getName () + "<br/>") ;}} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}



How to Use the jsp session object to write a shopping cart?

... This is simple and cannot be used simply to use session. setAttribute () to directly insert objects of items to be stored.
On a special page, use session. getAttribute () to read the stored items and display the shopping cart .........
Then, you can set a maximum of Session Timeout Cookies .....
 
Compile a simple Shopping Cart program using the session object

Ask Microsoft (China ).
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.