HTTP protocol Basics and Web service redirection, jump, and request forwarding

Source: Internet
Author: User
Tags browser cache apache tomcat


In Javaweb, HttpServletRequest and HttpServletResponse are almost the necessary parameters to handle various requests and operations, compared to the original servletrequest/servletresponse. They conform to the HTTP protocol, so start with the HTTP protocol, review some basic content, and then summarize the common knowledge points according to the protocol.

First of all, to introduce the DOS environment in a simple HTTP delivery text, the method of receiving messages. Run input cmd, open a command prompt to enter Telnet 127.0.0.1 8080, enter after the input interface, but at this time the characters entered can not be displayed, press and hold ' CTRL +] ', appear Microsoft telnet> and then press ENTER, after entering the interface, You can see that the characters you have entered can be displayed.

----------------------------------------------------------------------------------------------->

HTTP protocol Basic content:

Version: 1.0 One link, one request, 1.1 links, multiple requests for different resources.

----------------------------------------------------------------------------------------------->

Request is divided into request line, request header and request body, according to the different request method, can be divided into seven kinds, commonly used for GET, POST request. The request line is located on the first line of the entire message, including the Request way resource path protocol, such as: get/demo/1.html?username=jack&password=1234 http/1.1. The request content varies greatly depending on how the request is made, and if it is a GET request, it is placed in the form of a URL stitching behind the resource path in the request line, using "?" Split with "&" such as: http://localhost:8080/demo/1.html?username=jack&password=1234; if it is a POST request, the request will be placed separately in the request body. Because the URL is limited in length, there is a limit to what the GET request appends, typically 1024 bytes (1k), but there is no limit to the size of the request body of the post.

Common content of the request header:

accept:text/html,image/*--Support data types
Accept-charset:iso-8859-1--Character Set
Accept-encoding:gzip--Compression format supported
ACCEPT-LANGUAGE:ZH-CN--language environment
HOST:WWW.ITHEIMA.COM:80--Access the host
If-modified-since:tue, Jul 18:23:51 GMT--Cache time
referer:http://www.itcast.com/index.jsp – from which page, anti-theft chain – if not accessed by hyperlink 2.html returns null
user-agent:mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) User Data
Cookie--Means cookie technology
Connection:close/keep-alive--Link status
Date:tue, Jul 18:23:51 GMT--time

In addition, MIME represents Multipurpose Internet Mail Extensions (Multipurpose Internet Mail Extension), which includes the following common content:

Format: Large type/small type;
Large Type: 7 categories, representing all resources of the Internet.
Text: Used to standardize the representation of textual information, text messages can be a variety of character sets and or multiple formats;
Multipart: Multiple parts used to connect the body of a message form a message, which can be different types of data;
Application: Used to transfer application data or binary data;
Message: Used to package an e-mail message;
Image: Used to transfer static picture data;
Audio: Used to transmit sound or sound data;
Video: Used to transmit dynamic image data, which can be a format for audio editing together.
For example:
Text/css CSS Files
text/html HTML file
Text/javascript js File
image/* All pictures
image/jpeg jpg Images

----------------------------------------------------------------------------------------------->

The requested response is also divided into response lines, response headers, and response bodies. The format is: "Protocol/version status code status code corresponds to description information."

Status code:

1XX: The server responds to the browser and the data is being sent. Rarely used in general.
2XX: Server Response browser has ended normally. Common: 200 indicates normal.
3XX: The server responds to the browser, the request has not been completed and requires further action by the browser to complete the request.
Common status Code:
302 (307): With the response header location combined to complete the page re-jump.
304: Page Read cache
4XX: Server response browser, browser operation error.
Common: 404 Page Not found. (The General Request page cannot be found to indicate that the user URL was incorrectly written)
5XX: Server response Browser, server exception

Response header:

location:http://www.it315.org/index.jsp--Jump direction
Server:apache Tomcat-Server model
Content-encoding:gzip--Data compression
CONTENT-LENGTH:80--Data length
CONTENT-LANGUAGE:ZH-CN--language environment
content-type:text/html; charset=gb2312--Data type
Last-modified:tue, Jul 18:23:51 GMT-Last modified
Refresh:1;url=http://www.it315.org--timed refresh
Content-disposition:attachment; Filename=aaa.zip--Download
SET-COOKIE:SS=Q0=5LB_NQ; Path=/search
Expires:-1--cache
Cache-control:no-cache--Cache
Pragma:no-cache--Cache
Connection:close/keep-alive--Connection
Date:tue, Jul 18:23:51 GMT--time

----------------------------------------------------------------------------------------------->

The above are some of the basic content of HTTP, in Web services, the two most commonly used parameters HttpServletRequest and HttpServletResponse need to understand the agreement on the basis of a profound understanding of the use of flexible. Here we mainly discuss the implementation of request forwarding, clearing the redirect, page jump and page refresh process.

Redirect, the browser's request arrives at the server, the server response has two necessary elements, one is the status code: 302, one is the redirected address, after the browser receives the message, according to the two information to resend the request to the designated location, and page refresh refers to the server when sending the message, The header information contains "refresh", so that the browser can follow the delay to refresh this page or jump to the specified URL, request forwarding, is the browser request server, the server internal jump to a new URL to get to the resource and the original page to show to the browser process, There are two types of request forwarding and request based on the different forwarding methods.

Two ways to redirect:

/** * Mode one: Closer to the server response browser process */Response.setstatus (302) Response.setheader ("Location", "http://www.changjiang.com/ Testservlet "); /** * Mode two: more convenient */Response.sendredirect ("Http://www.changjiang.com/TestServlet");

