• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending Servlet , Caching in init()

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have 2 servlets. Let's say servletA and servletB

public servletA extends servletB {

public void init(ServletConfig servletConf) throws ServletException
{
super.init(servletConf);
}


public void doGet(req, res)
{
//something
try
{
myUtil.getEmployees();
}
catch(Exception ex)
{

}

}


}




public servletB extends HttpServlet{

public MyUtil myUtil;


public void init(ServletConfig servletConf) throws ServletException
{
super.init(servletConf);

myUtil = new MyUtil();

}

}


}


I expected the myUtil object to get created and get cached when servletA gets a request for the first time. What I see is that myUtil object gets created successfully in servletB but when doGet() is fired on servletA, it gives me NULL myUtil object and null pointer exception comes.


Assumption: myUtil object is for readonly.

Question1: Please help me in finding the cause of it. What is wrong?

Questions2: Is this strategy bad if all my servlets extend the same servlet (ServletB)?

Thanks in advance.

Vikas Aggarwal
[ September 13, 2006: Message edited by: Vikas Aggarwal ]
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I expected the myUtil object to get created and get cached when servletA gets a request for the first time.



You should have initialized it in ServletB's constructor (just like normal java classes) - but no, dont even think of instance variables in Servlet/Jsps. You would have to deal with concurrency issues.

Objects initialized in the init() method aren't cached in the config object. Did you read any literature that suggests so?

The proper way to pass on information (objects) across Servlets is to have them as scoped variables. Going by your post, since you are in a learning stage, I suggest you get some good book (HeadFirst is excellent) and begin reading up

ram.
 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ram for your inouts. I read these articles which talks about the caching in init() method and overriding it to improve performance. Am I doing wrong?

http://www.precisejava.com/javaperf/j2ee/Servlets.htm#Servlets102

http://edocs.bea.com/wls/docs70/servlet/progtasks.html#167964
[ September 13, 2006: Message edited by: Vikas Aggarwal ]
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies - I take my comments back (mostly knee jerk reaction when I skimmed your code and saw instance variables declared in your Servlet).

Your code works correctly for me (running jBoss 4.0)

ram.
 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problems Ram. This is great that it is working for you. Is there something extra I need to do for tomcat 5? I am using that?
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, jboss uses tomcat internally as its webserver. So it should work - for that reason, any server should run that code correctly.

You said you receive null & then a NPE, right? Your posting has an empty catch block that should have consumed the Exception. Perhaps it's some other portion of the code that's problematic?

ram.
 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK thanks Ram for your help. I will try to create a sample app and run it again. Let's see how it goes. But one thing is sure that it should work.

Thanks.
Vikas
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic