Forward and redirect

1. Forward 
a web component (servlet/jsp) to hand over unfinished processing to another web component to continue.
For example, a servlet forwards the processing results to a jsp for display.
2. How to forward
step1 and bind data to the request.
request.setAttribute(String name, Object obj);
Note:
The binding value is obtained according to the binding name.
Object request.getAttribute(String name);
step2, get the forwarder
RequestDispatcher rd = request.getRequestDispatcher(String uri);
uri: forwarding address.
step3, forwarding
rd.forward(request, response);
3. Features
of forwarding a. After forwarding, the address in the browser address bar remains unchanged.
b. There are restrictions on forwarding addresses (must be the same application).

The include directive
tells the container to insert the contents of the file specified by the file attribute
into the location of the directive when converting the jsp file into a servlet class.
For example: <%@include file="header.jsp"%>

Handle exceptions generated when the servlet is running
(1) Programmatically process
step1, catch exception
step2, forward for
example:
request.setAttribute("error_msg","The system is busy, try again later");
request.getRequestDispatcher("error.jsp" ).forward(request,response);
(2) Declarative processing (that is, handed over to the container for processing) 
step1, catch the exception, and then throw the exception to the container.
step2, configure the exception handling page
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/error.jsp</location>
</error-page>
declarative exceptions are often used Handle system exceptions (you cannot reply after the exception occurs, for example, the database service stops). After such an exception occurs, prompt the user to try again later.
Programmatic exceptions (forwarding methods) are more flexible and can be used to handle application exceptions (eg, user enters wrong password, etc.).


Comparison of forwarding and redirection
(1) Each web component involved in forwarding can share the same request object; redirection cannot.
Note:
  After the container receives the request, it will create the request object and the response object. When the corresponding sending is completed, the container will immediately delete the request object and the response object.
That is, the lifetime of the request object and the response object is a request and response period. The redirect is two requests.
(2) After redirection, the address in the browser address bar will change, but the forwarding will not change.
(3) The redirection address is arbitrary, and the forwarding address must be the same application.
(4) Forwarding is one thing unfinished, while redirection is one thing completed. 

Path problem
link address, form submission address, redirection, forwarding are four cases, if fill in the path.
<a href=""></a>
<form action="">
response.sendRedirect("");
request.getRequestDispatcher("");
(1) A path whose relative path
does not start with "/"
(2) Absolute paths
start with "/".
(3) How to write the absolute path
link address, form submission address, and redirection from the application name;
forwarding from the application name. 
Note:
To use String request.getContextPath(); to get the actual deployment application name.


The difference between forwarding and redirection
(1) Whether it is possible to share request
forwarding, but not redirection.
Note:
After the container receives the request, it will create the request and response, and
when the response is sent, the two objects will be destroyed.
(2) Whether the address in the browser address bar changes or not
, the forwarding does not change, but the redirection changes.
(3) The destination has unlimited
forwarding restrictions (the same application), while redirection is unlimited.

 

State management
(1) What is state management
The multiple interactions between the browser and the web server are handled as a whole, and the data (ie, states) involved in the multiple interactions are saved.
(2) cookie
1) What is a cookie?
The server temporarily saves a small amount of data on the browser side.
2) Working principle
When the browser accesses the server, the server can send a small amount of data to the browser in the form of a set-cookie message header, and the browser will temporarily save the data.
When the browser accesses the server again, it will send the previously saved data to the server in the form of a cookie header.
3) Add cookie
Cookie c = new Cookie(String name, String value);
response.addCookie(c); 
4) Read cookie
Cookie[] cookies = request.getCookies();
String cookie.getName();
String cookie. getValue();
5) Coding problem
cookie can only save legal ASCII characters, for Chinese, it needs to be converted into the corresponding ASCII
character representation.
URLEncoder.encode
URLDecoder.decode
Note:
When adding a cookie, it is recommended to uniformly encode it.
6) Generation time problem
By default, the browser keeps the cookie. As long as the browser is not closed, the cookie will exist forever. When the browser is closed, the cookie will be destroyed.
setMaxAge(int seconds)
Note: The
unit is seconds.
seconds > 0 The browser will save the cookie on the hard drive. If the specified time is exceeded, the cookie will be deleted.
seconds < 0 Default value
seconds = 0 Delete cookies
For example, to delete a cookie named "cart",
Cookie c = new Cookie("cart","");
c.setMaxAge(0);
respongse.addCookie(c );
7) Path problem
a. What is the path problem of cookies? When the
browser sends a request to an address on the server, it will compare whether the address meets the requirements of the cookie path, and only the cookie that meets the conditions will be sent out. .
b. The default path
of the cookie is equal to the path of the component (servlet/jsp) that added the cookie.
For example, /web07/biz01/addCookie.jsp adds a cookie named
"cart", then the cookie has a default path ("/web07/biz01/").
c. The matching rule
request address must be equal to the path of the cookie, or its sub-path.
/web07/findCookie1.jsp no
/web07/biz01/findCookie2.jsp yes
/web07/biz01/sub/findCookie3.jsp yes 
e.cookie can only hold strings.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325257065&siteId=291194637