1

Short version: How can I access to HttpRequest object from a POJO code that is called by a Command/JSP which is executed by WebContainer?

  • POJO is not aware of the CommandContext or HttpRequest (or its cousins).
  • POJO is pretty deep down in the execution stack, so changing the method signature means changing all the parent method signatures and the places these methods are called.

I did check a similar post as well (my POJO is called by the WebContainer and there has to be a way to access to the request somehow without going thru the hoops like this): Retrieving Web Session from a POJO Outside the Web Container

Long version: I have been trying to find this needle in a haystack: trying to figure out a way to access to the HttpRequest object (or its cousins) in WebSphere application server (Commerce actually) by means of going thru the current WebContainer thread (or any other means, similar to getting the Transaction via TransactionManager).

Knowing that these Container threads are tied to a single executing page/command/etc I was wondering if there is a way of doing this without employing WorkArea, ThredLocal style not so desired approaches?

The issue we are facing is that, there had been many pieces of code written over the years that had not cared about the storeId or langId. So, rather than fixing all these pieces of code, we would like to access to Session context somehow (with HttpRequest object) so we can get the CommandContext and/or other structures to find out our storeId and langId and other data tied to the session.

Any further details required, please feel free to ask.

One example pseudo-code snippet:

public class MyLittleHelper {

. . . 

void unawareMethod() {
  // I know it is really bad practice not to pass the actual objects around...  
  // But hear me out for a sec... 
  // This could be the easiest way of changing things, however bad it looks. 

  // my storeId and langId unaware method now needs to access to storeId and langId

}

. . .
} // end of class

Any help would be greatly appreciated.

Community
  • 1
  • 1
GunerE
  • 11
  • 3
  • I apologize for the confusion, but you don't want to use ThreadLocal? Or you're asking if that's an acceptable approach? – dbreaux Jan 31 '12 at 22:43
  • I understand the confusion, it is just that WebSphere is not so good at using ThreadLocal considering the nature of the Thread-pooling. IBM does suggest, however, WorkArea, but I am trying to figure out if there is a way to access to a Static helper method somewhere to find my way to the current http request that is attached to the current serving Web Container thread. – GunerE Feb 01 '12 at 16:40
  • I'm intrigued now. Do you have a link to an official statement about ThreadLocal under WebSphere? It's not like a single request will bounce around between threads, is it? We've definitely used ThreadLocal under WebSphere. – dbreaux Feb 02 '12 at 15:06
  • The statement is not directly from IBM actually, though Out of Memory issues rooted in ThreadLocal usage seems to be a general issue. I am thinking of using ThreadLocal as WorkArea seems to be an overkill... Still though I would like to see a way to access, say, the http request object using the WebContainer somehow. Can't find any way to do it still. Here is a reference (non-IBM): http://www.devwebsphere.com/devwebsphere/2005/06/dont_use_thread.html – GunerE Feb 02 '12 at 19:09
  • The author of that article, Billy Newport, is definitely sharp and knows what he's talking about. On the other hand, that article is very old and even refers to coming Java changes that are now long past. His warnings are sound, but it seems to me that you can safely use ThreadLocal if you don't expect values to be there between requests and if you clean out values before your request is finished. – dbreaux Feb 02 '12 at 21:13

0 Answers0