Storing Request-specific Data Without Using ServletRequest

There are times when you need to store request-specific attributes (pieces of data that must be available to one specific http-request during the lifetime of that http-request) in order to retrieve them later.

This is easy?if you have access to the ServletRequest instance. You simply use its methods, getAttribute() and setAttribute(), for attribute storage.

But sometimes your code does not provide access to the current request’s ServletRequest instance. One way around this is to modify the parameter-list of methods to pass the ServletRequest instance all the way down to the method that needs the request-specific attributes. This would use the ServletRequest’s getAttribute() method. However, this refactoring is not always possible.

Another solution is to use the thread’s local memory, because each ServletRequest is executed in one thread only (it does not switch threads).

First, define a ThreadLocal class and a class holding your data:

class RequestCache extends ThreadLocal{    protected Object initialValue()    {        return new RequestCacheEntry();    }}class RequestCacheEntry{    HttpServletReqeust mRequest;    // ... or any other member you'd like to store in this class.}

Then, modify your servlet to set the request-specific data and a public method to retrieve it:

public class MyServlet extends HttpServlet {    ...    private static final RequestCache smReqCache = new RequestCache();    public static RequestCacheEntry getCurrentRequestData()    {        return (RequestCacheEntry)smReqCache.get();    }    ...}

Next, add these pieces of code in your servlet’s doGet and/or doPost methods:

public void doGet(HttpServletRequest pRequest, HttpServletResponse pResponse){    RequestCacheEntry reqCacheEntry = (RequestCacheEntry)smReqCache.get();    reqCacheEntry.mRequest = pRequest;        ..    .. do all your servlet stuff :)    reqCacheEntry.mRequest = null;}

You can call MyServlet.getCurrentRequestData() to obtain your request-specific data anywhere in your servlet-code where you don’t have access to the current HttpServletRequest instance.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job