JSP Learning Note Seven Cookie

Source: Internet
Author: User
Tags session id

first of all, please mention the stateless nature of the HTTP protocol , It means that the server does not remember the client that has sent the request to it. Each time a request is received, it will be considered a new client. (That is, the server does not remember the client that sent the request to him).

so this time, we need to use cookie to save the user's status.

A cookie refers to a series of textual information that a Web server holds on the client. For example: To determine whether registered users have landed on the website, online shopping cart processing and so on. So it consumes the client's storage space.

S Ession is the server to maintain the state, is the server side of the client open storage space. Therefore, the server-side storage space is consumed.

1. There are two major mechanisms for saving the user's state: Cookies and session.

A, Cookie Effect:
1. Tracking for a specific object
2. Save user's web browsing history and habits
3. Simplified Login
Insufficient is Security Risk: Easy disclosure of user information

b, the role of the session

when the session is created, the server generates a unique session ID for the session, and the session ID is used to regain the session that was created in the subsequent request, and after the session is created, You can call the session related methods to add content to the session, which will only be saved in the server, sent to the client only session ID, when the client sends the request again, the session ID will be taken, once the server accepts the request, it will find the corresponding session based on the session ID, which is used again. This is a process in which the user's state is maintained.

2. common methods of cookies

CreateCookie object:Cookie Newcookie = new Cookie (String key,object value);
WriteCookie object:Response.addcookie (Newcookie);
ReadCookie object:cookie[] cookies = request.getcookies ();
Setof the Cookie objectexpiry date(seconds):setmaxage ()
Createafter the cookieAssign Value:SetValue (String Value)
Getof Cookiesname:GetName ()
Getof Cookiesvalue:GetValue ()
Getof Cookiesexpiry date(seconds):getmaxage ()

3. a JSP instance of a cookie is given below.


Login.jsp using a checkbox dologin.jsp create cookie cookie to the server cookie instance, setting cookie

There will be hyperlinks in dologin.jsp for connecting users.jsp. the users.jsp interface Displays the user name and password that you just entered in the login screen. the checkbox is checked to see if a Cookie needs to be created . The logic of the program is that the checkbox is selected to create a Cookie.

The first is the login interface login.jsp:

    <%//Gets the value of the element in the Cookie instance object cookie[] Cookie=request.getcookies ();    String username= "";    String password= ""; if (cookie!=null && cookie.length>0) {for (cookie C:cookie) {if (C.getname (). Equals ("username")) {Usernam    E=c.getvalue ();    } if (C.getname (). Equals ("password")) {password=c.getvalue (); }}}%> <body> 
<span style= "font-family:arial, Helvetica, Sans-serif;" The >dologin.jsp code is as follows:</span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "HTML" style= "Color:rgb (20, 25, 30);" ><span style= "font-family:arial, Helvetica, Sans-serif;" > <body></span>
    


users.jsp

The results show:




results when the checkbox is not selected:




1, JSP commonly used to have page, include, taglib directive these three kinds of instructions

Page : At the top of the screen, a page can contain multiple page directives .
Include: Embed an external file into the JSP and parse the JSP statements in the page.
Taglib: Use tag library, customize new tags, start custom behavior in JSP.

A, include Directives
Syntax <% include file= "Address"%>.
Case: Displays the current time of the page. The steps are as follows:

(1) write a date.jsp of the method that only outputs the time.

(2) used to display the page, need to includes <% include file= "date.jsp"%> this sentence.

Instance code:

date.jsp

<%//creates an instance of a date    d=new date (); SimpleDateFormat sdf=new SimpleDateFormat ("yyyy year mm DD day"); String S=sdf.format (d); Out.println (s);%>

inculde_command.jsp

<body>    

The results are shown below:


5, Include action (action tag)
<jsp:include page= "URL" flush= "True/false"/>
page : pages to include
Flush : whether the contained pages are read from the buffer

code example:

include_action.jsp

<body>    


6. Comparison of include directives and actions:

/tr>

 

include directives

Jsp:in Clude action

Syntax format

<%@ include file= "" %>

<jsp:include page= "";

Time of action

Page transition

Request period

contains

file

page

Convert to servlet

Main Page and include Page converted to a servlet

Main page and include convert to standalone servlet

Compile time

Slower-resources must be parsed

Faster

Execution time

Slightly faster

Slower-each resource must be parsed


JSP Learning Note Seven Cookie

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.