• 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

JSP extending Servlet

 
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,

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
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic