1

I have a problem. Lets say there are 2 servlets: Load() and Process(). During Load(), I want to create and initialize some objects. and During Process(), I want to use those objects for some other things.

Since there is no main class in a servlet (as opposed to desktop programming), I don't think that I can return object created by Load() and pass it as argument to Process() from the main class.

So, how could I create an object in during one servlet call and use/access that object from other servlet?

Bikash Gyawali
  • 969
  • 2
  • 15
  • 33
  • Search for "inter-servlet communication" – PeterMmm Mar 11 '11 at 07:32
  • You're looking at servlets from a wrong point of view. I'd suggest to read [this answer](http://stackoverflow.com/questions/3106452/java-servlet-instantiation-and-session-variables/3106909#3106909) and then rethink once again. – BalusC Mar 11 '11 at 11:35
  • @BalusC I read the link that you gave. I am using things in a **threadsafe** way as you described in that answer (of your's). Thanks for that, really nice expalnation. And I am using the HttpSession (as described by @Bozho below) for storing user-defined objects into session. I think it is fine. Why do you say that I am looking at servlets from wrong point of view? – Bikash Gyawali Mar 11 '11 at 12:12
  • The comparison with desktop programming has really no grounds. – BalusC Mar 11 '11 at 12:14
  • I agree. Maybe I didn't write things accurately. – Bikash Gyawali Mar 11 '11 at 12:17

2 Answers2

3

Use the ServletContext: getServletContext().setAttribute(..)

Also, consider placing the initialization code and the processing code in one servlet. If you are only having init() in one of them, and doGet() in the other, and these objects should be shared only between these two servlets, there is no point of this separation.

Update: if you want to reuse objects in successive requests by the same user (i.e. not initialize them once and use them everywhere), than instead of putting them in the ServletContext, put them in a smaller scope - the HttpSession (obtained by request.getSession())

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I am following this tutorial http://docstore.mik.ua/orelly/java-ent/servlet/ch11_03.htm What do you say about it? – Bikash Gyawali Mar 11 '11 at 08:19
  • 2
    @Bikash horrible. It's doing all kinds of possible hacks, instead of using the `ServletContext` which is _meant_ to share data between servlets. – Bozho Mar 11 '11 at 08:21
  • So far, I see that ServletContext is used for defining global variables at design time and can be accessed by various servlets at runtime. However, my criteria is a bit different. I need to create an object of a class(from an external jar attached to my web project) during the call of one of the servlet(/Load) and I need to access that object during the call of another servlet(/Process). Is ServletContext still good? – Bikash Gyawali Mar 11 '11 at 09:42
  • @Bikash - that's a rather different scenario the way you put it now. Still, those approaches there are incorrect. See my update. – Bozho Mar 11 '11 at 09:49
0

Not sure i understand what you mean with Load() and Process(). Servlets are no functions. They are mapped to a certain URL and their service() funktion gets called by the servlet container. More than one servlet can be mapped to an URL and they are called in the order they are defined in the web.xml.

To anser your question: state is normaly stored the the Session object via setAttribute()

You can than access it in the other Servlet via getAttribute().

bert
  • 7,566
  • 3
  • 31
  • 47
  • I didn't say that servlets were functions. I mean I wrote two servlest for myslef: Load and Process and of course, I have mapped them in web.xml. As pointed out by @PeterMmm, I think I found the term I was looking to google for - "inter-servlet communication". Thanks. – Bikash Gyawali Mar 11 '11 at 07:44