Request forwarding, redirection

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
// page: specified property;
// language: jsp page scripting language used;
// import: import the class;
// pageEncoding: jsp file their own coding;
//contentType:浏览器解析jsp的编码;
  
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
 
 
 
jsp built-in objects:
1.out: output content to the client;
2.pageContext:
3.request: request object, storing request information sent by the client to the server;
Common methods:
String getParameter (String name): The key request field name, field value return value;
String [] getParameterValues ​​(String name): The field names of the key request, the plurality of field values ​​return value (checkbox)
void setCharacterEncoding ( "encoding format"): coding setting request;
getRequestDispatcher ( "b.jsp") forward (request, response): forwarding the request to jump to a jump page mode b;.
ServerletContext getServerContext (): Object through ServerletContext projects;
4.response: response object;
The method provides:
1.void addCookie (Cookie cookis); increase server cookie object to the client;
2.void sebdRedirect (String locstion) throws IOException; one way page jump (redirects);
3.void setContentType (String type): set coding server response (provided the server type ContentType)
  Request forwarding Redirect
Address bar is changed Unchanged (check.jsp) Change (success.jsp)
Whether to retain data when the first request Retention Not Retained
The number of requests 1 2
Forward:
 Zhang (Client) - Services (A) - Services (B)
 
Redirect:
Joe Smith (Client) - Services (A) - to find B
Joe Smith (Client) - Services (B) - End
5.session:
  1.    Cookie (client, not the built-in object): Cooki is generated by the server, and then saved to the client;
Equivalent to the local cache, the client (hello.mp4, zs / abc) - server (hello.mp4; zs / abc)
Role: to improve the efficiency of access to the server, but poor security line.
Cookie:name=value
javax.servlet.http.Cookie
public Coookie(String nsme,String value)
String getName()
String getValue()
void setMaxAge (int expiry); the maximum lifetime (sec)
 
Server ready Cookie:
response.addCookie(Cookie cookie)
Page jump (forward, redirect)
Customer acquisition cookie: request.getCookies ();
 
Recommendations cookie saves only alphanumeric characters, or need to encode, decode
 
Use Cookie achieve Remember username feature
 
2.session: Session
 
. A visit the website: Start - closed
b Shopping: Browse - Payment - Exit
c E-mail: browsing, writing e-mail, quit
                   start end
 
 
session mechanism:
When the client first request to the server, the server generates a session object (for holding the information of the client);
And each will have a unique session object of the sessionID (used to distinguish it from other session);
Server will produce a cookie, and the cookie name = JESSIONID, value = value of sessionID server;
Then the server will respond to the client at the same time, the cookie is sent to the client, thus the client will have a cookie (JESSIONID);
Therefore, cookie client can be one to one session and server-side (JESSIONID-sessionID)
 
To serve the end of the session matching service will end with the first client in the cookie JESSIONID sessionid, if the match is successful (cookie jessionid and session sessionid), describes the user is not the first visit;: client requests a service in the second end
 
 
session method:
String getId():获取sessionid
boolean isNew (): determine whether a new user (first visit)
void invalidate (): failure of the session (log out, log off)
 
setAttribute();
getAttribute();
 
void setMaxInactiveInterval (sec): Set the maximum effective inactive time;
int getMaxInactiveInterval (s): Gets the maximum effective period of inactivity;
 
Cookie:
Not a built-in objects, to be used must be new;
However, the server will automatically generate a (new server is automatically a cookie) name = JESSIONID the cookie, and returned to the client
 
cookie and session difference:
  session cookie
Saved location Server Clients of
safety Safer Less secure
Save content Object String
JSP0 large built-in objects:
pageContext JSP page content
REQUEST request objects
response response objects
session session object
application global object
config configuration object (server configuration information)
out output target
The current page JSP page object (the equivalent of java in this)
exception exception object
 
Four kinds of objects range:
PageContext JSP page after the current page valid page jump invalid
REQUEST request same Object Request Valid Invalid other request (after forwarding valid; redirecting invalid)
session session object in the same session no matter how effective jump, are valid; after turning off / switch browsers invalid; from the sign - all valid between exit
application globally effective global object (the entire project effectively) are valid (effective switching browsers of any course) throughout the project operation; shut down the service, invalid other items;
Scope of the above four objects, () by the setAttribute assignment, () value by the getAttribute;
Scope of the above four objects, use the smallest possible range. Because the larger range, resulting in greater loss of performance;
 
There are more than four objects method:
Object getAttribute (String name): attribute name or attribute value in accordance with;
void setAttribute (String name, Object obj): Set the attribute value (add, edit)
        setAttribute ( "a", "b"); if there is a previous object, the object is a new one;
           If a previously already exists, then the value of b was changed to a
void removeAttribute (String name): according to the attribute name, delete objects
6.application: Global Object
String getContextPath () virtual path;
String getRealPath (String name): absolute path (absolute path virtual path relative)
7.config:
8.page:
9.exception:
 
 

Guess you like

Origin www.cnblogs.com/zxq6/p/11815967.html