The application _servlet of HttpServletRequest

Source: Internet
Author: User
Tags session id

The public interface class HttpServletRequest inherits from ServletRequest. A request from a client browser is encapsulated as a HttpServletRequest object. All information includes the requested address, the requested parameters, the submitted data, the uploaded file client's IP, and even the client operating system is included therein.   HttpServletResponse inherits the Servletresponse interface and provides methods related to the HTTP protocol, the primary function of which is to set the HTTP status code and manage cookies.   A, HttpServletRequest interface defines public interface HttpServletRequest extends ServletRequest;   Used to process a request message for the HTTP format of a servlet.   Method 1, Getauthtype public String getauthtype ();   Returns the authentication mode for this request.   2, GetCookies public cookie[] GetCookies (); Returns an array that contains all the cookies currently in the request.   If there is no cookie in this request, an empty array is returned.   3, Getdateheader public long Getdateheader (String name);   Returns the value of the specified request header field, which is converted to a long integer that reflects the exact to millisecond since 1970-1-1 (GMT). If the header field cannot be converted, throw a illegalargumentexception.   If this request header field does not exist, this method returns-1.   4, GetHeader public string GetHeader (string name); Returns the value of a Request header field.   (Translator Note: Unlike the previous method, the method returns a string) if the request header field does not exist, this method returns-1.   5, Getheadernames Public enumeration getheadernames ();   This method returns a list of String objects that reflect all header domain names that are requested.   Some engines may not allow access to the header domain through this method, in which case the method returns an empty list. 6. Getintheader public int Getintheader (String name);   Returns the value of the specified request header field, which is converted to an integer. If the header field cannot be converted, throw a illegalargumentexception.   If this request header field does not exist, this method returns-1.   7, GetMethod public String getmethod ();   Returns the HTTP method used by this request (for example: Get, POST, put) 8, GetPathInfo public String getpathinfo (); This method returns additional path information for the request URL after the servlet path of the requested URL. If the request URL includes a query string, the query string will not be included in the return value. This path must be decoded by the URL before returning. If there is no path information after the servlet path of this requested URL.   This method returns a null value.   9, getpathtranslated public String getpathtranslated (); This method obtains additional path information after the servlet path of the requested URL and converts it to a true path. The URL for this request must be decoded by URL before making the conversion. If there is no additional path information after the servlet path for this URL.   This method returns a null value.   10, getquerystring public String getquerystring (); Returns the query string contained in this request URL. A query string is in a URL by a ". Leads   If there is no query string, this method returns a null value.   11. Getremoteuser Public String Getremoteuser returns the username for the request, which is used as an HTTP user argument.   If there is no user name information in the request, this method returns a null value.   12, Getrequestedsessionid public String Getrequestedsessionid (); Returns the corresponding session ID for this request.   If for some reason the client provides a session ID that is not valid, the session ID will be different from the session ID in the current session, and a new session will be established.   If the request is not associated with a session, this method returns a null value. 13, Getrequesturi Public StrinG Getrequesturi (); The portion of the requested resource is defined in the URL of the request from the first line of the HTTP request. If a query string exists, the query string is not included in the return value. For example, a request is accessed through a URL path such as/catalog/books?id=1, and this method returns/catalog/books.   The return value of this method includes the servlet path and path information.   If a part of this URL path is URL-coded, the return value of this method must be decoded before it is returned.   14, Getservletpath public String getservletpath (); This method returns the request URL reflecting the part of the calling servlet. For example, a servlet is mapped to the/catalog/summer URL path, and a request uses a path such as/catalog/summer/casual.   The so-called reflection of the call to the servlet part refers to/catalog/summer. If this servlet is not invoked by path matching.   This method will return a null value.   15, GetSession public HttpSession getsession ();   Public HttpSession getsession (Boolean create); Returns the current valid session that is associated with this request. If you call this method without parameters, a new session will be created if no session is associated with the request.   If this method is invoked with a Boolean parameter, the session is only established if the argument is true. To ensure that the session can be fully maintained.   The servlet developer must call the method before the response is committed. If the parameter being brought is false, and no session is associated with the request.   This method returns a null value.   16, Isrequestedsessionidvalid public boolean isrequestedsessionidvalid (); This method checks whether the session associated with this request is currently valid.   If the session used in the current request is invalid, it cannot be returned through the GetSession method. 17. Isrequestedsessionidfromcookie Public Boolean Isrequestedsessionidfromcookie ();   If the session ID of the request is provided through a cookie from the client, the method returns True, otherwise it returns false.   18, Isrequestedsessionidfromurl public boolean isrequestedsessionidfromurl (); If the session ID of the request is provided through a portion of the client's URL, the method returns True, otherwise the return is false.   Note that this method differs from Isrequestedsessionidfromurl in the spelling of the URL.   The following methods will be canceled 19, Isrequestedsessionidfromurl public boolean isrequestedsessionidfromurl (); The method is replaced by Isrequestedsessionidfromurl.     HttpServletRequest main processing: 1. Read and write HTTP header 2. Get and set cookies 3. Get path Information 4. Identify HTTP session method:   String Getauthtype () If the servlet is protected by an authentication scheme, such as HTTP Basic authentication, the scenario name is returned. String Getcontextpath () returns the prefix of the URL for the specified servlet context (Web application). Cookie[] GetCookies () returns an array of cookies associated with the request. Long Getdateheader (String name) converts the output to a simplified version of the GetHeader () that is suitable for building a long type of date object. String GetHeader (string name) returns the specified HTTP header label. If it is given by the request, the name should be case insensitive. Enumeration Getheadernames () returns the weighted value of all HTTP header names that are given by the request. Enumeration Getheaders (String name) returns an enumeration value that requests the names of all HTTP headers of the specified type, which is useful for headers with multiple values. int Getintheader (String name) converts the output to a simplified version of the GetHeader () value of Int. String GetMethod () returns the HTTP request method (for example, get, post, and so on) string getPathInfo () returns any additional path information specified in the URL. String getpathtranslated () returns any additional path information specified in the URL, and the quilt is converted to a real path. String getquerystring () returns the query string, which is the part of the URL that follows. String Getremoteuser () If the user passes authentication, returns the remote user name, otherwise null. String Getrequestedsessionid () returns a client's session ID string Getrequesturi () that returns a portion of the URL, starting with "/", including the context, but excluding any query string. String Getservletpath () returns a simplified version of the substring HttpSession getsession () call GetSession (true) after the request URI context. HttpSession getsession (Boolean Create) returns the current HTTP session, and if it does not, creates a new session, and the Create argument is true. Principal Getprincipal () If the user passes authentication, returns a Java.security.Principal object that represents the current user, otherwise null. Boolean Isrequestedsessionidfromcookie () returns True if the requested session ID is supplied by a cookie object, otherwise false. Boolean Isrequestedsessionidfromurl () if the requested session ID is decoded in the request URL, returns True, or False Boolean isrequestedsessionidvalid () Returns TRUE if the session ID returned by the client is still valid. Boolean IsUserInRole (String role) returns TRUE if an authenticated user is currently associated with the specified roles, False if not, or if the user fails the authentication.  

