JSP request object

A request object definition
The client's request information is encapsulated in the request object, through which it is possible to understand the client's needs and then respond. It is an instance of the HttpServletRequest class. The request object has a request field, that is, the object is valid until the client's request is completed.
 
Two request object methods


 


 
 
Three examples
<%@ page language = "java" import = "java.util.*" contentType = "text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'index.jsp' starting page </ title >
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
         <!--
        <link rel =" stylesheet " type="text/ css " href ="styles.css">
        -->
  </ head >
 
  < body >
    < h1 > request built-in object </ h1 >
    <%
       request.setCharacterEncoding( "utf-8" ); //Solve the problem of Chinese garbled characters, and cannot solve the problem of garbled characters in URL transmission in Chinese.
       request.setAttribute( "password" , "123456" );
   
    %>
        用户名: <%= request.getParameter( "username" ) %> < br >   
        Hobbies: <%
           if (request.getParameterValues( "favorite" )!= null )
           {
                   String[] favorites = request.getParameterValues( "favorite" );
                   for ( int i=0;i<favorites.length;i++)
                   {
                      out.println(favorites[i]+ "  " );
                   }
                }
        %> < br >
         密码: <%= request.getAttribute( "password" ) %> < br >
         MIME type of the request body: <%= request.getContentType() %> < br >
         Protocol type and version number:  <%= request.getProtocol() %> < br >
         Server hostname: <%= request.getServerName() %> < br >
         Server port number: <%= request.getServerPort() %> < BR >
         The length of the requested file: <%= request.getContentLength() %> < BR >
         IP address of the requesting client: <%= request.getRemoteAddr() %> < BR >
         The real path of the request: <%= request. getRealPath( "request.jsp" ) %> < br >
         Request context path: <%= request.getContextPath() %> < BR >                          
  </ body >
</ html >
 
Four running effects


 
 
Five little knowledge points
1. Solve the problem of garbled Chinese parameters in URL transmission
Modify server.xml in D:\apache-tomcat-7.0.81\conf
    <Connector port="8888" protocol="HTTP/1.1"
               connectionTimeout="20000"

 

               redirectPort="8443" URIEncoding=" utf-8 "/>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326757142&siteId=291194637