wood burning stoves 2.0*
The moose likes JSP and the fly likes Why this code to get Init Param in JSP not work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Why this code to get Init Param in JSP not work?" Watch "Why this code to get Init Param in JSP not work?" New topic
Author

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

Robert Paris
Ranch Hand

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

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

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

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

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

Joined: Jul 28, 2002
Posts: 585
weird. I'm using tomcat 3.2.3 and it doesn't work. What version are you on?
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
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+
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why this code to get Init Param in JSP not work?
 
Similar Threads
Cant read config params in JSP
HSFJ Mock exam doubt
config.getInitParameter
why no config in jspInit() method?
JSP Init Param