Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

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 ?
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


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
Posts: 13411
Firefox Browser VI Editor Redhat
  • 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.
    Bookmark Topic Watch Topic
  • New Topic