The Artima Developer Community
Sponsored Link

Java Buzz Forum
Servlet Filters as poor man's AOP

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Vinny Carpenter

Posts: 276
Nickname: vscarpente
Registered: Feb, 2003

Vinny is a Java developer/architect working with Java, J2EE, OO, Linux, OpenSource.
Servlet Filters as poor man's AOP Posted: Mar 25, 2004 6:43 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Vinny Carpenter.
Original Post: Servlet Filters as poor man's AOP
Feed Title: Vinny Carpenter's Blog
Feed URL: http://www.j2eegeek.com/error.html
Feed Description: Welcome to my blog. I am a total Java geek that lives in Milwaukee, making my living as an architect/developer, spending all my time with Java, J2EE, OO, Linux, and open source. In my spare time, when I am not in front of my computers, I spend every other minute with my other loves: My wife, books, music, guitars, Formula-1 racing and StarGate. Check out my blog @ http://www.j2eegeek.com/blog
Latest Java Buzz Posts
Latest Java Buzz Posts by Vinny Carpenter
Latest Posts From Vinny Carpenter's Blog

Advertisement

I was just discussing this idea of Servlet Filters with a friend and I was trying to explain to him how Filters work and how they are really aspects in the AOP world. I think filters are really incredibly helpful and yet very few developers know about them and even fewer developers implement them. So my thought was that if we started using buzzwords like AOP around filters, suddenly Filters become sexy and everyone's jumping over to implement Filters. :-)

The filter API was introduced in the Servlet 2.3 specification and is defined by the Filter, FilterChain, and FilterConfig interfaces in the javax.servlet package. You define a filter by implementing the Filter interface. A filter chain, passed to a filter by the container, provides a mechanism for invoking a series of filters. A filter config contains initialization data. The most important method in the Filter interface is the doFilter() method, which is the heart of the filter. Filters intercept request to your web application based on the url-pattern specified in the web.xml, where the filters are defined.

I have used Filters extensively and have a few Filters ready to go when I am called into debug applications in production that are misbehaving or just broken. One of the filters I use quite often is a simple authentication filter that makes is easy to ensure consumers of the web application is authenticated. Here's a simple snippet:


public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {

if (req != null) {
HttpServletRequest request = (HttpServletRequest) req;
//could pass in false in the getSession() to return null for new session.
HttpSession mySession = request.getSession();
String loginStatus = (String) mySession.getAttribute(
"LOGIN");

if ((loginStatus != null) && (loginStatus.equals(Boolean.TRUE.toString()))) {
log.debug(
"FOUND A LOGGED IN USER - PASSING THRU");
//Logged in - Let's pass thru the user
chain.doFilter(req, res);
}
else {
log.debug(
"FOUND A NEW USER - CHECKING STATUS");
if ((request.getRequestURI().indexOf("login") != -1) ||
(request.getRequestURI().indexOf(
"index.jsp") != -1) ||
(request.getRequestURI().indexOf(
"images") != -1) ||
(request.getRequestURI().indexOf(
"ipo.css") != -1)) {
//User is going to or being redirected to login page or loading images - Let's pass thru the user
log.debug(
"NEW USER -> LOADING CSS, IMAEGS or BEING REDIRECTED TO THE INDEX OR LOGIN PAGE");
log.debug(
"request.getRequestURI() = " + request.getRequestURI());
chain.doFilter(req, res);
}
else {
log.debug(
"NEW USER - LET's FORWARD TO THE INDEX JSP AGE");
log.debug(
"request.getRequestURI() = " + request.getRequestURI());
RequestDispatcher ds = ctx.getRequestDispatcher(
"/index.jsp?timeout=true");
ds.forward(request, res);
}
}
}
}



Here's a copy of the original documented Filter java class.

Read: Servlet Filters as poor man's AOP

Topic: SeaJUG: Groovy Previous Topic   Next Topic Topic: Halfway Between the Gutter and the Stars

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use