Javaweb Series VII (response domain and request)

Source: Internet
Author: User



Introduction to the 1.http protocol
Agreement: The two sides reached a common goal
HTTP protocol: In the process of request and response, to follow a certain data format
Request: Enter an address in the address bar of the browser, click Enter, this address to the access to pay the server, this process is called the request
Response: The server receives the request, returns data to the browser for display according to the request, and the process responds
Features: Requests and responses exist at the same time, there is a response before the request, HTTP response based on the TCP protocol, the default port is 80
Using the command: Telnet to impersonate the HTTP protocol
Connect to Tomcat First
http/1.0 and HTTP1.1 differences
Use HTTP1.0 to disconnect if you make a corresponding exit
Responding with HTTP1.1 does not immediately disconnect

The 3.HTTP protocol responds with three parts
http/1.1 OK
server:apache-coyote/1.1
Accept-ranges:bytes
etag:w/"578-1432798092723"
Last-modified:thu, 07:28:12 GMT
Content-type:text/html
content-length:578
Date:thu, 07:49:29 GMT

First part: Response line
Structure: HTTP version Status Code status code description
Status code: Status of the current response
Common Status Codes
200: Success OK
302: Redirect often and forwards compared,
Redirect: A request was made and a request was requested two times and responded two times.
Forward: Forward the request, request it once, respond to the forwarding in the server internal execution
304: Find local cache
404: Resource Not Found
500: Server Internal error (code is wrong)
503: Server Hangs up
Part II: Response header:
Three important response header information:
Location: Often used with 302 for redirection
Code
Redirect Using status Code 302
Response.setstatus (302);
Set header information location to complete the redirect operation
Response.setheader ("Location", "/day07/my/my.html");
Refresh: Timed jump on page
Response.getwriter (). Write ("The page will jump after 3 seconds.");
Use Refresh header information for timed jumps
Response.setheader ("Refresh", "3;url=/day07/other/other.html");
Last-modified: Often request inside if-modified-since together use Find local cache

Content-disposition: When doing file download
Part III: Response Body: Responding to content on a page
Redirect and forward differences
In a request, another request was made.
Redirect: such as looking for a small article to borrow money 100,000, Xiaowen no money, tell me little warm money, I went to find Xiao Wen, the money to get
Request two times, respond two times
Forwarding: For example, find Zhang San borrow money, but Zhang San no money, Zhang San to help me find John Doe borrow money, finally give me
Request once, respond once

3. If-modified-since and last-modified Find local cache
The process of drawing analysis execution

such as access to images, the first time access, the server-side generated access time using the header information storage last-modified
Give a response: first display the picture to the page, carry the first time generated, save the time in the browser using if-modified-since
The second time to visit the picture, the request time carries the first time sends the request, to the server, takes the request time and the server above the time to compare,
If you have the same time, return the status Code 304, let's find the local cache, and if you don't have the same time, re-request the new data
4. Cache processing in web development
Use the shortcut key Ctrl F5: indicates no cache refresh
Remove the project redeployment, or find the Tomcat work directory and delete the contents of the work
Clear the cache directly using the browser
After the requested address, add a random number http://127.0.0./day11/1.html?id= random number

HTTP operation, the server creates an object request each time it is requested, and the corresponding object is created response
Request: Enter an address in the browser address, click Enter, access to the server, this process called the request
Response:
1.response Response Object
HttpServletResponse object, Standard one response
Response Line: Status code setStatus (int sc)
Response header: Structure: Key value
SetHeader (java.lang.String name, java.lang.String value): One key corresponds to a value, the first parameter header name, and the second value key corresponds to value
Setdateheader (java.lang.String name, long date): Number of milliseconds
Setintheader (java.lang.String name, int value): type int
A key corresponds to multiple value
AddHeader (java.lang.String name, java.lang.String value): One key corresponds to more than one value. The first parameter header name, the second value key corresponds to multiple value AddHeader ("AA", "AA") AddHeader ("AA", "BB") aa:aa,bb
Adddateheader (java.lang.String name, long date)
AddHeader (java.lang.String name, java.lang.String value)

Response Body: Content displayed inside the browser
Getoutputstream () byte stream
Getwriter () Character stream
Display the contents of the server to the browser, using response

