Summary of common methods of request in Java _java

Source: Internet
Author: User

The HttpServletRequest object represents the client's request, and when the client accesses the server through the HTTP protocol, all the information in the HTTP request header is encapsulated in the object, and the developer can get the customer's information through the object's method.

Request Common Methods :

a common way to obtain client environment information :

The 1.getRequestURL method returns the full URL of the client when the request is made.

The 2.getRequestURI method returns the resource name portion of the request line.

The 3.getQueryString method returns the parameter portion of the request line.

The 4.getRemoteAddr method returns the IP address of the client that made the request.

The 5.getRemoteHost method returns the full host name of the client that made the request.

The 6.getRemotePort method returns the network port number used by the client.

The 7.getLocalAddr method returns the IP address of the Web server.

The 8.getLocalName method returns the host name of the Web server.

9.getMethod get client request mode.

Copy 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 {
/**
* The URL is a subset of the URI.
* URI is used to identify a resource.
* URLs are used to identify a resource on the Internet.
*/
System.out.println (Request.getrequesturl ())//Get Request URL address
System.out.println (Request.getrequesturi ())//Get the requested resource
System.out.println (Request.getquerystring ());
System.out.println (REQUEST.GETREMOTEADDR ())//Get the caller IP
System.out.println (Request.getremotehost ());
Because it is not registered in DNS so the print result is 127.0.0.1, if it is Baidu Access this program, then 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);
}

}

In the browser address bar input: Http://localhost:8080/test/servlet/RequestDemo after the background output into the following figure:


second, obtain client request header :

1.getHeader (String name) method

2.getHeaders (String name) method

3.getHeaderNames () method

Copy 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 does not compress data
}
Gets all the values for the header of 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 the 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);
}

}

In the browser to access the program, the background output is as follows:

Obtain client request parameters (data submitted by client):

1.getParameter (name) method

2.getParameterValues (String name) method

3.getParameterNames method

4.getParameterMap method, do a lot of use when frame

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, HTTPSERVLETRESPO 
     NSE 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; }//Below is the professional way to do this traversal, to get the array data (avoid null pointer exceptions) for (int i=0;likes!=null&&i<likes.length;i++) {Syst Em. OUT.PRINTLN (Likes[i]);
     //Get all the 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 set map<string,string[]> map = Request.getparametermap () to encapsulate the data in the Request object;
       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, Ioex
 ception {doget (request,response);
 }
 }

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.