Printing All Request Parameters from HTTP request to std output


This isn't really a post, I am just writing a code to get all request parameters/attributes of a (HttpServletRequest) request object (almost every web developer must have tried this when in need).


Sometimes while coding for form-submit-handling, or ajax request i.e. any request made to server, at server end, we need to know what all different parameters we receive from request.
If this request was redirected/directed from some other jsp/servlet then we also would like to know if there is any request attribute present and if yes what all are those.


So, to get all the request (i.e. javax.servlet.http.HttpServletRequest) object's parameters and attributes -
Use this -
System.out.println("To out-put All the request-attributes received from request - ");

Enumeration enAttr = request.getAttributeNames(); 
while(enAttr.hasMoreElements()){
 String attributeName = (String)enAttr.nextElement();
 System.out.println("Attribute Name - "+attributeName+", Value - "+(request.getAttribute(attributeName)).toString());
}

System.out.println("To out-put All the request parameters received from request - ");

Enumeration enParams = request.getParameterNames(); 
while(enParams.hasMoreElements()){
 String paramName = (String)enParams.nextElement();
 System.out.println("Attribute Name - "+paramName+", Value - "+request.getParameter(paramName));
}


Copy-paste this code in any JSP / Servlet or a method where you have received request (i.e. javax.servlet.http.HttpServletRequest) as parameter in method.


Here is a sample code written by me to output the parameters to response -


File -Sample.html (makes a simple form request to sample.jsp)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>To Call JSP with some parameters</title>
</head>
<body>
<form action="sample.jsp" method="post">
Here is sample form - with few fields<br/>
TextField Param 1 - <input type="text" name = "nameTextField"/><br/>
Password Param 2 - <input type="password" name = "Password"/><br/><br/>
Radio Buttons Value gets sent only when its clicked, otherwise not.<br/> 
Radio Param 3 - <input type="radio" name = "radioButton"/><br/>
File Name Param 4 - <input type="file" name = "fileName"/><br/>
<!-- Below is hidden input tag its value will be always sent in request-->
<input type="hidden" name = "HiddenParam" value="hidden5"/>
<!--Input tag type --  text - password - checkbox - radio - submit - reset - file - hidden - image - button -->
<input type="submit" value = "Submit Values">
</form>
</body>
</html>

Which looks like this -

sample.jsp (which gets called on form submit)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import = "java.util.Enumeration;"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Getting All request parameters</title>
</head>
<body>
<%
System.out.println("To out-put All the request-attributes received from request - ");
Enumeration enAttr = request.getAttributeNames(); 
while(enAttr.hasMoreElements()){
 String attributeName = (String)enAttr.nextElement();
 System.out.println("Attribute Name - "+attributeName+", Value - "+(request.getAttribute(attributeName)).toString());
}

System.out.println("To out-put All the request parameters received from request - ");

Enumeration enParams = request.getParameterNames(); 
while(enParams.hasMoreElements()){
 String paramName = (String)enParams.nextElement();
 System.out.println("Attribute Name - "+paramName+", Value - "+request.getParameter(paramName));
}

System.out.println("Remote User Name - "+request.getRemoteUser());
System.out.println("Remote Address - "+request.getRemoteAddr());
System.out.println("Remote Host Name - "+request.getRemoteHost());

//Outputting HTML response
response.setContentType("text/html");
response.getWriter().print("<b>Values Submitted Are - <b/><br/>");
//Outputting all received parameteres - within bold tags
enParams = request.getParameterNames();int i=1; 
while(enParams.hasMoreElements()){
 String paramName = (String)enParams.nextElement();
        //outputting all received parameters
 response.getWriter().print("Param  - "+paramName+" --> Value - "+request.getParameter(paramName)+"<br/>");
}
%>
</body>
</html>

Output -
enjoy:-)

Comments

  1. Does the request.getAttributeNames() really work.

    I think there is no such method.

    ReplyDelete
    Replies
    1. Yes its there it just returns you, an Enumeration containing the names of the attributes available to the request.
      look at the docs -
      http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html
      http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getAttributeNames()

      Delete

Post a Comment

Popular posts from this blog

How to install / Configure Mantis For Apache-PHP-PostgreSQL combination

Modified ExtJS LOVCombo (List of Values) ux plugin - with select all, deselect all, text filter, bubble up and bubble down of selection

TriggerField with two triggers, TwinTriggerField with tooltips