• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why this code to get Init Param in JSP not work?

 
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In web.xml I have the following:

In the JSP I have:

But this prints out "null"!!! Why? Anyone know what I'm doing wrong?
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I GOT THE ANSWER!!!
Turns out the problem is that when you're dealing with a JSP as a Servlet, you need a servlet mapping. So to keep the jsp name, do this:

That will make it so when someone goes to: http://www.yoursite.com/test.jsp, the servlet context is initialized, loaded, etc. So NOW you have access to the ServletConfig/Context!!
 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robert
You should not have to explicitly obtain a reference to the ServletConfig object in your JSP code. The container supplies this automatically with the implicit variable config.
And you don't necessarily need a servlet mapping to resolve your null return value either. You may find that it is simply the way in which you are invoking your JSP page from the browser.
Try in the browser, and in your JSP code.
Cheers
Mark
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I know what you're saying but it doesn't work. I tried that (using tomcat) and without the servlet-mapping, it never finds the jsp page.
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robert
The <servlet-name> and <jsp-file> tags in the deployment descriptor already serve as a mapping from the Servlet name you enter in the browser to the location of the JSP file.
The <servlet-mapping> tag is used to determine which URL patterns should be handled by which servlet, and should not be required by the code you supplied earlier in this post.
I've tried it out using Tomcat without servlet mappings and it definitely does work.
Maybe I'm just lucky
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
weird. I'm using tomcat 3.2.3 and it doesn't work. What version are you on?
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
4.0.4
I've never used an earlier version than this so can't really comment on any Servlet/JSP behaviour other than Tomcat 4+
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic