Request and Response Usage Summary

Source: Internet
Author: User
RequestI conclude: just remember that as long as there is information about the client request, you can obtain it, such as request headers, request methods, request parameters, consumer IP and so on.
3. In what case is the response. Under what circumstances is the request.   Simply put, the request is as if you were to borrow something, the response is that he lent you this thing ... [JSP] Hidden object in JSP--request--
Request, you can use it in a JSP Web page, after translating to a servlet, It is converted to an object of the Javax.servlet.http.HttpServletRequest type, and the HttpServletRequest object is the object of the request made by the client, as long as there is information about the client request that can be obtained by it, such as the request header , request methods, request parameters, consumer IP, and so on.
Main methods of Request:
Getparameternames (): Gets the request parameter name issued by the client.
GetParameter (): Allows you to specify the request parameter name to obtain the corresponding set value.
getServerName (): the requested server.
Getprotocol (): Use protocol.
GetMethod (): Request method.
Getserverport (): Request port number.
Getcontextpath (): Context path. Context is better (i.e. a project)
Getservletpath (): servlet path.
Getrequesturi (): URI path.
GetQueryString (): query string.
GETREMOTEADDR (): Consumer host IP.
Getremoteport (): The user uses the port number.
Example:
Http://localhost:8080/myjsp/requestdemo.jsp?name=john&nick=johncn
The corresponding information is:
Requested server: localhost
Use protocol: http/1.1
Request method: Get
Port number requested: 8080
Context Path:/myjsp
Servlet Path:/requestdemo.jsp
URI Path:/myjsp/requestdemo.jsp
Query string: NAME=JOHN&NICK=JOHNCN
Consumer host ip:127.0.0.1
User use port number: 1060
Response[JSP] Hidden object in JSP--response--
The response hidden object of the JSP, after being converted to a servlet, corresponds to the HttpServletResponse type object, and the HttpServletResponse object has a response to the client request, which you can use to set up some messages to respond to. For example, title information, response status code, and so on.
Common methods of response:
SetHeader (): Is a common header setting method that you can use to set any name/value header.
Setintheader (): is a version that is specifically used to set the integer value header.
Setdateheader (): Is the date set version of SetHeader (), the second parameter is a long value that sets date, and 0 indicates GMT 1970/1/1 00:00.
Note: The above 3 functions are used to set the header of the HTTP protocol, and some knowledge of the HTTP protocol is required to properly use it.
SetStatus (): is used to set the status code of the response, for example 404 Not Found,httpservletresponse class provides some mnemonic constants, such as Sc_not_found is to represent 404 status codes (can be in the servlet In the API file, the associated mnemonic constants are queried.
Senderror (): Displays an error message based on the server's default error page return.
Sendredirect (): Sets the redirect page.
Getwriter (): Gets the PrintWriter object to write out the ontology information that responds to the server.

1. Output text and HTML code
Using the Write () method provided by response
Response.Write ("Peterbbs");
Response.Write ("< br/>"); output line-wrapping symbol
string table = "<table border=1> ...";
Response.Write (table);//output html



2. Output image
Response output image is realized by outputting the binary data of the image BinaryWrite ();
Specific methods:
(1) Convert the virtual address of the image to the physical address
(2) Define file stream read binary data and save
(3) Calling method BinaryWrite ();
(4) Set the output format of the current Web Forms page to "Image/pjpeg", which can output only the image of the filename suffix ". jpg"
(5) Call the end () method of the Response object to abort the other output of the page.

String path = Request.mappath ("~/image.gif");
FileStream fs = new FileStream (path,filemode.open,fileaccess.read);
byte[] ImageData = new byte[(int) fs. Length];
Fs. Read (ImageData, 0, (int) fs. Length);
Response.BinaryWrite (ImageData);
Response.End ()//When using this thing, you will find that any write method you Response is invalid.


3. Jump between pages
Here I will not write, in my Baidu space on the new window of the Response.Redirect method


4. Parameter passing between pages
directly on the Response.Redirect ("######.aspx? id = 10 "); On it, you need to remember this format, very fixed.
gets this value when the argument is passed directly to call Request.param

Other questions: in 1.JSP, request is not an instance of HttpServletRequest.   
                       response is not an example of httpservletresponse.   
        If, I don't understand, HttpServletRequest is not an interface. How can an interface have an instance?   
        If not, request/response it is an instance of which class it is. The request is an instance of a subclass of HttpServletRequest, which is ambiguous, in fact this particular class is different depending on the server, but these concrete classes implement the HttpServletRequest interface. Since it is not possible to determine which class is specific, the request can only be said to correspond to the javax.servlet.http.HttpServletRequest. You can use the same JSP page under different servers, embedding <%=request.getclass ()%> to see that the request object corresponds to the Javax.servlet.http.HttpServletRequest class.   
  Response objects correspond to the Javax.servlet.http.HttpServletResponse Class 2. When the request object gets the characters submitted by the customer, the garbled problem occurs, Special handling is required. The workaround is to add a sentence of
request.setcharacterencoding ("GB18030") to the page where the content of the request object is to be received;
The GB18030 here is based on your own page code. 3. In what case is the response. Under what circumstances is the request. Simply put, the request is as if you were to borrow something, the response is that he lent you this thing ...

 

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.