When I submit a form using the POST method, how can I retrieve the posted parameters as a string (name/value pairs seperated by "&")? If the form is posted with the GET method, I can use request.QueryString method for this purpose.

Ryan Breidenbach

You can use request.getQueryString() for GET requests because the parameters are included in the URL:


However, for POST request, the parameters are never displayed in the URL. The easiest ways you can retreive these are by calling getParameter(), getParameterNames() and getParameterValues(). To make this easier, you may want to create a utility method that handles this logic for you and returns a String that has the same format as the query portion of a GET request's URL. Such a method may look like:

public String createQueryString(ServletRequest req) {
  StringBuffer buf = new StringBuffer();
  Enumeration name = req.getParameterNames();
  String curName = null;
  String values[] = null;
  while (names.hasMoreElements()) {
    curName = (String)names.nextElement();
    values = req.getParameterValues();
    for (int i = 0; i < values.length; i++) {
  String queryStr = buf.toString();
  // remove last ampersand!
  return queryStr.substr(0, queryStr.length() -1);
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact