-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Amit,
On 3/2/2009 9:58 PM, Amit Chandel wrote: > I would love to see an example of such a filter. All you need to do is wrap the HttpSession object with one that does your database access. Something like the filter below. There are a few things missing: 1. The implementation of the readStoredKeys, readStoredValue, writeStoredValue, removeStoredValue, and expungeSession methods. 2. Error handling. Presumably all of this will be done in the above methods. 3. Caching of session data. The session effectively contains no data at all; the database contains everything. (See below for a crucial problem with this example filter). 4. Stub methods for the HttpSession wrapper class that call _wrapped.foo() for each foo() method. 5. IMPORTANT NOTE: since this class assumes all the data is in the database and nothing will be stored in memory, the methods setAttribute and removeAttribute are never called on the wrapped HttpSession object. This has the undesirable effect of /not notifying any of the HttpSessionAttributeListeners or HttpSessionBindingListeners/. You can easily synthesize the calling of HttpSessionBindingListener methods, but not the HttpSessionAttributeListener methods (because the listeners are managed by the container). If you want these attribute listeners to be notified, you'll have to call _wrapped.setAttribute() (or whatever) which means that the data goes into your session: in memory. Enjoy, - -chris public class JDBCSessionFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { if(request instanceof HttpServletRequest) request = wrap((HttpServletRequest)request); chain.doFilter(request, response); } protected class JDBCRequestWrapper extends HttpServletRequestWrapper { JDBCRequestWrapper(HttpServletRequest request) { super(request); } // Override getSession methods to use our wrapper public HttpSession getSession(boolean create) { HttpSession session = super.getSession(create); if(null == session) return null; else return new JDBCSessionWrapper(session); } // Override this method, too, in case the underlying // implementation doesn't call getSession(boolean) public HttpSession getSession() { return this.getSession(true); } } protected class JDBCSessionWrapper implements HttpSession { private HttpSession _wrapped; JDBCSessionWrapper(HttpSession wrapped) { _wrapped = wrapped; } // Implementation here depends on how you want your // session to act. This implementation is a simple // write-through, non-caching one. public Enumeration getAttributeNames() { return this.readStoredKeys(getId()); } public Object getAttribute(String key) { // No data is kept in the session itself return this.readStoredValue(getId(), key); } public void setAttribute(String key, Object value) { // No data is kept in the session itself if(null == value) removeAttribute(key); else this.writeStoredValue(getId(), key, value); } public void removeAttribute(String key) { this.removeStoredValue(getId(), key); } // Remember to override getValueNames, getValue, // setValue, and removeValue to refer to the // above methods. public void invalidate() { _wrapped.invalidate(); this.expungeSession(getId()); } // Implement the remaining methods to simply // pass-through to the "_wrapped" member. } } -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkmtUV0ACgkQ9CaO5/Lv0PCMCACdF4yb9RQEyZSd/fvzmMZzCdPU WHsAnjtdcMswZclf/0Q/tvQBk0p1TeYJ =0xzb -----END PGP SIGNATURE----- --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org