wood burning stoves 2.0*
The moose likes Servlets and the fly likes Add init parameter to listerner from web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Add init parameter to listerner from web.xml" Watch "Add init parameter to listerner from web.xml" New topic
Author

Add init parameter to listerner from web.xml

Fouad Fares
Ranch Hand

Joined: Apr 16, 2003
Posts: 38
Hello,
I have a web application developped under 9i jdeveloper .
1- I'm using the web.xml file as a configuration file .
2- I have a java listener of servletcontext and sessions .
I'm not able to read parameter initialised in my web.xml
from this listener class.
Here are the web.xml :
=================================
<web-app>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<listener>
<listener-class>Banque.divers.Jcls_Init</listener-class>
<init-param>
<param-name>path</param-name>
<param-value>bnq</param-value>
</init-param>
</listener>
<mime-mapping>
Etc ...
</web-app>
=================================
Here are the Listener Class :
=================================
package Banque.Divers;
import javax.servlet.http.*;
Etc ...
public class Jcls_Init implements ServletcontextListener, HttpSessionListener
{
public Jcls_init()
{
}
ServletContext servletcontext ;
public void contextInitialialized(ServletContextEvent sce)
{
String tag = sce.getServletContext().getInitParameter("tag") ;
System.out.println(tag);
}
It returns me a null value .
=================================
Could you please advise me how can i solve this problem ?
Thank You in advance,
Fouad fares.
Baka Neko
Greenhorn

Joined: Dec 09, 2003
Posts: 14
public void contextInitialialized(ServletContextEvent sce)
{
String tag = sce.getServletContext().getInitParameter("Name of the Parameter you wannna have the value, in your case path");
System.out.println(tag);
}
Im also in the middle of this problem. Lol
Maybe this will help ya...for me Im still getting NULL ...maybe there is something wrong with the invoker of the web.xml in Tomcat-Folder..o_O?
Any help would be great!
Baka out
Fouad Fares
Ranch Hand

Joined: Apr 16, 2003
Posts: 38
Hello,
Sorry i mean getInitParameter("path") and not tag .
it's typing error .
Best regards .
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

From the spec:

You cannot use an init-param just anywhere. It should also be noted that this value is retrievable from the ServletConfig and FilterConfig objects, which are passed in to the init methods of servlets and filters, respectively.

You might try using a context-param element:
Those parameters serve to initialize anything and everything that can obtain a ServletContext object. (servlets, filters, listeners, etc).
[ December 10, 2003: Message edited by: Mike Curwen ]
Chad Dalton
Greenhorn

Joined: Feb 19, 2003
Posts: 20
Basically,
replace

with

--chad
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Basically, that's wrong.

The code should remain the same, it's *where* in web.xml you declare the parameter, and with what tag.
[ December 12, 2003: Message edited by: Mike Curwen ]
Chad Dalton
Greenhorn

Joined: Feb 19, 2003
Posts: 20
Originally posted by Mike Curwen:
Basically, that's wrong.

The code should remain the same, it's *where* in web.xml you declare the parameter, and with what tag.
[ December 12, 2003: Message edited by: Mike Curwen ]

Excuse me, he wants the parmeter to be servlet level and NOT context level from what I understand. Now if he wants the parmeter to be context level (and available to all servlets in that context) then he leaves the code alone and fixes the web.xml, but if he wants ONLY his listener class to use the parmeter then he needs to change the code to what I provided.
--chad
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
Originally posted by Chad Dalton:

Excuse me, he wants the parmeter to be servlet level and NOT context level from what I understand. Now if he wants the parmeter to be context level (and available to all servlets in that context) then he leaves the code alone and fixes the web.xml, but if he wants ONLY his listener class to use the parmeter then he needs to change the code to what I provided.
--chad

That is incorrect. Neither of these will work with his current web.xml file. The way the parameter has been defined in his code snippet is wrong, no matter what code you try to use to get the value back out. If he wants to define the parameter at a servlet level, then it should go in the init params for the servlet, otherwise he can define it as a context-param element.
Either way, he is going to have to change how it is currently defined in the web.xml
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Excuse me, he wants the parmeter to be servlet level and NOT context level from what I understand.

It's not a question of what he wants, it's what will work or not. If you want to use web.xml to configure a Listener (not a servlet), then you *need* to use a context-param, you have no other choice. The DTD tells you that you cannot place an init-param inside a listener declaration. In fact, I'm quite surprised Tomcat doesn't have a big problem with it. I believe it should at least throw a warning.

change the code to what I provided

The code you provided does not work in a Listener, only classes that extend HttpServlet, and only those that call super.init(config); in their init() method (if they're overriding it).


[ December 15, 2003: Message edited by: Mike Curwen ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add init parameter to listerner from web.xml