How Java obtains the Get and POST request URLs and parameter lists

Source: Internet
Author: User

Reprint: http://blog.csdn.net/yaerfeng/article/details/18942739

The GET request in the servlet can be obtained through the HttpServletRequest Getrequesturl method and GetQueryString () to obtain the complete request path and request all parameter list, post need Getparametermap () method traversal, whether get or post can be getrequesturl+getparametermap () to get the request full path

[Java]View Plaincopyprint?
  1. Package Com.zuidaima
  2. Import java.io.IOException;
  3. Import Java.io.PrintWriter;
  4. Import Java.util.Map;
  5. Import javax.servlet.ServletException;
  6. Import Javax.servlet.http.HttpServlet;
  7. Import Javax.servlet.http.HttpServletRequest;
  8. Import Javax.servlet.http.HttpServletResponse;
  9. Public class Getparams extends HttpServlet {
  10. private Static final long serialversionuid = 1L;
  11. Public Getparams () {
  12. super ();
  13. }
  14. protected void doget (HttpServletRequest request,
  15. HttpServletResponse response) throws Servletexception, IOException {
  16. PrintWriter writer = Response.getwriter ();
  17. Writer.println ("GET" + request.getrequesturl () + ""
  18. + request.getquerystring ());
  19. map<string, string[]> params = Request.getparametermap ();
  20. String queryString = "";
  21. For (String Key:params.keySet ()) {
  22. String[] values = Params.get (key);
  23. For (int i = 0; i < values.length; i++) {
  24. String value = Values[i];
  25. QueryString + = key + " =" + Value + "&";
  26. }
  27. }
  28. //Remove the last space
  29. queryString = querystring.substring (0, Querystring.length ()- 1);
  30. Writer.println ("GET" + request.getrequesturl () + "" + queryString);
  31. }
  32. protected void DoPost (HttpServletRequest request,
  33. HttpServletResponse response) throws Servletexception, IOException {
  34. PrintWriter writer = Response.getwriter ();
  35. map<string, string[]> params = Request.getparametermap ();
  36. String queryString = "";
  37. For (String Key:params.keySet ()) {
  38. String[] values = Params.get (key);
  39. For (int i = 0; i < values.length; i++) {
  40. String value = Values[i];
  41. QueryString + = key + " =" + Value + "&";
  42. }
  43. }
  44. //Remove the last space
  45. queryString = querystring.substring (0, Querystring.length ()- 1);
  46. Writer.println ("POST" + request.getrequesturl () + "" + queryString);
  47. }
  48. }

The put request gets no parameters because the put request parameter is not stored in the HTTP Header, but in the HTTP Body, so the parameter cannot be obtained through the Request.getparameter () method.

If you want to get the parameters of a PUT request, you can refer to this code as follows:

?
123456 String queryString = "";BufferedReader reader = newBufferedReader(new InputStreamReader(request.getInputStream()));String line;while((line = reader.readLine()) != null) {    queryString += line;}

Get the queryString, it is a "a=1&b=2" format string, then can be easily converted into a Map, and finally through the Map.get () method to get the specified parameters.

How Java obtains the Get and POST request URLs and parameter lists

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.