• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extending Servlet , Caching in init()

 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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 ]
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!