Page refresh needs to carry the message content, in fact, the use of the scene is quite a lot:

/** * Jump-two times are 200 (there may be a second 304 read browser cache) * Format: seconds-To specify the number of seconds to refresh the current page * format: seconds; url= "" To specify a second after the jump to the specified URL */response.setheader ( "Refresh", "2"); Response.setheader ("Refresh", "0;url=1.html");

Take a look at some examples of page refreshes:

private void Test1 (HttpServletResponse response) throws IOException {Response.setheader ("Refresh", "3"); String data = new Random (). Nextint (100000) + ""; Response.getwriter (). write (data); } private void Test2 (HttpServletResponse response) throws IOException {Response.setheader ("refresh", "3;url="/day06/ Index.jsp ' "); Response.setcontenttype ("text/html;charset=gb2312"); Response.getwriter (). Write ("Login successful, will jump after 3 seconds, if not, please click <a href= ' xxx ' > Hyperlink </a>"); }

Finally see the server internal request forwarding, preferred to get forwarded RequestDispatcher,

Current servlet path:  http://localhost:8080/day08/a/b/Demo01Servlet Another servlet path: http://localhost:8080/day08/a/b/ Demo02servlet ServletRequest (Common) request.getrequestdispatcher ("Demo02servlet") Request.getrequestdispatcher ("/a/b /demo02servlet ")   --Note: no day08, (extension: directed to specific gravity) ServletContext this.getservletcontext (). Getrequestdispatcher ("/a/b/ Demo02servlet ")

Then you need to specify whether the request is forwarded or the request contains:

Forward (ServletRequest request, servletresponse Response) requests forwarding A forward B, outputting only B content to the browser. (If a does not have data sent response.iscomitted = False, the cache will be emptied) request forwarding only outputs the last servlet content if iscommitted = True, the forward will throw an exception. Generally if you output a small amount of data, consider Iscommitted=falseinclude (servletrequest request, servletresponse Response) request contains a contains B, first output a content to the browser, The contents of the output B to the browser. The request contains, outputting all of the servlet's aggregated content.

When using request forwarding, one request involves multiple servlet resources, which are cross-domain operations and require special attention to encoding consistency within each resource, so the following two lines of code are required in the generic servlet to ensure consistent resource encoding:

Request.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html;charset=utf-8");

Finally, summarize the characteristics of request forwarding:

    • The request path does not change, but can involve multiple resources on the server side
    • You can share the data of the request scope in a single request
    • Once a request, using request forwarding, Tomcat will create two request and a response object, two request object data is the same (can be understood as objects are cloned)

HTTP protocol Basics and Web service redirection, jump, and request forwarding

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.