JSP built in objects: simple introduction and use of Request and Response

  • 2020-06-01 10:47:13
  • OfStack


request objects for JSP built-in objects
The client's request information is encapsulated in an request object, through which the client's needs can be understood and then responded to. It is an instance of the HttpServletRequest class.
The number is specified by the method
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
4 int getContentLength() returns the length of the request body (in bytes)
5 String getContentType() gets the MIME type of the request body
6 ServletInputStream getInputStream() gets a binary stream of 1 line in the body of the request
7 String getParameter(String name) returns the parameter value of the name specified parameter
8 Enumeration getParameterNames() returns an enumeration of available parameter names
9 String[] getParameterValues(String name) returns an array of all the values of the parameter name
10 String getProtocol() returns the requested protocol type and version number
11 String getScheme() returns the requested plan name, http.https, ftp, etc
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 this request
14 BufferedReader getReader() returns the decoded body of the request
15 String getRemoteAddr() returns the client IP address that sent this request
16 String getRemoteHost() returns the client host name that sent this request
17 void setAttribute(String key,Object obj) sets the property value of the property
18 String getRealPath(String path) returns the real path of the 1 virtual path

< %@ page contentType="text/html;charset=gb2312"%> 
< %request.setCharacterEncoding("gb2312");%> 
< html> 
< head> 
< title>request object _ case 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> 
 Of the requested server IP : < %=request.getServerName()%>< br> 
 Port of the request server: < %=request.getServerPort()%>< br> 
 The client IP Address: < %=request.getRemoteAddr()%>< br> 
 Client host name: < %=request.getRemoteHost()%>< br> 
 The value submitted from the 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 _ case 2< /title> 
< /head> 
< body bgcolor="#FFFFF0"> 
< form action="" method="post"> 
 User name: < input type="text" name="username">    
 The secret   Code: < input type="text" name="userpass">    
< input type="submit" value=" Enter the " > 
< /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 _ case 3< /title> 
< /head> 
< body bgcolor="#FFFFF0"> 
< form action="" method="post"> 
 Specialize in: < 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 the " 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> 

JSP built-in objects for response objects
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.
The number is specified by the method
1 String getCharacterEncoding() returns what character encoding the response is encoded with
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) redirects the client's request

Related articles: