JSP Programming Technology 5-Shopping Cart implementation-session object, 5-session

Source: Internet
Author: User

JSP Programming Technology 5-Shopping Cart implementation-session object, 5-session

First of all, I would like to thank you for your support for my article. I am a college student who just found my own direction. Of course, my knowledge and ability are limited and I am working hard to make up for it, when I saw my first article published on the CSDN homepage, I had read more than 100 in just a few hours. It was a great encouragement for me. I was so flattered that I was really touched, thank you very much for your support. In any case, we are all on the shoulders of giants. In today's fierce social competition, especially the rapid development of the IT industry, we need to learn many things every day to keep up with the pace of society. My idea is to show the real things I have mastered to everyone through text, I want to share more practical things with you. Of course, in my article, I just want to tell you my thoughts. If you have any good methods or suggestions, please add your friends or comment or leave a message. From today on, I have made up my mind to step into the IT field. I hope you will give me more advice. I know this process is very hard, but I will continue to work hard...

The following is an example of my own shopping cart, which uses the JSP built-in session object to implement a session between the client and the server. I also said in the previous case, after connecting the client to the server and logging out, the session object will always exist.

First, create a client page. What I do is very simple. I hope you can understand this process first. What's important is this idea. I also add comments to the relevant position in the code.

The next step is to display the shopping cart page. When the client clicks "show Shopping Cart", the server returns the information of the current Shopping Cart. The session object exists throughout the process.

Well, a complete shopping cart will be implemented.

To sum up, first we need to establish a link between the client and the server (here I have already connected, see the previous article on the connection method) to generate a session object, as long as the client and the server are continuously connected, the session always exists. Otherwise, in many cases, the server can identify who is still connected or who is offline through the session object. Then the server obtains the information submitted by the client and executes the corresponding JSP program (that is, to identify what books the client has bought). Finally, when the client clicks "show Shopping Cart, the server returns to the shopping cart page to display the books currently purchased by the client.

I hope you can understand this process. This process is very helpful for WEB development in the future. Many of our things are built on session objects. If you have any questions, please comment or leave a message.


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 .....
 
Jsp implements Shopping Cart. Can I ask if the database operation is good or the session is good?

How much burden do you have to put on the database when using data inventory? In addition, for shopping carts, this kind of thing that requires real-time operations, the database is prone to concurrent errors or crashes when there is a large access volume.

It is undeniable that the use of sessions is indeed very efficient, and sessions are for various connections, so it is easy to manage, but the use of sessions is not perfect, because sessions are valid, the length varies depending on the server settings. If the Session times out during the shopping process, everything in the shopping cart will be lost.

I don't know if you have read Dangdang's shopping cart. When you go offline and go online again, things in the shopping cart still exist, which is very convenient for users.

Therefore, if your server is strong enough, you can use a static variable to save the shopping cart of all users. For example, you can use a static Map to use IP as the Key, differentiate the shopping cart of different users, so that the user can save the content in the shopping cart when going offline.

This method has been implemented, but it does not use a large number of concurrent access tests to test its stability, but it must be feasible.

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.