my dog learned polymorphism*
The moose likes JSP and the fly likes config.getInitParameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "config.getInitParameter" Watch "config.getInitParameter" New topic
Author

config.getInitParameter

Graham Mead
Ranch Hand

Joined: Sep 28, 2001
Posts: 57
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jul 28, 2001
Posts: 260
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.


Asher Tarnopolski
SCJP,SCWCD
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
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

Joined: Feb 14, 2001
Posts: 285
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

Joined: Jul 28, 2002
Posts: 585
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

Joined: Feb 14, 2001
Posts: 285
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

Joined: Sep 23, 2001
Posts: 72
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: config.getInitParameter