Javaweb Study Summary (iv)--http Agreement (2)

Source: Internet
Author: User
Tags apache tomcat

1.0 HttpServletRequest Object-java Encapsulation of HTTP requests

The main function of this object is to get the requested parameter information

The Core API

Request Line

Request.getmethod (); Request method

Requset.getrequeturi ()/Request.getrequesturl (); Gets the qualified name of the requested resource

Request.getprotocol ()//requested HTTP protocol version

Request Header

Request.getheader ("name")//according to the request header name, get the request value

Requset.getheadernames ()//Get the name of all request headers

Entity content

Request.getinputstream ()//Get Entity Data content

2.0 service and Doxx method differences-source code Analysis

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); }    }

3.0 How request parameters are passed to get the problem

The Core API

Request.getparameter ("parameter name"); Gets the parameter value based on the parameter name (only one parameter can get a value)

Request.getparametervalue ("parameter name"); Gets the value of a parameter based on the parameter name (gets the value of more than one parameter)

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

4.0 coding problem of request parameter--Chinese garbled problem

Modify post mode parameter encoding

Request.setcharacterencoding ("Utf-8");

Modifying the Get mode parameter encoding

Manual decoding

String name = new String (Name.getbytes ("iso-8859-1"), "Utf-8");

5.0 HTTP Response

Specific content of the HTTP response

http/1.1 OK                -- response line Server:apache-coyote/1.1         --Response header (key-vaule) Content-length:24  Jan 01:54:57 GMT                                   -- a blank line  This is the Hello servlet!!!                  --Entity Content

5.1 Response Lines

#HTTP协议版本

#响应状态码

200 means the request processing is complete and the perfect return

302 indicates that the request requires further refinement

404 indicates that the Client access resource could not be found

500 indicates server resource send error (server internal error)

#状态描述

5.2 Response Header

Common response Headers

Location:http://www.it315.org/index.jsp   -Represents the address of the redirect,
This header is used with the 302 status code. Server:apache Tomcat ---Represents the type of server Content-encoding:gzip- -the type of data compression that the server sends to the browser content-length:80 -- Represents the length of data sent to the browser by the server CONTENT-LANGUAGE:ZH-CN --Indicates the language content-type:text/html supported by the server; charset=gb2312 -- Represents the data type and content encoding that the server sends to the browser, Last-modified:tue 18:23:51 GMT -Indicates the last modification time of the server resource refresh:1;url=http:// Www.it315.org- -Indicates timed refresh content-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 (session management is used) Expires:-1 --Indicates that the 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

5.3 Entity Content

6.0 HttpServletResponse Object--java API for HTTP response processing

1. Response lines

Response.setstatus ()//Set status code

2. Response header

Response.setheader ("name", "value")//Set response header

3. Entity Content

Response.getwriter (). writer (); Send character entity content

Response.getoutputstream (). writer (); Send byte entity contents

Javaweb Study Summary (iv)--http Agreement (2)

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.