Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP extending Servlet

 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is it possible for jsp to extend a servlet? I have following scenario:

I have a servlet which initialises some static objects in its init method. All my servlets extend this servlet to access those variables. I have few JSPs which also require same static objects. So is it possible to extend those JSP to my parent servlet and read those variables?

Thanks.

Vikas Aggarwal
 
Balasubramani Dharmalingam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is possible for JSP to extend any class(not only servlet) by using "extends" attribute of page directive.

For example:
<%@ page extends="org.bala.HelloWorldServlet" %>
 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bala,

One more question related to this. So when for the first time the JSP will be hit, is server going to fire jspInit() as well or just the init method of my servlet?

Thanks,
Vikas
[ October 13, 2006: Message edited by: Vikas Aggarwal ]
 
Balasubramani Dharmalingam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Vikas,
There is some problem when the jsp extends Servlet, when you invoke the jsp from browser it will execute only the service of the extended servlet, it wont render anything that you have given in jsp file.
[ October 13, 2006: Message edited by: Balasubramani Dharmalingam ]
 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, That's a big problem. In that case I will have to make a common jsp and in that jsp's jspInit() I will have to reinitialise all objects. The inlude that common jsp in to all jsps at complie time. Will that work?
[ October 13, 2006: Message edited by: Vikas Aggarwal ]
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to take a step back, and re-evalutate the requirement.

What is it that is stored in the "static" variable?
What purpose is it storing

Could you instead store in the application scope of your webapp (ie ServletContext getAttribute/SetAttribute methods)
Another alternative could potentially to use JNDI to access shared resources.

It all comes down to what you are using "static variables" for.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic