JSP built-in objects: a simple introduction to request and response and the use of _jsp programming

Source: Internet
Author: User

JSP built-in object Request object
The client's request information is encapsulated in a request object to understand the customer's needs and then respond. It is an instance of the HttpServletRequest class.
Serial Number Method description
1 Object getattribute (String name) returns the property value of the specified property
2 enumeration Getattributenames () returns an enumeration of all available property names
3 String getcharacterencoding () returns the character encoding method
4 int Getcontentlength () returns the length of the requested body (in bytes)
5 String getContentType () Gets the MIME type of the requested body
6 ServletInputStream getInputStream () gets the binary stream of a row in the request body
7 string GetParameter (string name) returns the parameter value of the specified parameter of name
8 enumeration Getparameternames () returns an enumeration of the available parameter names
9 string[] Getparametervalues (String name) returns an array of all values containing the parameter name
A String Getprotocol () returns the type and version number of the protocol used for the request
One String Getscheme () returns the plan name for the request, such as: Http.https and FTP
A String getServerName () returns the server host name that accepts the request
int Getserverport () returns the port number used by the server to accept this request
BufferedReader Getreader () returns the decoded request body
String getremoteaddr () Returns the client IP address that sent this request
String Getremotehost () returns the client host name that sent the request
void setattribute (String key,object obj) Set property value for property
A string Getrealpath (string path) returns the true path of a virtual path
Copy Code code as follows:

<%@ page contenttype= "text/html;charset=gb2312"%>
<%request.setcharacterencoding ("gb2312");%>
< html>
< head>
< Title>request Object _ example 1</title>
< body bgcolor= "#FFFFF0" >
< form action= "" method= "POST" >
< input type= "text" name= "Qwe" >
< input type= "submit" value= "submitted" >
</form>
Request Mode:<%=request.getmethod ()%>< br>
The requested resource:<%=request.getrequesturi ()%>< br>
Requested protocol:<%=request.getprotocol ()%>< br>
The requested filename:<%=request.getservletpath ()%>< br>
The requested server's ip:<%=request.getservername ()%>< br>
Requesting the server's port:<%=request.getserverport ()%>< br>
Client IP address:<%=request.getremoteaddr ()%>< br>
Client host name:<%=request.getremotehost ()%>< br>
The value of the form submission:<%=request.getparameter ("Qwe")%>< br>
</body>
<%@ page contenttype= "text/html;charset=gb2312"%>
<%request.setcharacterencoding ("gb2312");%>
<%@ page import= "Java.util.Enumeration"%>
< html>
< head>
< Title>request Object _ example 2</title>
< body bgcolor= "#FFFFF0" >
< form action= "" method= "POST" >
User name:< 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>
<%@ page contenttype= "text/html;charset=gb2312"%>
<%request.setcharacterencoding ("gb2312");%>
< html>
< head>
< Title>request Object _ example 3</title>
< body bgcolor= "#FFFFF0" >
< form action= "" method= "POST" >
Good at:< input type= "checkbox" Name= "CB" value= "ON1" &GT;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>

JSP built-in object Response object
The Response object contains information about the response to a customer request, but it is rarely used directly in the JSP. It is an instance of the HttpServletResponse class.
Serial Number Method description
1 String getcharacterencoding () returns what character encoding is used for 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 Response head length
5 void setContentType (String type) sets the MIME type of the response
6 Sendredirect (java.lang.String location) requests to redirect clients
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.