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

Source: Internet
Author: User

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

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.