Request and Respone in the Java Web

Source: Internet
Author: User


Response and request

------------------


The process by which the server processes requests:
Each time the server receives a request, it opens a new thread for the request.
The server encapsulates the client's request data into the request object, requesting the data carrier! Bag
The server also creates a response object, which is connected to the client and can be used to send a response to the client. Phone


------------------




Response: Its type is HttpServletResponse
Servletresponse--> types that are not protocol Independent
Httpservletresponse--> types associated with the HTTP protocol


Recall the HTTP protocol! What does the content of the response in the HTTP protocol contain?
* Status code: 200 for success, 302 for redirection, 404 for Client Error (no resource accessed), 500 for server-side error
> senderror (int sc)--Send error status codes, such as 404, 500
> senderror (int sc, String msg)--Also send error status code, can also bring an error message!
> setStatus (int sc)--Send a successful status code that can be used to send 302
Case:
> Send 404
* Response head: Content-type, Refresh, location, etc.
The head is a key value pair! There may be a header (a name, a value), or there may be a header (a name, multiple values!). )
> *****setheader (string name, String value): For single-valued response headers, for example: Response.setheader ("AAA", "AAA");
> AddHeader (string name, String value): Response header for multiple values
Response.AddHeader ("AAA", "A");
Response.AddHeader ("AAA", "AA");
Response.AddHeader ("AAA", "AAA");
> Setintheader (String name, int value): Response headers for single-valued int types
` Response.setintheader ("Content-length", 888);
> Addintheader (String name, int value): Response headers for multi-valued int types
> Setdateheader (String name, Long value): Response headers for single-valued millisecond types
Response.setdateheader ("Expires", 1000 * 60 * 60 * 24);
> Adddateheader (String name, Long value): Response headers for multi-valued millisecond types
Case:
> Send 302, set location header, complete the redirect!
> Timed refresh: Set refresh header, you can interpret it as a timed redirect!
> Disable browser cache: Cache-control, pragma, expires
> <meta> tags can replace the response header: <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
* Response body: Usually HTML, can also be a picture!
Two streams for > response:
<> Servletoutputstream, which is used to send byte data to the client. Servletoutputstream out = Resopnse.getoutputstream ();
<> PrintWriter, used to send character data to the client! The encoding needs to be set. PrintWriter writer = Response.getwriter ();
<> Two streams cannot be used at the same time!
Case:
> Sending character data using PrintWriter
> Send byte data using Servletoutputstream (image)
* Redirect: Set 302, set location! The change is only location, so Java provides a quick way to complete the redirect!
> Sendredirect (String location) method


------------------


Request---encapsulates all of the requested data from the client!
Request Line
Request Header
Blank Line
Request body (get no body)


Recall the HTTP protocol! The data in the request agreement can be obtained by requesting the Object!
* Get Common information
> Get client IP, case: IP. REQUEST.GETREMOTEADDR ()
> Request mode, Request.getmethod (), may be post or get
* Get HTTP request header
> *****string getheader (String name) for single-valued headers
> int Getintheader (String name) for request headers of single-valued type int
> Long Getdateheader (String name) for a single-value millisecond type of request header
> enumeration<string> getheaders (String name) for multi-valued request headers


Case:
> Identifying user Browser types through user-agent
> Anti-theft chain: If the request is not sent through the hyperlink on this site, send error status code 404. Referer this request header, indicating the source of the request!
* GET request URL
Http://localhost:8080/day10_2/AServlet?username=xxx&password=yyy
> String getscheme (): Get protocol, HTTP
> String getservername (): Gets the server name, localhost
> String getserverport (): Get Server port, 8080
> *****string getcontextpath (): Get the project name,/day10_2
> String getservletpath (): Gets the servlet path,/aservlet
> String getquerystring (): Gets the part of the parameter, which is the part following the question mark. Username=xxx&password=yyy
> String Getrequesturi (): Gets the request URI, equal to the project name +servlet path. /day10_2/aservlet
> String getrequesturl (): Gets the request URL, equal to the entire request path that does not contain the parameter. Http://localhost:8080/day10_2/AServlet


* GET request Parameters: Request parameters are sent to the server by the client! It may be in the request body (POST), or after the URL (GET)
Request parameters: There is one parameter for one value, and one parameter for multiple values!
> *****string getparameter (String name): Gets the request parameter value for the specified name, for single-valued request parameters
> string[] getparametervalues (String name): Gets the request parameter value for the specified name, applies to the multivalued request parameter
> Enumeration<string> getparameternames (): Get all request parameter names
> *****map<string,string[]> getparametermap (): Gets all request parameters, where key is the parameter name and value is the parameter value.
Case: Hyperlink Parameters
Case: Form data


* Request forwarding and request inclusion
RequestDispatcher rd = Request.getrequestdispatcher ("/myservlet"); Get the RequestDispatcher object using request, the parameter of the method is the servlet path of the servlet being forwarded or contained
Request Forwarding: *****rd.forward (request,response);
Request includes: Rd.include (request,response);


Sometimes a request requires multiple servlet collaboration to complete, so you need to jump to another servlet! in one servlet
> A request across multiple servlets, which requires the use of forwarding and inclusion.
> Request Forwarding: Complete the response body from the next servlet! The current servlet can set the response header! (Leave the head without body)
> Request contains: Two servlet co-not completed response body! (All stay)
> Whether the request is forwarded or the request is included, it is within a range of requests! Use the same request and response!
* Request Domain
The three domain objects in the servlet: request, Session, application, have the following three methods:
> void SetAttribute (String name, Object value)
> Object getattribute (String name)
> void RemoveAttribute (String name);
> Use Request.setattribute (), Request.getattribute () to transmit values within the same request range! The previous servlet calls SetAttribute () to save the value, and the next servlet calls GetAttribute () to get the value.


* Differences in Request forwarding and redirection
> Request Forwarding is a one-time response, and redirection is two requests two responses
> The request forwarding Address bar does not change, and redirects show the address of the latter request
> Request forwarding can only be forwarded to other servlets in this project, and redirection is not only redirected to other servlets in this project, but also to other projects
> Request Forwarding is the server-side behavior, just give forward the servlet path, and redirect needs to give RequestUri, that is, contains the project name!
> Request forwarding and redirection efficiency is forwarding high! Because it's a request!
<> need to change the address bar, then redirect must be used!
<> need to get data from the request domain in the next servlet, you must use forwarding!
  
------------------

Request and Respone in the Java Web

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.