Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Roshini Sridharan
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!