views:

1172

answers:

4

Is it possible to get the raw HTTP Request from the HttpServletRequest object? I want to see the raw request as a String if at all possible.

I need to get the full text of the request, in this case it's a POST request, so the URL doesn't help. It's also part of a multi-part form, so I can't just call the "getParameterNames()" or "getParameterValues()".

Thank you,

A: 

Sounds like you need a servlet filter. There is no standard way to handle multipart/form-data, so you'll have to take care to cache this data appropriately when wrapping the HttpServletRequest.

McDowell
Note: this answer is no longer accurate. Servlet 3.0 (JEE6) introduced support for `multipart/form-data`. http://download.oracle.com/javaee/6/api/javax/servlet/annotation/MultipartConfig.html
McDowell
+1  A: 

or if you can write some interceptor to convert the parameter names and values to string format object and set it in request context, or filter is a good idea too.

narup
+1  A: 

Well, it sounds like you are doing some sort of troubleshooting. Why not just drop the multi-part form component while you are looking at the raw form data. You can use the following JSP snippet to construct the form data.

<%
Enumeration en = request.getParameterNames();
String str = "";
while(en.hasMoreElements()){
   String paramName = (String)en.nextElement();
   String paramValue = request.getParameter(paramName);
   str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue);
}
if (str.length()>0)
   str = str.substring(1);
%>
Chris J
+1  A: 

You can read the raw HTTP request by doing:

ServletInputStream in = request.getInputStream();

and then use the regular readmethods of the InputStream.

Hope that helps.

monchote