Servlet--httpservletrequest interface, HttpServletResponse interface

Source: Internet
Author: User

    • HttpServletRequest interface
Definition
Public interface HttpServletRequest extends ServletRequest;

Used to handle a request for the HTTP format of the 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 current cookies 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 is accurate to milliseconds since 1970-1-1 (GMT). If the header field cannot be converted, throw a illegalargumentexception. If the 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. (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 ();
The method returns a list of String objects that reflect all the header domain names requested. Some engines may not allow access to the header domain in this way, 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 the 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 within the return value. This path must be decoded by the URL before it is returned. 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 the additional path information after the Servlet path of the URL of the request, and converts it into a real path. The URL of the request must be URL-decoded before 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 that is contained by this request URL. A query string character in a URL consists of a "? Leads If there is no query string, this method returns a null value.
11, Getremoteuser
Public String Getremoteuser
Returns the user name that was requested, which is used for the 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 session ID provided by the client is not valid, the session ID will be different from the session ID in the current session, and a new session will be created at the same time. If the request is not associated with a session, this method returns a null value.
13, Getrequesturi
Public String Getrequesturi ();
The part of the requested resource is defined from the first line of the HTTP request that is returned in the requested URL. 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 portion of this URL path is URL-encoded, the return value of the method must be decoded before it is returned.
14, Getservletpath
Public String Getservletpath ();
This method returns the part of the request URL that reflects the calling Servlet. For example, a Servlet is mapped to the URL path of/catalog/summer, and a request uses a path such as/catalog/summer/casual. The so-called reflection calls the Servlet part refers to the/catalog/summer. If this Servlet is not called 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 associated with this request. If this method is called without parameters, a new session will be created without a session associated with the request. If this method is called with a Boolean parameter, the session is created only 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 not valid, it will not be returned by the GetSession method.
17, Isrequestedsessionidfromcookie
public boolean Isrequestedsessionidfromcookie ();
If the session ID of this request is provided by a cookie on the client, the method returns True, otherwise it returns false.
18, Isrequestedsessionidfromurl
public boolean isrequestedsessionidfromurl ();
If the session ID of this request is provided through a portion of the client's URL, the method returns True, otherwise false is returned. Note that this method differs from Isrequestedsessionidfromurl in the spelling of the URL.
The following methods will be canceled.
19, Isrequestedsessionidfromurl
public boolean isrequestedsessionidfromurl ();
This method is replaced by Isrequestedsessionidfromurl.

    • HttpServletResponse class
Defined
Public interface HttpServletResponse extends Servletresponse
Describes an HTTP response that is returned to the client. This interface allows the Servlet programmer to take advantage of the header information specified by the HTTP protocol.
Member variables
public static final int sc_continue = 100;
public static final int sc_switching_protocols= 101;
public static final int SC_OK = 200;
public static final int sc_created = 201;
public static final int sc_accepted = 202;
public static final int sc_non_authoritative_information= 203;
public static final int sc_no_content = 204;
public static final int sc_reset_content = 205;
public static final int sc_partial_content= 206;
public static final int sc_multiple_choices = 300;
public static final int sc_moved_permanently = 301;
public static final int sc_moved_temporarily= 302;
public static final int sc_see_other = 303;
public static final int sc_not_modified = 304;
public static final int sc_use_proxy = 305;
public static final int sc_bad_request = 400;
public static final int sc_unauthorized = 401;
public static final int sc_payment_required= 402;
public static final int sc_forbidden = 403;
public static final int sc_not_found = 404;
public static final int sc_method_not_allowed = 405;
public static final int sc_not_acceptable = 406;
public static final int sc_proxy_authentication_required= 407;
public static final int sc_request_timeout= 408;
public static final int sc_conflict = 409;
public static final int sc_gone = 410;
public static final int sc_length_required= 411;
public static final int sc_precondition_failed = 412;
public static final int sc_request_entity_too_large= 413;
public static final int sc_request_uri_too_long= 414;
public static final int sc_unsupported_media_type= 415;
public static final int sc_internal_server_error= 500;
public static final int sc_not_implemented = 501;
public static final int sc_bad_gateway = 502;
public static final int sc_service_unavailable = 503;
public static final int sc_gateway_timeout = 504;
public static final int sc_http_version_not_supported= 505;
The above HTTP status codes are defined by http/1.1.
Method
1, Addcookie
public void Addcookie (Cookiecookie);
Adds a specified cookie to the response. You can call this method multiple times to define multiple cookies. In order to set the appropriate header field, the method should be called before the response is committed.
2, Containsheader
public boolean Containsheader (String name);
Checks whether the specified response header is set.
3, Encoderedirecturl
public string encoderedirecturl (string url);
Encodes the specified URL used by the Sendredirect method. If the encoding is not required, the URL is returned directly. This additional encoding method is provided because, in the case of redirect, the rules and general conditions that determine whether to encode URLs are different. The given URL must be an absolute URL. A relative URL cannot be received, and a illegalargumentexception is thrown. All URLs provided to the Sendredirect method should be run in this way to ensure that session tracking works correctly in all browsers.
4, Encodeurl
public string encodeurl (string url);
Encodes the URL that contains the session ID. If the encoding is not required, the URL is returned directly. The servlet engine must provide a URL encoding method, because in some cases we will have to rewrite the URL, for example, to include a valid session in response to the request, but the session cannot be maintained by means of a non-URL (such as a cookie). All URLs provided to the Servlet should be run in this way to ensure that session tracking can be
Run correctly in the browser.
5, Senderror
public void Senderror (int statusCode) throws IOException;
public void Senderror (int statusCode, String message) throws
IOException;
Sends the client an error response with the given status code. If a message parameter is provided, which is emitted as part of the response body, the server returns the standard information corresponding to the error code. When this method is called, the response is immediately committed. After calling this method, the Servlet will no longer have more output.
6, Sendredirect
public void Sendredirect (String location) throws IOException;

Sends a temporary steering response (sc_moved_temporarily) to the client using the given path. The given path must be an absolute URL. Relative URLs will not be received and will throw a illegalargumentexception. This method must be called before the response is committed. When this method is called, the response is immediately committed. After calling this method, the Servlet will no longer have more output.


This method is necessary to say, pay attention to the interface inside the description is: Must be absolute path.

But in the actual development, we can write the relative path, can also write absolute path, this is why?

Open Tomcat inside the source of response in a look to understand:

public void Sendredirect (String location)         throws IOException {        if (iscommitted ())            throw new IllegalStateException                (sm.getstring ("coyoteResponse.sendRedirect.ise"));        Ignore any call from an included servlet        if (included)            return;         Clear Any data content the has been buffered        resetbuffer ();        Generate a temporary redirect to the specified location        try {            String absolute = toabsolute (location);            SetStatus (sc_found);            SetHeader ("location", absolute);        } catch (IllegalArgumentException e) {            setStatus (sc_not_found);        }        Cause the response to being finished (from the application perspective)        setsuspended (True);    }

 /** * Convert (if necessary) and return the absolute URL that represents the * resource referenced by this POSSIB  Ly relative URL.     If This URL is * already absolute, return it unchanged. * * @param location URL to being (possibly) converted and then returned * * @exception illegalargumentexception I f a malformedurlexception is * thrown if converting the relative URL to an absolute one */private String to        Absolute (String location) {if (location = = null) return (location);        Boolean leadingslash = Location.startswith ("/");            if (Leadingslash | |!hasscheme (location)) {redirecturlcc.recycle ();            String scheme = Request.getscheme ();            String name = Request.getservername ();            int port = Request.getserverport ();                try {redirecturlcc.append (scheme, 0, Scheme.length ());                Redirecturlcc.append ("://", 0, 3); Redirecturlcc.append (name, 0,Name.length ()); if ((Scheme.equals ("http") && port! = 80) | | (Scheme.equals ("https") && port! = 443))                    {redirecturlcc.append (': ');                    String PortS = port + "";                Redirecturlcc.append (PortS, 0, Ports.length ());                    } if (!leadingslash) {String relativepath = Request.getdecodedrequesturi ();                    int pos = Relativepath.lastindexof ('/');                                        RelativePath = relativepath.substring (0, POS);                    String Encodeduri = null;                    Final String frelativepath = RelativePath; if (securityutil.ispackageprotectionenabled ()) {try{Encodeduri = (stri                                                                    NG) accesscontroller.doprivileged (new Privilegedexceptionaction () { PuBlic Object Run () throws ioexception{return Urlencoder.encodeurl (Frelativepath);                           }                           });                                } catch (Privilegedactionexception pae) {IllegalArgumentException iae =                            New IllegalArgumentException (location);                            Iae.initcause (Pae.getexception ());                        Throw iae;                    }} else {Encodeduri = Urlencoder.encodeurl (RelativePath);                    } redirecturlcc.append (Encodeduri, 0, Encodeduri.length ());                Redirecturlcc.append ('/');            } redirecturlcc.append (location, 0, Location.length ()); } catch (IOException e) {illegalargumentexception iae = new IllegalArgumentException (lo                cation); Iae.Initcause (e);            Throw iae;        } return redirecturlcc.tostring ();        } else {return (location); }    }

7, Setdateheader

Public void Setdateheader (String name, long date);
Sets the response header with a given name and date value, where the date value should be a long integer that is accurate to milliseconds since 1970-1-1 (GMT). If the response header is already set, the new value overrides the current value.
8, SetHeader
public void SetHeader (string name, string value);
Sets the response header with a given name and domain. If the response header is already set, the new value overrides the current value.
9, Setintheader
public void Setintheader (String name, int value);
Sets the response header with a given name and shaping value. If the response header is already set, the new value overrides the current value.
10, SetStatus
public void setStatus (int statusCode);
This method sets the status code for the response, and if the status code is already set, the new value overrides the current value. Some of the following methods will be canceled \
11, Encoderedirecturl
public string encoderedirecturl (string url);
This method is replaced by Encoderedirecturl.
12, Encodeurl
public string encodeurl (string url);
This method is replaced by Encodeurl.
13, SetStatus
public void SetStatus (int statusCode, String message);
This method sets the status code for the response, and if the status code is already set, the new value overrides the current value. If a message is provided, it will also be sent as part of the response body.

Servlet--httpservletrequest interface, HttpServletResponse interface

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.