Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • 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
  • 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!!
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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+
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic