How Java implements URLs with request parameters (Get/post) and methods for getting get and POST request URLs and parameter lists _java

Source: Internet
Author: User
Tags readline

The

specific code looks like this:

public static string Sendget (String url,string param) {string result = ' "; try{String urlname = URL + "?"
 +param;//url U = new URL (urlname);
 URLConnection connection = U.openconnection ();
 Connection.connect ();
 BufferedReader in = new BufferedReader (New InputStreamReader (Connection.getinputstream ()));
 String Line;
 while (line = In.readline ())!= null) {result + = line; 
 } in.close (); }catch (Exception e) {System.out.println ("helloword!!
 "+e);
 return result;
 public static string Sendpost (String url,string param) {string result= "";
 try{url httpurl = new URL (URL);  
 HttpURLConnection httpconn = (httpurlconnection) httpurl.openconnection ();
 Httpconn.setdooutput (TRUE);
 Httpconn.setdoinput (TRUE);
 PrintWriter out = new PrintWriter (Httpconn.getoutputstream ());
 Out.print (param);
 Out.flush ();
 Out.close ();
 BufferedReader in = new BufferedReader (New InputStreamReader (Httpconn.getinputstream ()));
 String Line; while (line = In.readline ())!= null) {result = = Line
 } in.close (); }catch (Exception e) {System.out.println ("helloword!
 "+e);
 return result; }

Here is a description of how Java gets get and POST request URLs and parameter lists

Get requests in the servlet can obtain the complete request path and request all parameter lists through the HttpServletRequest Getrequesturl method and GetQueryString (), the need for post Getparametermap () method traversal, either get or post can be getrequesturl+getparametermap () to obtain the full path of the request

Package Com.zuidaima import java.io.IOException; 
Import Java.io.PrintWriter; 
Import Java.util.Map; 
Import javax.servlet.ServletException; 
Import Javax.servlet.http.HttpServlet; 
Import Javax.servlet.http.HttpServletRequest; 
Import Javax.servlet.http.HttpServletResponse; 
 public class Getparams extends HttpServlet {private static final long serialversionuid = 1L; 
 Public Getparams () {super (); } protected void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexceptio 
  n {printwriter writer = response.getwriter (); 
  Writer.println ("Get" + request.getrequesturl () + "" + request.getquerystring ()); 
  map<string, string[]> params = Request.getparametermap (); 
  String querystring = ""; 
   For (String Key:params.keySet ()) {string[] values = Params.get (key); 
    for (int i = 0; i < values.length i++) {String value = Values[i]; 
   QueryString + = key + "=" + Value + "&"; } 
  } 
  // Remove the last space querystring = querystring.substring (0, Querystring.length ()-1); 
 Writer.println ("Get" + request.getrequesturl () + "" + querystring); } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcepti 
  on {printwriter writer = response.getwriter (); 
  map<string, string[]> params = Request.getparametermap (); 
  String querystring = ""; 
   For (String Key:params.keySet ()) {string[] values = Params.get (key); 
    for (int i = 0; i < values.length i++) {String value = Values[i]; 
   QueryString + = key + "=" + Value + "&"; 
  }///Remove the last space querystring = querystring.substring (0, Querystring.length ()-1); 
 Writer.println ("POST" + request.getrequesturl () + "" + querystring); } 
}

The above code is easy to understand, I hope to learn from the Java post get URL request parameters related methods to help, thank you all the time to the cloud-Habitat community website support, with your support, we will do better.

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.