JSP built-in object -- Request

Source: Internet
Author: User
Tags html header

Each request object encapsulates a user request and all request parameters are encapsulated in the request object. Therefore, the request object is an important way to obtain request parameters.

1. Get Request Parameters and request headers

A Web application is a request/response architecture application. When a browser sends a request, some request headers are always included, and some request parameters may be sent to the server, the server is responsible for parsing the Request Header/Request Parameters by JSP or servlet, while the way through which JSP or servlet gets request parameters is request. Request is an instance of the httpservletrequest interface. It provides the following methods to obtain request parameters.

(1) string getparameter (string paramname) obtains the value of the paramname request parameter.

(2) map getparametermap () obtains a map object consisting of all request parameters and parameter values.

(3) Enumeration getparameternames () obtains the enumeration object consisting of all request parameter names

(4) value of the string [] getparametervalues (string paramname) paramname request parameter. If the request parameter has multiple values, this method returns an array composed of multiple values.

Httpservletrequest provides the following methods to access the request header:

(1) string getheader (string name) obtains the value of the specified request header.

(2) Java. util. Enumeration <string> getheadernames get the names of all request headers

(3) Java. util. Enumeration <string> getheaders (string name) gets multiple values of the specified Request Header

(4) int getintheader (string name) obtains the value of the specified request header and converts the value to an integer.

For developder, both the request header and request parameters are data sent by the user to the server. The difference is that the request header is automatically added by the browser, so a request always contains several request headers; the Request Parameters usually need to be added under the control of the developer. there are usually two ways for the client to send request parameters.

1. GET request: When you directly enter a request sent by the access address in the browser address bar or submit a request sent by the form, the form element corresponding to the form does not set the method attribute, or set the method property to get. These requests are all GET requests. A GET request will convert the name and value of the request parameter to a string and append it to the original URL. Therefore, you can see the name and value of the request parameter in the address bar. In addition, GET requests send a small amount of data, generally not greater than 2 kb

2. POST requests: This method is usually sent by submitting a form (represented by the form HTML element), and the method attribute of the form element must be set to post. The size of POST request parameters is not restricted, but usually depends on the server limit. The amount of data transmitted by post requests is larger than that transmitted by GET requests. In addition, the request parameters and corresponding values sent in the POST method are transmitted in the HTML header. Users cannot see the request parameter values in the address bar, which is relatively secure.

Case of Form submission to obtain request headers and Request Parameters

<Form if = "form1" method = "Post" Action = "request1.jsp"> User Name: <br> <input type = "text" name = "name"> <HR> gender: <br> male <input type = "radio" name = "gender" value = "male"> female <input type = "radio" name = "gender" value = "female"> <HR> favorite colors: RED: <input type = "checkbox" name = "color" value = "red"> Green: <input type = "checkbox" name = "color" value = "green"> Blue: <input type = "checkbox" name = "color" value = "blue"> <HR> country: <select name = "country"> <option value = "China"> China </option> <option value = "us"> us </option> <option value = "Russia "> Russia </option> </SELECT> <HR> <input type =" Submit "value =" Submit "> <input type =" reset "value =" reset "> </form>

Zookeeper ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<Body> <% // obtain all request headers/request parameter names enumeration <string> headernames = request. getheadernames (); While (headernames. hasmoreelements () {string headername = headernames. nextelement (); out. println (headername + "--->" + request. getheader (headername) + "<br>");} Out. print ("<HR>"); // sets the decoding method. For Simplified Chinese, UTF is used for decoding. When compiling a page, request is decoded in the corresponding format. setcharacterencoding ("UTF-8"); // obtain the form value string username = request at a time. getparameter ("name"); string gender = request. getparameter ("gender"); string [] color = request. getparametervalues ("color"); string National = request. getparameter ("country"); %> <% -- output the value of the Form in sequence -- %> your name: <% = username %> <HR> your gender: <% = gender %> <HR> your favorite color: <% for (string C: color) {%> <% = C %> <HR> <% }%> your country: <% = National %> <HR> </body>

Note: not all form fields generate request parameters, but form fields with name attributes generate request parameters. The relationship between form fields and request parameters is as follows:

1. Each form field with the name attribute corresponds to a request parameter

2. If multiple form fields have the same attributes, multiple form fields generate only one request parameter. This parameter has multiple values.

3. Specify the parameter name and value for the name attribute of the form field.

4. If the Disabled = "disabled" attribute is set for a form field, the form field does not generate request parameters.

2. Get Chinese characters in the GET Request Header

<%
// Obtain the query string in the Request Header

String rawquerystr = request. getquerystring ();
// Use urldecoder to decode the string
String querystr = urldecoder. Decode (rawquerystr, "UTF-8 ");
%>

3. Execute forward or include

Another function of request is to execute forward and include, that is, to replace the forward and include action commands provided by JSP.

The httpservletrequest class provides a getrequestdispatcher (string path), where path is the target path to be forward or include. This method returns requestdispatcher. The object provides the following two methods:

1. Forward (servletquest request, servletresponse reponse) executes forward

2. Include (servletquest request, servletresponse reponse) execute include

Eg ------ getrequestdispatcher ("/a. jsp"). Forward (request, reponse)

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.