java day40 [session technology: Cookie, technical sessions: Session, JSP: learning portal]

Chapter One   session technology

1. Session: a session comprises multiple requests and responses.
* A session: the first browser sends a request to the server resources, session establishment, until one party off until the
2. Function: between multiple requests within the scope of a session, data sharing
3. Method:
1. Client technical session: Cookie
2. technical server-side session: session

The second chapter   Cookie

1. Concept: client session technology to save data to the client

2. Getting Started:
* Use these steps:
1. Create a Cookie object, bind data
* new new Cookie (String name, String value)
2. Send Cookie object
* response.addCookie (Cookie the cookie)
3. get Cookie, get data
* cookie [] request.getCookies ()

3. The principle
* set-cookie header based on a response and achieve cookie request header

4. cookie details
1. Can you send more than one cookie?
* Can
* Cookie can create multiple objects, use multiple calls addCookie response method to send cookie.
2. cookie stored in the browser for how long?
1. Under default, when the browser is closed, Cookie data is destroyed
2. persistent storage:
* setMaxAge (int seconds The)
1. positive: Cookie data is written to the hard disk file. Persistent storage. And specify the cookie survival time after time, cookie files automatically lapse
2. Negative: Default
3. Zero: delete cookie information
3. A cookie can not save Chinese?
* Before tomcat 8 cookie can not be stored directly in the Chinese data.
* Chinese data needs to be transcoded --- generally use URL encoding (% E3)
* After tomcat 8, cookie support Chinese data. Special characters are not supported or recommended storage URL encoding, decoding URL parsing
4. cookie sharing?
1. Assuming a tomcat server, deploy a number of web projects, the cookie can not share in these web project?
* Cookie by default can not be shared

* SetPath (String path): set a cookie acquisition range. By default, set the current virtual directory
* If you want to share, you can set the path to "/"

2. between different tomcat server cookie sharing?
* SetDomain (String path): If you set an identical domain name, the cookie can be shared among multiple servers
* setDomain ( "baidu.com.") , Then tieba.baidu.com and news.baidu.com in the cookie can be shared

5. Cookie features and functions
1. cookie data stored in the client browser
2. The browser limited (4KB) for a single size of a cookie, and there are limits on the total amount of a cookie in the same domain (20)

* Role:
1. the cookie used to store a small amount of generally less sensitive data
2. Without login, complete server for the client's identity identification

6. Case: Remember the last access time
1. Requirements:
1. Access a Servlet, if it is the first visit, the prompt: Hello, welcome to your first visit.
2. If it is not the first visit, the prompt: Welcome back, your last visit time: displays the time string

2. Analysis:
1. Cookie can be accomplished using
2. Servlet in the server to determine whether there is a named lastTime of the cookie
1 are: not the first visit to
1. Response data: Welcome back, your last visit time : June 10, 2018 11:50:20
2. write back Cookie: lastTime = June 10, 2018 11:50:01
2. no: is the first visit to
1. The response data: hello, welcome first visit
2. write back Cookie: lastTime = 2018 Nian 6 Yue 10 Ri 11:50:01