2. Case one: Redirection of response
3. Case two: Using response to achieve a timed jump on a page
Can be done directly in the page label
<meta http-equiv= "Refresh" content= "3;url=/day09/response/login.html" >
4. Case three: Disable browser caching with response implementation
* Use header information
Cache-control:no-cache
Pragma:no-cache
Expires:-1: What method to use, Setdateheader ("", Number of milliseconds): number of milliseconds, 1970-1-1 to present
These header information disables the effect of browser caching, in some versions of the browser that is low, IE6
Response.setheader ("Cache-control", "No-cache");
Response.setheader ("Pragma", "No-cache");
Response.setdateheader ("Expires",-1);
5, response to the page output Chinese problem resolution
Output content to page: There are two ways to use byte stream and character stream output
Use byte flow to page output Chinese
will appear in Chinese garbled?
Not necessarily, with the browser's default encoding
Solve garbled problems
First set the browser's encoding
To set the encoding of a byte array
Response.setheader ("Content-type", "text/html;charset=utf-8");
Response.getoutputstream (). Write ("byte stream Chinese". GetBytes ("Utf-8"));
The encoding of the browser is consistent with the encoding of the byte array
Use character flow to page output Chinese
Use character stream output Chinese will be garbled?
There's gotta be garbled.
A character stream produces a buffer, first the output is placed in the buffer, output through the buffer
The response buffer will have a default encoding of iso8859-1, but this encoding does not support Chinese
Workaround
First set the encoding of the response buffer
Response.setcharacterencoding ("Utf-8");
Set the encoding for the browser
Response.setheader ("Content-type", "text/html;charset=utf-8");
Make the response buffer code consistent with the browser encoding
6. Use of the inter-write method
First Detail question: Use character flow to page output Chinese solution, there is a shorthand way
Use a shorthand method
Response.setcontenttype ("Text/html;charset=utf-8");
Second detail question:
Byte stream and character stream are mutually exclusive, two streams cannot be used together
The third point of detail:
"Text/html;charset=utf-8", separated by semicolons, not using other symbols
Fourth detail question:
You cannot use a character stream to output a number directly to a page
Output a number directly using the character flow to the page,
To the Utf-8 code table to find the corresponding value of the number corresponding to the character output, and not directly output the number
Fifth detail question:
When redirecting operations, the shorthand method
Request for 2.http Protocol
Get and post submission forms, respectively, to view the requested data format
Get commit: The requested data is carried in the address bar
Post submission: Does not carry data in the address bar
HTTP requests:

The HTTP protocol request consists of three parts:
First part: Request line
The request line contains: Request method, request address, HTTP version
Part II: request Header
Like the Key-value form, a key can correspond to a value, or it can correspond to more than one value
Three important request headers
Referer: Anti-theft chain, get the address of the current request

Create two folders my and other
Create two pages respectively my.html other.html
Only files inside my folder can be accessed
Only files inside my folder can access the servlet
User-agent: Gets the information for the currently requested browser
If-modified-since: Often used in conjunction with response inside Last-modified for querying the local cache
Part III: Request body
Data submitted via post User=lisi
There are many ways to submit forms, two commonly used get and post

9. Request Object
HttpServletRequest, representing the request object, the parent interface is ServletRequest
10. Get the information to the client
Get form Submission Method
GetMethod ()
Get to request address
Getrequesturl ()
Gets the project name to the request
Getcontextpath ()
Gets the IP address of the requested client
GETREMOTEADDR ()

11. Get the header information to the request
Use method GetHeader ("header information name");
Important Header information
Referer user-agent if-modified-since
Request.getheader ("Referer");
Request.getheader ("User-agent");

12. Get data submitted through the form
Method
GetParameter (java.lang.String name)
-Gets the data method of the form submission, the value of the parameter entry name
-String username = request.getparameter ("username");

    getparametervalues (java.lang.String name), return string[]
 -For a check box, get multiple submitted data, parameter entry name value
 -//Get all values in check box
    string[] loves = Request.getparametervalues ("Love");
   //arrays
    System.out.println (arrays.tostring (loves));

Getparametermap (): Return map<java.lang.string,java.lang.string[]>
-Gets the value of the name of the form entry, and the value entered
-//Gets the value of the input name, and the value entered
map<string,string[]> map = Request.getparametermap ();
There are two ways to traverse a map
Get all the keys
set<string> keys = Map.keyset ();
Traversal set, get value, set traversal in two ways
for (String Key:keys) {
Get value based on key
String[] values = Map.get (key);
System.out.println (key+ "::" +arrays.tostring (values));
}
Getparameternames (): enumeration<java.lang.string>
-Gets the value of the name of all form entries

In the form input item, input Chinese, appear Chinese garbled

13. Get form submit Chinese data garbled problem solve
Form submission method, common get and post
Solve garbled problem when using post to submit Chinese data (set encoding of request buffer)
Submit data using post and put it in the request body
Post submission Chinese data has garbled problem
Request gets data submitted via post and also puts the data in the request buffer
Response buffer default encoding is iso8859-1, and the request buffer inside the default encoding is also iso8859-1, does not support Chinese
/WORKAROUND: Set the encoding of the request buffer
Request.setcharacterencoding ("Utf-8");
Get submitted data available in Chinese
String username = request.getparameter ("username");
System.out.println ("Username::" +username);

Solving garbled problems when using get to submit Chinese data
Get submit data is placed in the browser address bar, the Chinese data URL encoding.
Three ways to solve the problem:
Configure in Tomcat
Locate the Tomcat directory conf below Server.xml, in the configuration port place, and finally add uriencoding= "Utf-8"
First encode the code to decode the servlet.
Encoding transformation using string construction method (Key Mastery)
Username = new String (username.getbytes ("iso8859-1"), "Utf-8");
Code: String username = request.getparameter ("username");
Username = new String (username.getbytes ("iso8859-1"), "Utf-8");
14. Request Domain
Domain: Within a certain range, stored value and value
ServletContext domain: Entire Web project
Stored value: SetAttribute
Value: GetAttribute
Request is also a domain object
Scope: one request
Stored value: SetAttribute
Value: GetAttribute
Request domain objects are used frequently and forwarded together
Request.getrequestdispatcher ("Forwarded path without project name"). Forward ();
Create a Demo1, in this servlet, set a value msg itcast to the request domain and forward to Demo2
In Demo2, gets the value set through DEMO1 to the request domain
15. Forwarding and redirection differences
Redirect:
Request two times, redirect address with project name
Forward:
Request once, the forwarding address does not need to have the project name
Redirect: From one Web site to another
Forwarding: Data is required inside the request







Javaweb Series VII (response domain and request)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.