All attributes of response object in JSP are introduced in detail

Time:2020-11-26

Interface: javax.servlet.http . httpservletresponse, its parent interface is servletresponse, and servletresponse now has only one httpservletresponse sub interface. The main reason is that the current J2EE Web programs mainly use HTTP protocol as the main communication protocol.

Function of response object:

1. setHeader( String name , String value )

Set the value of the specified http file. If the value already exists, the new value will override the old value
Set header information: response.setHeader (“header content”, “header parameter”);
Refresh is commonly used, for example: response.setHeader (“refresh”,”1″);
Jump after a few seconds: response.setHeader (“refresh”,”2:URL=XXX”);

2 redirection — response jump: response.sendRedirect (the “jump address”)

(redirection will lose all request parameters. The effect of redirection is exactly the same as that of re entering a new address in the address bar and pressing enter, that is, the second request is sent.)
After this statement is executed, the page address changes to the address after the jump
Common questions in interview: the difference between two kinds of jump: address bar does not change — belongs to server side jump
Jump unconditionally immediately after a jump statement is executed — the code that follows will no longer be executed
Note: if you use forward jump, you must release all resources before jump;
When using forward, the set attribute set by request can still be retained to the next page
By passing parameters
  response.sendRedirect (“jump address”):
Address bar change — belongs to client jump
Jump after all code of the page is executed
The property of the client cannot be saved. The request field cannot be changed
Passing parameters by rewriting the URL address

3.addCookie( Cookie cook )

Add a cookie object to save client user information
A cookie is a set of resources stored on the server side in the client side
Through cookie to save the password or how long no need to enter the password and other functions
How to set Cookie: public void addcookie (cookie cookie)
Cookie is set to the client through the server — response
If you want to get a cookie — request on the server side
There is a public cookie [] getcookies() method in the request object
When the request object is used to get all the cookies, jsessionid will appear, indicating the number of the unique client on the server. This value is set after the client first accesses the server
By default, cookies are only valid for the current browser. If you need to stay on the computer for a long time, you can set the save time of cookies by setting the maximum retention time of cookies: setmaxage ()

1.. addHeader( String name , String value )
Add http file header information, and the header will be sent to the client. If the same header exists, the existing header will be overwritten

2.. containsHeader( String name )
Determines whether the HTTP file header with the specified name already exists, and then returns true and false Boolean values

3.. sendError( int )
Send an error message to the client

4.. setHeader( String name , String value )
Set the value of the specified http file. If the value already exists, the new value will override the old value

Recommended Today

Regular expression sharing for checking primes

This regular expression is shown as follows: Regular expressions for checking prime numbers or not To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of […]