HttpServletRequest Introduction

Get information about the request line

Get Network connection Information

Get Request header information

Details of the browser passing parameter information

GET Request Parameters

Get the entity content of the request message

Using request domain properties to pass information

The problem of Chinese reading of request parameters

The HTTP request message sent to the Web server by the Web client is divided into three parts:

Request Line Post/demo/login http/1.1

Request message Headers

Message body (also called entity content) username=xxxx&password=1234

The ServletRequest interface class defined in the Servlet API is used to encapsulate request messages.

HttpServletRequest is a servletrequest sub-interface dedicated to the HTTP protocol, which encapsulates HTTP request messages.

Call the HttpServletRequest object's various methods inside the service () method to get the request message.

The request line for the HTTP request message includes the request method, the resource path, and the HTTP protocol version:

Get/it315/servlet/requesturi?param1=a&param2=b http/1.1

The GetMethod method returns the requested method in the HTTP request message.

The Getrequesturi method returns the resource name portion of the request line.

The GetQueryString method returns the parameter portion of the request line.

The Getprotocol method returns the protocol name and version in the request line.

The Getcontextpath method returns the path of the Web application to which the request resource belongs.

The GetPathInfo method returns additional path information in the request URL. The extra path information is the content that follows the path of the servlet in the request URL and before the query parameters, and begins with "/".

The GetPathTranslated method returns the true path of the resource corresponding to the additional path information in the URL.

The Getservletpath method returns the name of the servlet or the path mapped by the servlet.

The Getremoteaddr method returns the IP address of the client that made the request, in the form of "192.168.0.3" character text. (*)

The Getremotehost method returns the full hostname of the requesting client, which is the "pc1.it315.org" format.

The Getremoteport method returns the port number of the network interface used by the requesting client.

The Getlocaladdr method returns the IP address of the network interface on the Web server that receives the current request.

The Getlocalname method returns the host name of the IP address of the network interface on the Web server that receives the current request.

The Getlocalport method returns the port number of the network interface on the Web server that receives the current request.

The getServerName method returns the host name that the current request points to.

The Getserverport method returns the server port number that the current request is connected to.

The Getscheme method returns the requested protocol name, such as HTTP, HTTPS, or FTP.

The Getrequesturl method returns the full URL of the client when the request is made.

GetHeader method

Getheaders method

Getheadernames method

Getintheader method

Getdateheader method

getContentType method

Getcontentlength method

Getcharacterencoding method

Enumeration headernames = Request.getheadernames ();

while (Headernames.hasmoreelements ()) {

String headername = (string) headernames.nextelement ();

Out.print (Headername + ":" + request.getheader (headername) + "<br>");

/* If you want to consider the same request header names may occur multiple times,

Then you should use the following code snippet instead of the above line of program code * *

/*enumeration values = request.getheaders (headername);

while (Values.hasmoreelements ()) {

Out.print (Headername + ":" + (String) values.nextelement () + "<br>");

}*/

}

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.