javascript
Brief description  about Online courses   join in Online courses
View pavani chamala reddy 's Profile

servlet api

1. How to get the list of all the request parameters?
Asked by pavani chamala reddy | Mar 3, 2011 |  Reply now
Replies (1)
View Java Teacher 's Profile
for getting all the request parameters you can use the following code............


// This method is called by the servlet container to process a GET request.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doGetOrPost(req, resp);
}

// This method is called by the servlet container to process a POST request.
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doGetOrPost(req, resp);
}

// This method handles both GET and POST requests.
private void doGetOrPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Get the value of a request parameter; the name is case-sensitive
String name = "param";
String value = req.getParameter(name);
if (value == null) {
// The request parameter 'param' was not present in the query string
// e.g. http://hostname.com?a=b
} else if ("".equals(value)) {
// The request parameter 'param' was present in the query string but has no value
// e.g. http://hostname.com?param=&a=b
}

// The following generates a page showing all the request parameters
PrintWriter out = resp.getWriter();
resp.setContentType("text/plain");

// Get the values of all request parameters
Enumeration enum = req.getParameterNames();
for (; enum.hasMoreElements(); ) {
// Get the name of the request parameter
name = (String)enum.nextElement();
out.println(name);

// Get the value of the request parameter
value = req.getParameter(name);

// If the request parameter can appear more than once in the query string, get all values
String[] values = req.getParameterValues(name);

for (int i=0; i<values.length; i++) {
out.println(" "+values[i]);
}
}
out.close();
}
Apr 15, 2011