Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Cookie in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use Cookie in Java, the article is very detailed, has a certain reference value, interested friends must read it!

What is Cookie?

In real life, when customers shop in the supermarket for the first time, waiters usually ask if they can apply for a membership card to earn points for future discounts and other welfare activities. Membership card will record the customer's name, points, consumption records and other information, if customers want to participate in supermarket welfare activities need to provide a membership card, the waiter swiped the card in the background to find out which user is using the membership card.

Now switch roles in real-life cases.

When the user does not register the user information in the Web server, but uses the login service provided by the website, the server will tell the browser to jump to the login page to register the user information. After the login is completed, the browser initiates a login request to the server, and the server stores the user's information in the Cookie and responds to the browser's new Cookie. After the browser gets the Cookie, the browser stores it in the cache area.

When the user registers the user information with the Web server and uses the login service provided by the website, the server will judge which user is in the request by the Cookie carried in the request, and query the database with the Cookie information to complete the service needed by the user.

The necessity of Cookie

HTTP is a stateless protocol, which means that the server does not retain any data (state) between two requests. Because the Web server has to face the concurrent access of many users, in order to improve the ability of the Web server to deal with concurrent access, when designing the HTTP protocol, it is stipulated that when the Web server sends HTTP reply messages and documents, it does not save any state information of the Web browser process that issued the request, so as to reduce the load on the server, and at the same time, stateless also reduces the overhead of HTTP requests.

However, in the necessary scenarios, such as login, shopping, etc., you need to save the user's state (information), so you have to use Cookie.

How Cookie works

The first time you access the server, there is no Cookie, register a new Cookie with the server.

The second and later access to the server, there is Cookie, there is no need to register a new Cookie.

Simulated user login

Requirements Analysis:

When the user visits the homepage.jsp, it determines whether the Cookie of the username is carried in the request, and if it does not exist, it notifies the browser to jump to the login page to register the user information. When the user visits the homepae.jsp again, it will not be blocked, and the user can visit the personal home page.

Write code:

Servlet of personal home page

@ WebServlet (name = "homepageServlet", urlPatterns = "/ homepage") public class HomePageServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Cookie [] cookies = req.getCookies () For (Cookie cookie: cookies) {if (! cookie.getName (). Equals ("username")) {/ / if this is the first time for the user to visit the personal home page, notify the browser to jump to the login page to log in to resp.sendRedirect (req.getContextPath () + "/ login.jsp");}

Servlet of the user logged in

@ WebServlet (name = "loginServlet", urlPatterns = "/ login") public class LoginServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / get the http request parameter username String username = request.getParameter ("username"); / / set the type of response content response.setContentType ("text/html;charset=utf-8") / / get the cookie value Cookie [] cookies = request.getCookies (); for (Cookie cookie: cookies) {if (! cookie.getName (). Equals ("username")) {/ / the user visits Cookie userCookie = new Cookie ("username", username) for the first time; userCookie.setMaxAge (300); response.addCookie (userCookie) } response.sendRedirect (request.getContextPath () + "/ homepage.jsp");}

Open the browser and experiment:

When I visited the homepage page for the first time, I saw that the browser cache did not have a Cookie named username, and it was only after logging in that the browser cache had this Cookie.

The above is all the contents of the article "how to use Cookie in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report