File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes JSP extending Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP extending Servlet" Watch "JSP extending Servlet" New topic
Author

JSP extending Servlet

Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
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

Joined: Dec 06, 2004
Posts: 116
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" %>


Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>
Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
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

Joined: Dec 06, 2004
Posts: 116
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

Joined: Jun 22, 2001
Posts: 140
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

Joined: Jul 06, 2005
Posts: 1027
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP extending Servlet