Forums Register Login

anywhere to set "init-param" type values for a JSP page ?

+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
Hi,

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.

<context-param>
<param-name>theName</param-name>
<param-value>theValue</param-value>
</context-param>

repeat for as many as you need...

Then, to read them out in your JSP use the following code:

ServletContext context = getServletContext();
String myValue = context.getInitParameter("theName");

Application level parameters are accessable via the servlet or jsp... and are global to all your applications.


Hope this helps
+Pie Number of slices to send: Send
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.
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3560 times.
Similar Threads
config.getInitParameter
mapping all jsps to single servlets init parameters
Servlet init parameter from JSP ?
Confusion in setting Init Parameters for jsp
Mistakenly changed web.xml
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:52:31.