3. code for:
Package cn.itcast.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet ( "/ cookietest")
public class CookieTest the extends the HttpServlet {
protected void the doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
data formats and encoding // Setup Response message body
response.setContentType ( "text / html; . 8 = UTF-charset ");

.. 1 // get all cookies
cookies [] = cookies request.getCookies ();
Boolean = In Flag to false; // no cookie is lastTime
// 2 cookie traverse the array.
IF (= null cookies! cookies.length &&> 0) {
for (cookie cookies: cookies) {
.. 3 // Get the name of the cookie
String name = the Cookie.getName ();
// name. 4 determines whether:. lastTime
iF ( "lastTime" .equals (name)) {
// have the Cookie, is not the first access

flag = true;// There lastTime of the cookie

// Cookie set the value
// Get the string current time, reset the value of Cookie, re-send the cookie
a Date DATE = new new a Date ();
SimpleDateFormat SDF = new new SimpleDateFormat ( "yyyy Year MM month dd day HH: mm: SS");
String str_date = sdf.format (DATE);
System.out.println ( "before encoding:" + str_date);
// the URL encoding
str_date = URLEncoder.encode (str_date, "utf -8");
after System.out.println ( "encoding : "+ str_date);
cookie.setValue (str_date);
// set a cookie survival time
cookie.setMaxAge (60 * 60 * 24 * 30); // month
response.addCookie (cookie);

Response data //
// Get the Cookie value, time
String value = cookie.getValue ();
System.out.println ( "before decoding:" + value);
// decode the URL:
value = URLDecoder.decode (value, " 8-UTF ");
System.out.println (" decoded: "+ value);
. response.getWriter () the Write (" <h1> Welcome back, your last visit time: "+ value +" </ h1 > ");

BREAK;

}
}
}

IF (Cookies == null || || In Flag == cookies.length to false == 0) {
// No, first access

// Set Cookie value of
// Get the current time string, reset the Cookie value resend the cookie
a date dATE = new new a date ();
SimpleDateFormat SDF = new new SimpleDateFormat ( "yyyy Year mM month dd day HH: mm: SS");
String str_date = sdf.format (dATE);
System.out.println ( " before encoding: "+ str_date);
// the URL encoding
str_date = the URLEncoder.encode (str_date," UTF-. 8 ");
System.out.println (" encoded: "+ str_date);

cookies Cookie = new new cookies (" lastTime ", str_date);
// set a cookie survival time
cookie.setMaxAge (60 * 60 * 24 * 30); // month
response.addCookie (cookie);

. response.getWriter () the Write (" <h1> Hello welcome to your first visit </ h1> ");
}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

 

The third chapter   JSP: learning portal

1. Concept:
* the Java Server Pages and the: java server pages
* can be understood as: a special page, which can either specify the definition of html tags, they can be defined java code
* to simplify the writing! ! !

2. Principle
* The JSP is essentially a Servlet

3. JSP script: JSP define how Java code
1. <Code%%>: java codes defined in the service process. What service method can be defined, what the script can be defined.
2. <! Code%%>: java code is defined, the position of the members jsp java class after conversion.
3. <% = Code%>: java code is defined, is output to the page. What output statement can be defined, what the script can be defined.

4. JSP built-in objects:
* do not need to get in and create a jsp page, the object can be used directly
* jsp a total of nine built-in objects.
* Today study 3:
* Request
* the Response
* OUT: character output stream object. You can output the data on the page. And response.getWriter () similar
* response.getWriter () and out.write () the difference:
* Before the real tomcat server responds to the client, will go first response data buffer, the buffer data to find out.
* Response.getWriter () output data always out.write before ()

5. Case: Reconstruction Cookie Case

Chapter IV   Session: main course

1. Concept: server-side session techniques, sharing data among multiple requests in one session, the data stored in the server object. HttpSession
2. Quick Start:
1. Get HttpSession object:
HttpSession request.getSession the session = ();
2. Use the HttpSession object:
Object getAttribute (String name)
void setAttribute (String name, Object value)
void removeAttribute (String name)

3. Principle
achieve * Session is dependent on Cookie's.

4. Details:
1. When the client is closed, the server does not shut down, twice obtaining session whether the same?
* by default. No.
* If you need the same, you can create a Cookie, the key is JSESSIONID, set the maximum survival time for the cookie persistence save.
C = new new cookie cookies ( "the JSESSIONID", session.getId ());
c.setMaxAge (60 * 60);
response.addCookie (C);

2. The client is not shut down, shut down the server, the session is to get twice the same right?
* Not the same, but to ensure that data is not lost. tomcat automatically accomplish the following
* passivation session:
* off normal before the server, the session object serialization on the hard disk
* session activation:
* After starting the server, the file conversion for the session to session object in memory.

When 3. session is destroyed?
1. shut down server
2. session object calls invalidate ().
3. session default expiration time of 30 minutes
are selectively configured to modify
<the session-config>
<-the session timeout> 30 </-the session timeout>
</ the session-config>

5. session characteristics of
data 1. session for storing a plurality of times within a session request, the presence server side
data store 2. session may be of any type, any size

* The difference between the session and Cookie:
1. session data stored on the server side, Cookie client
no size limitation 2. session data, Cookie there
3. session data security, Cookie respect unsafe

Chapter Five   Case: verification code

1. Case Requirements:
1. Access the login page with the code of the login.jsp
2. user to enter a user name, password and a verification code.
* If the username and password entered incorrectly, skip the login page Note: user name or password is incorrect
* If the code is entered incorrectly, jump login page Tip: Codes error
* If all input is correct, then jump to the home page success.jsp, displayed: user name, you are welcome

2. Analysis:

Guess you like

Origin www.cnblogs.com/xuweng/p/11272020.html