Httpservletrequest,httpservletresponse objects in a servlet and differences between service and Doxx methods

Source: Internet
Author: User
Tags apache tomcat

One: The HttpServletRequest object is used to obtain the request data.

Core API:                    request line:                         Request.getmethod ();   Request Method                        Request.getrequeturi ()   / request.getrequeturl ()   Request Resource                        Request.getprotocol ()   Request HTTP Protocol version                                        request header:                        Request.getheader ("name")   gets the request value based on the request header                        Request.getheadernames ()    get all request header names                    entity content:                        Request.getinputstream ()   get entity content data

Two: Service and Doxx method differences

httpsevlet type of source code:protected voidService (HttpServletRequest req, HttpServletResponse resp)throwsservletexception, IOException {//get the Request methodString method =Req.getmethod (); if(Method.equals (method_get)) {LongLastModified =getlastmodified (req); if(LastModified = =-1) {                //servlet doesn ' t support if-modified-since, no reason//To go through further expensive logicdoget (req, resp); } Else {                Longifmodifiedsince; Try{ifmodifiedsince=Req.getdateheader (header_ifmodsince); } Catch(illegalargumentexception iae) {//Invalid Date Header-proceed as if none was setIfmodifiedsince =-1; }                if(Ifmodifiedsince < (lastmodified/1000 * 1000)) {                    //If The servlet mod time is later, call Doget ()//Round down to the nearest second for a proper compare//A ifmodifiedsince of-1 would always is lessmaybesetlastmodified (resp, lastmodified);                Doget (req, resp); } Else{resp.setstatus (httpservletresponse.sc_not_modified); }            }        } Else if(Method.equals (Method_head)) {LongLastModified =getlastmodified (req);            Maybesetlastmodified (resp, lastmodified);        Dohead (req, resp); } Else if(Method.equals (method_post)) {DoPost (req, resp); } Else if(Method.equals (method_put)) {DoPut (req, resp); } Else if(Method.equals (Method_delete)) {DoDelete (req, resp); } Else if(Method.equals (method_options)) {dooptions (REQ,RESP); } Else if(Method.equals (method_trace)) {dotrace (REQ,RESP); } Else {            //            //Note that this means NO servlet supports whatever//method is requested, anywhere on the this server. //String errmsg= Lstrings.getstring ("http.method_not_implemented"); Object[] Errargs=NewObject[1]; errargs[0] =method; ErrMsg=Messageformat.format (errmsg, Errargs);        Resp.senderror (httpservletresponse.sc_not_implemented, errmsg); }    }

Three: The request parameter gets passed
Get method: The parameter is placed behind the URI
Post mode: Parameters are placed in the entity content

Get the Get mode parameters:
Request.getquerystring ();
Get post Mode parameters:
Request.getinputstream ();

Four: The problem: But the above two are not common, and the obtained parameters need to be further resolved.
So you can use a uniform and convenient way to get parameters:

The Core API:
Request.getparameter ("parameter name"); Get parameter values based on parameter name (note that you can only get parameters of one value)
Request.getparametervalue ("parameter name"); Gets the parameter value (parameter that can get multiple values) according to the name of the argument

Request.getparameternames (); Get list of all parameter names

Five: Request parameter encoding problem
To modify the post mode parameter encoding:
Request.setcharacterencoding ("Utf-8");
Modify the Get mode parameter encoding:
Manual decoding: String name = new String (Name.getbytes ("iso-8859-1"), "Utf-8");

Six: Response line
#http协议版本
#状态码: The server processes the result of the request (status)
A common state:
200: Request processing complete and perfect return
302: Indicates that the request requires further refinement.
404: The resource that represents the client access is not found.
500: Indicates that the server sent an error to the resource. (Server internal Error)
Seven: Common response headers

Location:http://www.it315.org/index.jsp-Represents the redirected address, which is used with the status code of 302. Server:apache Tomcat---represents the type of server content-encoding:gzip--represents the type of data compression that the server sends to the browser content-LENGTH:80--represents the length of data that the server sends to the browser content-LANGUAGE:ZH-CN--represents the language content supported by the server-type:text/html; charset=gb2312--indicates the data type and content encoding the server sent to the browser last-modified:tue, each Jul 18:23:51 GMT--indicates the last modified time of the server resource refresh:1;url=http://www.it315.org--Indicates timed refreshContent-disposition:attachment; Filename=aaa.zip--means to tell the browser to download the resource (used when downloading the file) Transfer-Encoding:chunkedset-COOKIE:SS=Q0=5LB_NQ; Path=/search--represents the cookie information that the server sends to the browser (for session management) Expires:-1--indicates that the notification browser is not cached cache-control:no-Cachepragma:no-Cacheconnection:close/keep-alive--Indicates the connection status of the server and the browser. Close: Closing connection keep-alive: Saving connection

HttpServletResponse:

HttpServletResponse Object Modification Response information:

Response Line:
Response.setstatus () Setting the status code
Response header:
Response.setheader ("name", "value") Set the response header
Entity content:
Response.getwriter (). writer (); Send character entity content
Response.getoutputstream (). Writer () Send byte entity contents

Httpservletrequest,httpservletresponse objects in a servlet and differences between service and Doxx methods

Related Article

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.