|
Coding the Servlet Filter
You may choose, for example, to store browser information and the CPU time used in your filter. If you have access to data from a logon process, it would be natural to store this in the filter as well (Listing 3).
Coding the Servlet
In the servlet class (a Struts Action class), you can also store the CPU time and call the logging system, shown in Listing 4 ( click here for the complete program).
Coding the Log Formatter
This example uses the JDK 1.4 logging system, and if you add your own "formatter" to it, it's utterly simple to fetch the data from the ThreadLocal object and write it out with the other logging data:
package hansen.playground;
. . .
public class MyFormatter extends Formatter {
public String format(LogRecord record) {
String myData = MyThreadLocal.getFormatted();
return ... formatted data from "record" and "myData" ...
}
}
Coding the error.jsp Page
Finally, if you have an error page (error.jsp), you may print the contents of the ThreadLocal object. Like this:
<%@ page language="java" %>
<%@ page import="hansen.playground.*" %>
<h3>In error.jsp</h3>
<%
out.println("<pre>"+MyThreadLocal.getFormatted()+"</pre>");
%>
A Word of Warning
There are a couple of things you'll need to keep in mind when you're using ThreadLocals.
First, be careful to initialize the ThreadLocal object whenever the thread starts working, or you might end up connecting a logged-off user's data to another user's thread. When you're using ThreadLocals in a situation in which a filter initializes the object with user data, make sure there are no other entries to your servlet applicationexcept through the filter.
Second, always remember that the ThreadLocal data is connected to the thread, not the user or anything else. A good idea is to delete the ThreadLocal data when it's no longer used. This addition to the filter class accomplishes that task:
. . .
// Go to the servlet
chain.doFilter(request, response);
MyThreadLocal.listData();
MyThreadLocal.set(null);
. . .
Whenever several non-related components running in the same thread need to share data, the ThreadLocal class is a viable solution. It's very simple to use, if you keep in mind that proper initialization and clean-up of local data is essential to avoid mixing data from different unit-of-works.
Other Resources
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|