File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add init parameter to listerner from web.xml

 
Fouad Fares
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Sorry i mean getInitParameter("path") and not tag .
it's typing error .
Best regards .
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically,
replace

with

--chad
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic