JavaWeb - response、session、cookie

session mechanism:

The client first request server, the server generates a session object (to save the customer information);

jsessionid sessionid corresponds with the unique identification;

Duanyou service will have a cookie, while the response to the client sends the cookie to the client, thus the client will have a cookie (JSESSIONID);

key name value
value JSESSIONID sessionId

Therefore, cookie client can be one to one session and server-side (JSESSIONID - sessionID)

When the second client / n times requested service: the server will first use the client cookie types of server-side session JSESSIONID to match sessionid, if the match is successful (cookie jsessionid and sesion sessionid), the description for this user is not the first visit without having to log in; (jsessionid-sessionid)

response: the response object

The method provides:

void addCookie (Cookie cookie); the server to the client to increase the cookie object
void sendRedirect (String LOCATION) throws IOException;: One way the page jump (redirects)
void setContetType (of the type String): Set the server response coding ( setting contentType type of server-side)

Request forwarding Redirect
Whether to change the address bar constant change
Whether to retain the first request data Retention Not Retained
The number of requests 1 2
Jump position occurs Server The second client makes the jump

Forward:
Joe Smith (Client) -> [Service Window A (server) -> Services window B]

Redirect:
Joe Smith (Client) -> Services window A (server) -> go to B
Joe Smith (Client) -> Services window B (server) -> End

Cookie

(Client, not the built-in object): Cookie is generated by the server, and then sent to the client save.
Equivalent to the role of local cache: Client (hello.mp4, zs / abc) - > server (hello.mp4; zs / abc)
role: to improve the efficiency of access to the server, but poor security.

Cookie: name=value
javax.servlet.http.Cookie
public Cookie(String name,String value)
String getName():获取name
String getValue():获取value
void setMaxAge(int expiry);最大有效期 (秒)

Server ready Cookie:
response.addCookie (Cookie the cookie)
page jump (forward, redirect)
client obtains cookie: request.getCookies ();

. a server to increase cookie: response objects; client obtains objects: request Object
b can not get one single object directly. only once to get all of the cookie

Recommendations cookie saves only alphanumeric characters, or need to encode, decode

Published 41 original articles · won praise 1 · views 560

Guess you like

Origin blog.csdn.net/qq_41620020/article/details/104968950