This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

config.getInitParameter  RSS feed

 
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
 
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
 
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.
 
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:
 
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.
 
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
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!