Archive

Archive for the ‘Servlets’ Category

Http Servlet Debug Filter

October 27th, 2008 No comments

If you have programmed any considerably sized Java web application, chance that you wrote some debug statements to dump what’s coming over wire as request and going out as response. Next time you run into such requirement, well, save some typing and use this Http Servlet Debug Filter.

This is simple jar file which you need to add to the WEB-INF/lib folder and it logs the Servlet Request and Response details to the log file. Optionally (by specifying a parameter) it also returns the debug to the browser as commented html so it can be be seen via “View Source” browser command.

Here is the sample response from the filter.

<!--
================= Start of Http Servlet Request (968784) =================
-----------------------------------------------------------------
From and Time
-----------------------------------------------------------------
Time: Mon Oct 27 23:27:31 PDT 2008
IP: 127.0.0.1
Host: 127.0.0.1

-----------------------------------------------------------------
Servlet Values
-----------------------------------------------------------------
AuthType: null
CharacterEncoding: null
Class: org.apache.coyote.tomcat5.CoyoteRequestFacade
ContentLength: -1
ContentType: null
ContextPath: /hdf
Locale: en_US
Method: GET
PathInfo: null
PathTranslated: null
Protocol: HTTP/1.1
QueryString: hdf.debugToClient=true
RemoteUser: null
RequestedSessionId: 221e7c81176e8af317081378f143
RequestURI: /hdf/index.html
RequestURL: http://localhost:8080/hdf/index.html
Scheme: http
ServerName: localhost
ServerPort: 8080
ServletPath: /index.html
Session: org.apache.catalina.session.StandardSessionFacade@110f850
UserPrincipal: null

-----------------------------------------------------------------
Servlet Parameters
-----------------------------------------------------------------
hdf.debugToClient=true

-----------------------------------------------------------------
Servlet Attributes
-----------------------------------------------------------------
com.sun.enterprise.http.sessionTracker=org.apache.coyote.tomcat5.SessionTracker@87c801

-----------------------------------------------------------------
Session Details
-----------------------------------------------------------------
Id: 222bab8d43997024f4d6051542f5
CreationTime: 1225175251640
LastAccessedTime: 1225175251640
MaxInacctiveInterval: 1800
<no session attributes>

-----------------------------------------------------------------
Servlet Cookies
-----------------------------------------------------------------
name=JSESSIONID, value=221e7c81176e8af317081378f143, domain=null, path=null, maxAge=-1, version=0, secure=false, coment=null

-----------------------------------------------------------------
Servlet Locales
-----------------------------------------------------------------
en_US
en

-----------------------------------------------------------------
Raw Http Headers
-----------------------------------------------------------------
host=localhost:8080
user-agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-language=en-us,en;q=0.5
accept-encoding=gzip,deflate
accept-charset=ISO-8859-1,utf-8;q=0.7,*;q=0.7
keep-alive=300
connection=keep-alive
referer=http://localhost:8080/hdf/
cookie=JSESSIONID=221e7c81176e8af317081378f143
if-modified-since=Tue, 28 Oct 2008 06:26:35 GMT
if-none-match=W/"398-1225175195890"
cache-control=max-age=0
================= End of Http Servlet Request ===================
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

</head>
<body>
<h5>Debug <a href="index.html?hdf.debugToClient=true">page</a> to test the Http Servlet debug filter.  View source to see the details.</h5>
</body>
</html>

<!--
================= Start of Http Servlet Response (968784) =================
Time: Mon Oct 27 23:27:31 PDT 2008
Status: -1
CharacterEncoding: ISO-8859-1
ContentLength: 398
ContentType: text/html;charset=ISO-8859-1
Locale: en_US

-----------------------------------------------------------------
Session Details
-----------------------------------------------------------------
Id: 222bab8d43997024f4d6051542f5
CreationTime: 1225175251640
LastAccessedTime: 1225175251640
MaxInacctiveInterval: 1800
<no session attributes>

-----------------------------------------------------------------
Set Cookies
-----------------------------------------------------------------
<no cookies>

-----------------------------------------------------------------
Set Http Headers
-----------------------------------------------------------------
ETag=W/"398-1225175249921"
Last-Modified=Tue, 28 Oct 2008 06:27:29 GMT
================= End of Http Servlet Response ===================
-->

Installation

1. Download the jar file (here is the source)
2. Add the jar file to the WEB-INF/lib folder
3. Add the following filter mapping to your web.xml

	<filter>
		<filter-name>hdf</filter-name>
		<filter-class>com.brsanthu.hdf.HttpDebugFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>hdf</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

4. You can also download pre-packaged war file, which can be deployed and accessed via “http://localhost:8080/hdf/index.html?hdf.debugToClient” (change the server/port accordingly)

Attached source code is released as is without any warranty and you can use/modify the source as you required.

Let me know if you have any suggestions.