Take control of the servlet environment, Part 1

Invisibly extend the functionality of the servlet API

1 2 Page 2
Page 2 of 2
public class BootStrap extends net.rudiment.servlet.HttpServlet
{
    public net.rudiment.servlet.ResponseWrapper wrapResponse(
HttpServletRequest request, HttpServletResponse response )
    {
        return( new net.rudiment.servlet.cookie.ResponseWrapper( request,
response ) );
    }
    public net.rudiment.servlet.RequestWrapper wrapRequest(
HttpServletRequest request, net.rudiment.servlet.ResponseWrapper response
)
    {
        return(
            new net.rudiment.servlet.cookie.RequestWrapper(
                request,
                response,
                factory
            )
        );
    }
    private static final net.rudiment.servlet.SessionWrapperFactory
factory =
        new SessionWrapperFactory()
        {
            public net.rudiment.servlet.SessionWrapper wrapSession(
net.rudiment.servlet.RequestWrapper request,
net.rudiment.servlet.ResponseWrapper response, HttpSession session )
            {
                return(
                    new
net.rudiment.servlet.session.database.SessionWrapper( request, response,
session )
                );
            }
        };
}

The wrapResponse() method instantiates the RequestWrapper from the cookie subpackage. The wrapRequest() method completes the same. The SessionWrapperFactory instance is an anonymous inner class; since it's a stateless object, you don't need to create a new one each time the servlet executes (wasting memory and clock ticks), so we designated it a static final member.

The framework is now in place. You've configured your bootstrap. Your last step is to extend the bootstrap servlet instead of the RSEF version, like so:

public class MyServlet extends BootStrap

And you're ready to go! It couldn't possibly be simpler ... or could it? Stay tuned to find out!

Conclusion

The framework we introduced here will open the doors to a multitude of performance and scalability enhancements -- completely transparent to your existing servlet-based project. In the next installment, we will discuss session management and touch on database storage. Part 3 will focus on the cookie-domain bug. Then you will begin to see the real power of RSEF.

Thomas E. Davis is a Sun-certified Java developer and the chief technology officer of his second successful Internet-related company. In addition to being a Java advocate, Thomas is a strong proponent of the extreme programming, design patterns, and refactoring philosophies, which he preaches to his colleagues and supports within his own department. Thomas welcomes constructive criticism and intelligent comments on his articles. Craig Walker is a Sun-certified Java programmer (awaiting the results of his developer exam) and senior software engineer. A former consultant for IBM who worked on that company's internal Java initiatives, Craig has pursued the mastery of many Java APIs ranging from the presentation layer to the enterprise and everything in between.

Learn more about this topic

  • Books

1 2 Page 2
Page 2 of 2