Javaweb Development uses cookie creation-get-persistence, automatic login, shopping record, action path _java

Source: Internet
Author: User
Tags parent directory jbpm

1, what is a cookie? Conveniently Baidu Netizen's say

Simply put, a cookie is a piece of information that the server holds on your computer so that the server can use it to identify your computer. When you browse the site, the Web server will first send a small piece of information on your computer, and the next time you visit the same site, the Web server will first see if it has left the cookie data, if so, it will be based on the contents of the cookie to judge the user, Send out specific Web content to you.

2. Where is the cookie?

3. Can I delete cookies?

4, the principle of the implementation of cookies

The first time the browser is requested, in the cookie store of the browser, there is no cookie,

For the first access without cookies, the browser adds cookies to the Web server via an HTTP request message, and the browser stores the cookie fragment in the form of a "name/value" pair (Name-value pairs) and saves it locally. On the next visit, the Web server adds the Set-cookie response header through the HTTP response message and sends the cookie message to the browser

Let's actually take a look at the cookie.

Create a cookie.jsp, set session= "false" for ease of observation

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "session=" false "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
 
 

The first time we access cookie.jsp files, in IE input

http://localhost:8080/day01/cookie.jsp

In the request header, you can see that the first visit is not carrying cookies

In the response header, passed through Set-cookie and saved in the browser local cookie Store

We visit the cook.jsp file for the second time to see if there is any change

In the request header, you can see again access, from the browser local cookie store, the request that carries the cookie

The following are the response headers:

We use an interaction diagram to understand the next cookie mechanism:

Let's look at the creation and acquisition of cookies

The code in COOKIE.JSP means: if there is no cookie in the request, create and return it, and then output the cookie key value pair (Name-value) with a cookie in the request.

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "session=" false "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
 
 

First time Access

Second visit

The above operation is we need to close the browser, in the debugging why?
Because by default, a cookie is a session-level cookie stored in the browser's kernel and deleted after the user exits the browser, you will need to use maxage, in seconds, if you want the browser to store the cookie on disk.

Let's take a look at the persistent cookie.

<%
cookie[] cookies = request.getcookies ();
if (Cookies!=null && cookies.length>0) {for
(Cookie cookie:cookies) {
out.print (cookie.getname () + ":" +cookie.getvalue ());
}
} else{
out.print ("No cookie, being created, and returning");
Cookie cookie = new Cookie ("name", "Wyf");
Cookie.setmaxage (a);
Response.addcookie (cookie);
}
%>

Cookie.setmaxage (30); Set to 30 seconds, here is not a screenshot, the name can be, the first access without cookies to create cookies, second access to output cookie key pairs, close the browser, in 30 seconds to access, still prompt cookie key value pairs, Instead of the previous hint no cookie created cookies

Automatic Login

login.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
 
 

success.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "session=" false "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

First access http://localhost:8080/day01/login.jsp Enter the name parameter value, Commit, in success.jsp, first obtain the submitted parameter name value, if not NULL, then set a cookie directly , save the parameter name value, then the page outputs the name parameter value, the second access directly input http://localhost:8080/day01/success.jsp,
Because this time we have to carry the parameter name value, we only need to get the value from the cookie value and then display the output

Show the most recent shopping record

books.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

book.jsp

<% @page import= "java.util.ArrayList"%> <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

To say a logical relationship:

The following is a list of books in books.jsp,

Javaweb
Java
Oracle
Ajax
Javascript
Android
Jbpm

I randomly choose a link (such as Javaweb), To jump to the book.jsp, the first access is not with a cookie, so will call book.jsp the following method, create a cookie, in the Book.jsp page, click Return to books.jsp to take out cooks.jsp brought over Cookievalue , and then show the list of selected books

Cookie Cook = new Cookie ("Safly" +book,book);
Response.addcookie (Cook);

Now that I have returned to books.jsp, I have chosen a book of Javaweb, we choose one (if Java) and then jump to books.jsp, now we will take 1 cookies ( Is the first time to select Javaweb visit books.jsp time, sent to me, the key value of this cookie is saflyjavaweb–javaweb, but? Select the second book Java is not with cookies (no Saflyjava–java)
And then you go into the cooks.jsp.

if (Cookiename.startswith ("Safly")) {
bookcookies.add (c);}

So also put the saflyjavaweb–javaweb into a bookcookies (store the selected list of books) and then, that Saflyjava–java will be created, in the click of Renturn, back to cookies.jsp

。。。。 3rd, 4, 5 times when the selection of books, is the same process
If you choose 5 Books in the books.jsp, how do you deal with them when you choose one of these 5 books?
C.getvalue (). Equals (book) to get the selected books, we need to remove the cookie and then add it back in and pass the COOKIES.JSP code as follows:

Tempcookie.setmaxage (0);
Response.addcookie (Tempcookie);

If you choose 5 Books in books.jsp, how do you deal with the 6th book that is not in this 5 book?
We are Tempcookie = bookcookies.get (0); Take out the first book of the 5 books, then tempcookie.setmaxage (0); Delete the cookie for the first book, and then pass the 6th book, create a cookie back to cookies.jsp

Here are some screenshots:

The action path of the cookie

cookie2.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
 
 

writercookie.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
 
 

To cookie2.jsp is the access to the writercookie.jsp parent directory cookie2.jsp

The above is a small set to introduce Javaweb development using cookies to create-get-persistence, automatic login, shopping records, the role of the path, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.