Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

config.getInitParameter

 
Graham Mead
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm trying to access an intialisation parameter in a jsp file.
My web.xml file has the following entry
<servlet>
<servlet-name>Jsp1</servlet-name>
<display-name>Jsp1</display-name>
<jsp-file>/Jsp1.jsp</jsp-file>
<init-param>
<param-name>myInitial</param-name>
<param-value>Y</param-value>
</init-param>
</servlet>
Using config.getInitParameter("myInitial")in the JSP always returns null!!!
Has anyone else had these problems and sorted it out.
TIA Graham
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be confusing servlet init params with context init params.
Call getInitParameter on the servlet itself (i.e. remove the "config.") and this should fix your problem.
Cheers
 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
let me disagree with the last post. to use the context parameters you set the <context-param>
tag, and not <init-param>.
i guess you get your init parametes == null because you have to call your servlets with path which includes your <servlet-name> and not the direct path.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this was a while ago. but I got the answer for you. The problem was after you set up your servlet context for your JSP, you need to set up a servlet mapping so the web server can map the servlet context to your jsp! Like this:
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Graham
I suspect you may be calling the JSP file directly, instead of using the deployment decriptor configuration.
That is, if you call your JSP file with:

then you will definitely get a null return from config.getInitParameter("myInitial").
However, if you invoke your JSP file with:

then you should get your desired result.
Hope this helps
Mark
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
Yes, but even though he set Servlet-Name, if he has no servlet mapping, then you're idea won't work.
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert
It wasn't just an idea. I ran Graham's code that he supplied in his post and it worked. Servlet mapping is definitely not a pre-req to locating a JSP page.
 
Pat Wallwork
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You definitely don't have to use servlet mappings if you set your init-param's in your web.xml like this:

then to call it from a jsp you just use:
getServletContext().getInitParameter("name");
-Pat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic