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 why no config in jspInit() method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "why no config in jspInit() method?" Watch "why no config in jspInit() method?" New topic
Author

why no config in jspInit() method?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
JSPs are translated into Servlet before serving to any client. And so JSPs also have the same life cycle after translation (init(), service() and destroy()).

My question is, why jspInit() method (corresponding to init() method of servlet) doesn't take any config parameter???

Thanks.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Why should it?
What would you pass to it?

The servlet gets passed its ServletConfig information, which is documented in web.xml for that servlet.
You don't have an entry in web.xml for a JSP, so you don't

Don't lose hope however. If you look at the Api page for JspPage, you will see that it specifies that getServletConfig() must return the desired info, while in the init method.

So you can get hold of it regardless.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Stefan Evans:

The servlet gets passed its ServletConfig information, which is documented in web.xml for that servlet.
You don't have an entry in web.xml for a JSP, so you don't


Did you mean, we can't provide configuration parameter (config init paramater) in web.xml for JSP.

If yes then it is NOT right. we can give init paramaters to JSP also through web.xml:



Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
As originally posted by Stefan Evans:

Don't lose hope however. If you look at the Api page for JspPage, you will see that it specifies that getServletConfig() must return the desired info, while in the init method.


Enough said.
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
Since JSPs are dynamically converted into servlets, it is not possible to give init paramaters for a particular JSP.
However, if you precompilation like JSPC, then it will generate a xml file which you would need manually copy paste its contents to web.xml. In this case you can give the init parameters for each JSP file.

Hope this helps.


Manesh
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
why jspInit() method (corresponding to init() method of servlet) doesn't take any config


Just to add to this, even in the servlet version, init() is overridden , and not the init with the ServletConfig.


And,,,,
Since JSPs are dynamically converted into servlets, it is not possible to give init paramaters for a particular JSP.


Why not?? Is ths not equivalent to what rathi ji specified.

<servlet><jsp-file>/MyJSP.jsp</jsp-file><init-param><param-name>key</param-name><param-value>value</param-value></init-param></servlet>
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
I was not aware of this. Thanks geetu for updating my knowledge.
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
hey but wait i minute..
i had read so.. but seems to have a problem...



This is my configuration..
and my jsp has



'null' is printed. Can someone explain why is that ??
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
I think you can try mapping servelt to the jsp file and then try again.

<servlet-mapping>
<servlet-name>oneInApp2</servlet-name>
<url-pattern>>/jsp/oneInApp2.jsp</url-pattern>
<servlet-mapping>
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
Thats correct Dilip.I didnt have the mapping in my web.xml
Thanks
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
One more oint tht hs to be noted..
if u access the jsp.. directly .. without goin through the servelt mapping.. u will never get the init parameters.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by geetu lalchandani:
One more oint tht hs to be noted..
if u access the jsp.. directly .. without goin through the servelt mapping.. u will never get the init parameters.


How to access JSP with going through the servelt mapping???
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
Originally posted by rathi ji:


How to access JSP with going through the servelt mapping???


Just through normal way.
[ May 26, 2006: Message edited by: Dilip Kumar Jain ]
Peter Hu
Ranch Hand

Joined: Apr 15, 2005
Posts: 33
The jspInit method is provided by the jsp convention. It's not the equivalent of the servlet init method, rather it provides the similar opportunety to do your initialization work. The servlet's init method still exists and lives well inside jsp. The reason jsp doesn't inherit directly, instead offers the jspInit, is that it wants to make sure the execution of init() is proper without being diverted accidentally by non-experienced programmers. If you want the config, override the init method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why no config in jspInit() method?