JSP built-in objects: Brief introduction and use of Request and Response

Source: Internet
Author: User


JSP built-in Object request object
The request information of the client is encapsulated in the request object, so that the client can understand the customer's requirements and then respond. It is an instance of the HttpServletRequest class.
Serial number method description
1 object getAttribute (String name) returns the attribute value of the specified attribute
2 Enumeration getAttributeNames () returns the Enumeration of all available attribute names
3 String getCharacterEncoding () returns the character encoding method
4 int getContentLength () returns the length of the Request body (in bytes)
5 String getContentType () Get the MIME type of the Request body
6. ServletInputStream getInputStream () obtains the binary stream of a row in the Request body.
7 String getParameter (String name) returns the parameter value of the specified name parameter.
8 Enumeration getParameterNames () returns the Enumeration of available parameter names
9 String [] getParameterValues (String name) returns an array containing all values of the parameter name
10 String getProtocol () returns the protocol type and version number of the request.
11 String getScheme () returns the scheduler name used for the request, such as http. https and ftp.
12 String getServerName () returns the server host name that receives the request
13 int getServerPort () returns the port number used by the server to accept the request.
14 BufferedReader getReader () returns the decoded Request body
15 String getRemoteAddr () returns the IP address of the client sending the request.
16 String getRemoteHost () returns the client host name that sent this request
17 void setAttribute (String key, Object obj) set the attribute Attribute Value
18 String getRealPath (String path) returns the actual path of a virtual path.
Copy codeThe Code is as follows:
<% @ Page contentType = "text/html; charset = gb2312" %>
<% Request. setCharacterEncoding ("gb2312"); %>
<Html>
<Head>
<Title> request object_example 1 </title>
</Head>
<Body bgcolor = "# FFFFF0">
<Form action = "" method = "post">
<Input type = "text" name = "qwe">
<Input type = "submit" value = "submit">
</Form>
Request Method: <% = request. getMethod () %> <br>
Requested Resource: <% = request. getRequestURI () %> <br>
Request Protocol: <% = request. getProtocol () %> <br>
Request file name: <% = request. getServletPath () %> <br>
IP address of the requested server: <% = request. getServerName () %> <br>
Request server port: <% = request. getServerPort () %> <br>
Client IP address: <% = request. getRemoteAddr () %> <br>
Client Host Name: <% = request. getRemoteHost () %> <br>
Value for form submission: <% = request. getParameter ("qwe") %> <br>
</Body>
</Html>
<% @ Page contentType = "text/html; charset = gb2312" %>
<% Request. setCharacterEncoding ("gb2312"); %>
<% @ Page import = "java. util. Enumeration" %>
<Html>
<Head>
<Title> request object_example 2 </title>
</Head>
<Body bgcolor = "# FFFFF0">
<Form action = "" method = "post">
Username: <input type = "text" name = "username">
Password: <input type = "text" name = "userpass">
<Input type = "submit" value = "enter">
</Form>
<%
String str = "";
If (request. getParameter ("username ")! = Null & request. getParameter ("userpass ")! = Null ){
Enumeration enumt = request. getParameterNames ();
While (enumt. hasMoreElements ()){
Str = enumt. nextElement (). toString ();
Out. println (str ":" request. getParameter (str) "<br> ");
}
}
%>
</Body>
</Html>
<% @ Page contentType = "text/html; charset = gb2312" %>
<% Request. setCharacterEncoding ("gb2312"); %>
<Html>
<Head>
<Title> request object_example 3 </title>
</Head>
<Body bgcolor = "# FFFFF0">
<Form action = "" method = "post">
Good at: <input type = "checkbox" name = "cb" value = "ON1"> VC
<Input type = "checkbox" name = "cb" value = "ON2"> JAVA
<Input type = "checkbox" name = "cb" value = "ON3"> DELPHI
<Input type = "checkbox" name = "cb" value = "ON4"> VB
<Br>
<Input type = "submit" value = "enter" name = "qwe">
</Form>
<%
If (request. getParameter ("qwe ")! = Null ){
For (int I = 0; I <request. getParameterValues ("cb"). length; I ){
Out. println ("cb" I ":" request. getParameterValues ("cb") [I] "<br> ");
}
Out. println (request. getParameter ("qwe "));
}
%>
</Body>
</Html>

Response object of JSP built-in object
The response object contains information about the response to the customer request, but it is rarely used directly in JSP. It is an instance of the HttpServletResponse class.
Serial number method description
1 String getCharacterEncoding () indicates the character encoding used in the response.
2 ServletOutputStream getOutputStream () returns a binary output stream of the response
3 PrintWriter getWriter () returns an object that can output characters to the client.
4 void setContentLength (int len) set the Response Header Length
5 void setContentType (String type) set the MIME type of the response
6 sendRedirect (java. lang. String location): redirect client requests

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.