JSP built in object: brief introduction and use of request and response

Time:2021-4-17

Request object of JSP built-in object
The client’s request information is encapsulated in the request object, through which we can understand the customer’s needs, and then make a response. 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 an 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() gets the MIME type of the request body
6. Servlet InputStream getinputstream() gets the binary stream of one line in the request body
7 string getparameter (string name) returns the parameter value of the parameter specified by name
8 enumeration getparameternames() returns an enumeration of available parameter names
9 string [] getparametervalues (string name) returns an array containing all the values of the parameter name
10 string getprotocol() returns the protocol type and version number for the request
11 string getscheme() returns the plan name for the request, such as: http.https And FTP
12 string getservername() returns the host name of the server that accepted 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 that sent the request
16 string getremotehost() returns the host name of the client that sent the request
17 void setAttribute (string key, object obj) sets the attribute value of the attribute
18 string getrealpath (string path) returns the real 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>
Requested protocol: <%= request.getProtocol ()%>< br>
Requested file name: <%= request.getServletPath ()%>< br>
IP of the requested server: <%= request.getServerName ()%>< br>
Port of request server: <%= request.getServerPort ()%>< br>
Client IP address: <%= request.getRemoteAddr ()%>< br>
Client host name: <%= request.getRemoteHost ()%>< br>
Value submitted from form: <%= 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”>
User name: < input type = “text” name = “user name” >
Password:
< 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: 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 responding to customer requests, but it is rarely used directly in JSP. It is an instance of the httpservletresponse class.
Serial number method description
1 string getcharacterencoding() returns which character encoding is applied
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) sets the response header length
5 void setcontenttype (string type) sets the MIME type of the response
6 sendRedirect( java.lang.String Location) to redirect the client’s request

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]