Skip to main content

My suggestion for Servlet 3.0

Posted by felipeal on June 25, 2007 at 9:52 AM PDT

I have taught Java and web-tier (Servlets/JSP/taglibs) development to about 10 classes over the last 3 years. And everytime I teach servlet attributes, I face the same situation: first I explain what request attributes are, why they are useful, and which methods you use to manipulate them. Then, when I teach session, I have to explain that it has a similar concept, with the exact same methods. And when I get to the ServletContext topic - well, you got the picture...



Now, to make things worse, the Servlet API is taught a couple of weeks after the OO concepts, like interface. It's already hard to explain interfaces for people coming from non-OO backgrounds (you have to explain many concepts and analogies, like 'interfaces are contracts', 'an interface defines methods signatures that classes must implement', and so on...), and when they are finally understanding it, they face a situation where an interface would make sense, but it's not used.



So, putting A and B together, there should be an interface defining an object that handles user attributes, and both ServletRequest, HttpSession, ServletContext, and PageContext (from JSP) would implement it. Something like AttributeContainer (which is a bad name as container has zillions other meanings on Java EE), AttributeHolder, AttributeStore, etc (I honestly cannot think about a nice name for it).



This is a very simple change, as the classes already have the methods - it's just a matter of "making the contract official" by defining such interface. In fact, it's so simple that I believe the previous EG didn't think it would worth the effort (I had suggested it before, I believe in the 2.5 timframe) - they might think it doesn't bring enough value.



But I can guarantee it would help in at least 3 scenarios:

  1. Teaching attributes on Servlet courses
  2. Teaching interfaces (once you reach the attributes class, this is a good example of interface use)
  3. Provide an easy (although not necessarily more efficient) way to write code that store attribute in many different scopes (like JSTL set tag):
    
    
    public void set( String var, String scope, Object value ) {
      AttributeHolder holder = attHolders.getHolder( scope );
      if ( holder == null ) {
        throw new BlaBlaException( "invalid scope: " + scope );
      }
      holder.setAttribute( var, value );
    }
    
    



I know the EG has more serious issues to handle, but this is a minor one, which would not hurt anyone. Besides, this is a new major spec release (3.0), so that's a good time to include some change (it could even deprecate the method that return the attribute names in an Enumeration, and provide an additional method that uses a Set, for instance).


Related Topics >>