Use Servlet to process HTTP requests and servlet to process requests
The specific method is as follows:
Request Line Method:
- GetMethod () is used to obtain the request line, such as get and post.
- GetRequestURI () obtains the unified Resource Identifier (relative, does not contain QueryString) of the request (URL is a specific URI, which not only uniquely identifies the resource, it also provides information for locating the resource. URI is a semantic abstract concept. It can be absolute or relative, while URL must provide sufficient information for locating. Therefore, it is absolute, in general, the relative URL is for another absolute URL, which is basically absolute .), For example,/lovo/index.html
- GetRequestURL () obtains the unified resource locator (used for absolute location locating, that is, it can be located without knowing the root directory. Is there any? For example, http: // 127.0.0.1: 8080/lovo/index.html? Name = string.
- Get the protocol and version by getProtocol.
- GetContextPath () obtains the context path, that is, the virtual path. The root path of the web application for external access, such as http: // localhost: 8080/sun.
- GetServletPath () gets the Servlet ing path.
Request Header method:
GetHeader (String name) returns the value of the specified request header, such as request. getHeader ("Accept"); HTTP header used to access Accept; request. getHeader ("Host") Output: 192.168.0.1: 8080; request. getHeader ("Referer"); request. getHeader ("Accept-Language"); request. getHeader ("Accept-Encoding"); request. getHeader ("User-Agent"); request. getHeader ("Connection"); request. getHeader ("Cookie ")
GetHeaderNames () This method obtains the name of an Enumeration containing all request headers received in a specific request.
GetIntHeader (String name) obtains the value of a specific request header and converts it to the INT type.
GetDateHeader (String name) obtains the value of a specific request header and converts it to the Date type.
Message Body related methods:
- GetParameter (String name) obtains the value of the form parameter.
- GetParameterValues (String parameterName) obtains multiple parameter values of the same parameter name and returns a String array object.
- GetParameterNames () returns the list of all form parameter names in the request by enumeration.
Request forwarding and request range:
Request forwarding:
GetRequestDispatcher (String path) returns the RequestDispatcher object in the given path.
GetNamedDispatcher (String name) returns the RequestDispatcher object of a Servlet with a name.
Forward (ServletReqeust, ServletResponse) distributes request and response objects to the new resources determined by the RequestDispatcher object
Include (ServletReqeust, ServletResponse) is similar to forward () but there are some restrictions. This method includes server-side resources. The contained resources cannot change the response status code or set header information.
Request range:
The request scope only applies between two resources related to a request. If you need to continue to transfer to the third or more Servlets, you should continue to call setAttribute () and getAttribute () of the HttpServletRequest object () method
Appendix: request Method Daquan:
Protocol: request. getProtocol ()
Output: HTTP/1.1
Server Information getServletConfig (). getServletContext (). getServerInfo ()
Output: JavaServer Web Dev Kit/1.0 EA (JSP 1.0; Servlet 2.1; Java 1.2; Windows NT 5.0x86; java. vendor = Sun Microsystems Inc .)
Client IP: request. getRemoteAddr ()
Output: 192.168.0.106
Client Host Name: request. getRemoteHost ()
Output: abc
Encoding: request. getCharacterEncoding ()
Output: GB2312
Number of bytes sent: request. getContentLength ()
Output:-1
Data Type: request. getContentType ()
Output: null
Authorization header: request. getAuthType ()
Output: basic or digest
Request type: request. getMethod ()
Output: usually GET or POST. But occasionally HEAD, PUT, Delete, OPTIONS, or TRACE.
Additional path information in the URL: request. getPathInfo ()
Output: the part of the URL following the Servlet Path and before the query string.
Request. getPathTranslated ()
The path information mapped to the actual server path.
Request. getQueryString ()
This is the query string appended to the URL in string form, and the data is still URL encoded. In Servlet, undecoded data is rarely used. Generally, getParameter is used to access various parameters.
Request. getRemoteUser ()
If the Authorization header is provided, it indicates the user part. It indicates the name of the user who sends the request.
Request. getRequestedSessionId ()
Output SessionId
Request. getRequestURI ()
Request URL
Request. getServletPath ()
The part in the URL that calls the Servlet. It does not contain additional path information and query strings.
Request. getHeader ("Accept ")
Access the HTTP header of Accept.
Request. getHeader ("Host ")
Output: 192.168.0.1: 8080
Request. getHeader ("Referer ")
Request. getHeader ("Accept-Language ")
Request. getHeader ("Accept-Encoding ")
Request. getHeader ("User-Agent ")
Request. getHeader ("Connection ")
Request. getHeader ("Cookie ")
Response protocol name: request. getScheme ()
Output: HTTP
Web Server Name: request. getServerName ()
Output: 192.168.0.1
Server listening port: request. getServerPort ()
Output: 8080
Original address: Get all the parameter names and their parameter values in the request. Author: eraser
There are two methods in the request.
Request. getParameterMap ();
Request. getParameterNames ();
I want to use these two methods.
1. Use request. getParameterNames ();
Enumeration enu = request. getParameterNames ();
While (enu. hasMoreElements ()){
String paraName = (String) enu. nextElement ();
System. out. println (paraName + ":" + request. getParameter (paraName ));
}
2. Request. getParameterMap ();
Access URL: http: // 127.0.0.1: 8080/test. jsp? A = 1 & B = 2 & c = 3
Test code:
Map map = request. getParameterMap ();
Set keSet = map. entrySet ();
For (Iterator itr = keSet. iterator (); itr. hasNext ();){
Map. Entry me = (Map. Entry) itr. next ();
Object OK = me. getKey ();
Object ov = me. getValue ();
String [] value = new String [1];
If (ov instanceof String []) {
Value = (String []) ov;
} Else {
Value [0] = ov. toString ();
}
For (int k = 0; k <value. length; k ++ ){
System. out. println (OK + "=" + value [k]);
}
}