anywhere to set "init-param" type values for a JSP page ?
Joined: Jun 21, 2005
we know that, for servlet, we can set some init-param values in the web.xml so that when the servlet instance is loaded it always first get these values. Now, in my JSP page, I want to set some init-param values too, and I what JSP page to retrieve the values when the page is loaded. Is there any way to do it ?
Joined: Jan 16, 2001
Its not directly possible to set the init parameters for JSP page because it gets converted to Servlet at one point of time.
To get around this.. use application level parameters within you web.xml file as follows:
Place this xml code within the <web-app> tags in your web.xml.. but not within the <servlet> tags.
You can do this. You just need to declare your JSP as a servlet in web.xml
In this case, I've created a JSP named init.jsp and mapped it to the url-pattern "init.jsp". The mapping is important. You don't want people hitting it directly (which bypasses the settings in web.xml).
Then, in the JSP, you can get the initParameters from the implicit "config" object.
NOTE: In my own code, I would never do this. In my opinion, JSPs should be nothing more than a view. All variable declarations and initializations should be done in servlets.