The HttpServletRequest object represents the client's request. When the client accesses the server through the HTTP protocol, all the information in the HTTP request header is encapsulated in this object. Developers can obtain the client's information through the methods of this object.
Common request methods :
1. Common methods to obtain client environment information :
1. The getRequestURL method returns the complete URL when the client makes the request.
2. The getRequestURI method returns the resource name part in the request line.
3. The getQueryString method returns the parameter part of the request line.
4. The getRemoteAddr method returns the IP address of the client that made the request.
5. The getRemoteHost method returns the complete host name of the client that made the request.
6. The getRemotePort method returns the network port number used by the client.
7. The getLocalAddr method returns the IP address of the WEB server.
8. The getLocalName method returns the host name of the WEB server.
9.getMethod gets the client request method.
Copy the code code as follows:
package com.yyz.request;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
*URL is a subset of URI.
* URI is used to identify a resource.
*URL is used to identify a resource on the Internet.
*/
System.out.println(request.getRequestURL());//Get the request URL address
System.out.println(request.getRequestURI());//Get the requested resource
System.out.println(request.getQueryString());
System.out.println(request.getRemoteAddr());//Get the visitor IP
System.out.println(request.getRemoteHost());
//Since it is not registered on dns, the print result is still 127.0.0.1. If Baidu accesses this program, print www.baidu.com
System.out.println(request.getRemotePort());//Get the requested resource
System.out.println(request.getMethod());//Get the requested resource
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Enter: http://localhost:8080/test/servlet/RequestDemo in the browser address bar and the background output is as follows:
2. Obtain the client request header :
1.getHeader(String name) method
2.getHeaders(String name) method
3.getHeaderNames() method
Copy the code code as follows:
package com.yyz.request;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//Get request headers
public class RequestDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String head = request.getHeader("Accept-Encoding");
//Some browsers do not support compressed data
if(head.contains("gzip")){
//Output compressed data
}else{
//Output uncompressed data
}
//Get all values of the header with the specified name
Enumeration e = request.getHeaders("Accept-Encoding");
while(e.hasMoreElements()){
String value = (String ) e.nextElement();
System.out.println(value);
}
System.out.println("------------------------");
//Get all headers and values
e = request.getHeaderNames();
while(e.hasMoreElements()){
String name = (String)e.nextElement();
String value = request.getHeader(name);
System.out.println(name+"="+value);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Access the program in a browser and the background output is as follows:
3. Obtain the client request parameters (data submitted by the client):
1.getParameter(name) method
2.getParameterValues(String name) method
3.getParameterNames method
4.getParameterMap method, used extensively when making frameworks
package com.yyz.request; import java.io.IOException; import java.util.Enumeration; import java.util.Map; import java.util.Map.Entry; import javax.servlet.ServletException; import javax.servlet.http .HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //Get request data public class RequestDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //http://localhost:8080/test/servlet/RequestDemo? name=yyz String value = request.getParameter("name"); System.out .println(value); //http://localhost:8080/test/servlet/RequestDemo?like=sing&like=dance String likes[] = request.getParameterValues("like"); if(likes!=null ){ for(String like : likes){ System.out.println(like); } } //The following is a professional way of writing this kind of traversal, a way to obtain array data (to avoid null pointer exceptions) for(int i=0;likes!=null&&i<likes.length;i++){ System.out.println(likes[ i]); } //Get all names and get the value based on the name Enumeration e = request.getParameterNames(); while(e.hasMoreElements()){ String name = (String) e.nextElement(); value = request.getParameter(name); System.out.println(name+"="+value); } System.out.println("------------- ---------"); //Get the Map collection used to encapsulate data in the request object Map<String,String[]> map = request.getParameterMap(); for(Entry<String, String[] > me : map.entrySet()){ String name = me.getKey(); String [] v = me.getValue(); System.out.println(name+"="+v[0]); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }