"Andrew V. Zhdanov" wrote:

> Hi, i do study Servlet Specification version 2.3 and i do like very much the
> HttpSessionListener concept: there (for those who does not know) we can use
> createSession event and destroySession event for managing session. Naturaly
> i'd like to create a new instance of MyDocument class on createSession
> event. But it (MyDocuemnt) needs SessionConfig for getInitParameters - but
> all that i have in HttpSesionEvent (Servlet API class) is HttpSession.
> Question is: How could i get ServletConfig from HttpSession or in any other
> way, but MyDocument does not extends HttpServlet.
> Could smb help me? Is anybody interested at it?
> Best Regards.
> [...]

Hi :-)  I suggest you try the following:

    public void sessionCreated(HttpSessionEvent e){
        ...
        HttpSession session=e.getSession();
        ServletContext  sctx= session.getServletContext();
        sctx.getInitParameter(...);
        ...
    }

But with the above code, ServletContext.getInitParameter(...) will be
used( Not ServletConfig/GenericServlet.getInitParameter(...) )

there are 2 kinds of initParameters(please see the following emails):
-
  % ServletConfig.getInitParameter(...)
  % GenericServlet.getInitParameter(...)
   they return the "<init-param>" in "senelet-declaration" in
   WEB-INF/web.xml

- ServletContext.getInitParameter(...)
   I am not sure where I can define it and which tag I can use to define it?
   % in TOMCAT_HOME/conf/server.html  ? OR  in WEB-INF/web.xml ?
   % which "tag" I can use?




Bo
June 17, 2001




please see the folloing email:
//*******************************************************
Ajay Ejantkar wrote:
>
>     public void init() throws ServletException {
>         // Read initialization parameters from the web.xml file
>         ServletConfig config = getServletConfig();
>         String driverClassName =
> config.getInitParameter("driverClassName");

Change the init() method to

    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        // Read initialization parameters from the web.xml file
        //no longer need next line
        //ServletConfig config = getServletConfig();
        String driverClassName = config.getInitParameter("driverClassName");
        String url = config.getInitParameter("url");
        //... etc.

You can't get the init parameters unless the servlet has access to the config
object. That only happens when you call super.init(config). The base servlet
class stores a reference to the config object. Inside the init(ServletConfig
config) method, you already have a reference to the config object, so you no
longer need to call getServletConfig().

If you look at GenericServlet javadoc, you will see that it implements
ServletConfig. So if your servlet extends HttpServlet (which extends
GenericServlet) you can call getInitParameter(String) in your other servlet
methods without needing a reference to the config object. For example:

public void doPost(.....) {
  String param = getInitParmater("name");
  //... etc.
}

Since GenericServlet implements ServletConfig, all the ServletConfig methods
are available in your servlet. The method call is handled by GenericServlet
which forwards the call to the config object that was saved when your code
calls super.init(config), which is another reason to use the
init(ServletConfig) method instead of init().

Kevin Mukhar
//*******************************************************

On Mon, 14 May 2001, Jeff Kilbride wrote:

> I had the same problem when I first started using init
> parameters. What I found is that you can't use the ServletConfig
> object to retrieve parameters from your web.xml file. You must use
> the ServletContext object instead, so if you change your code to
> this:
>
> message = getServletContext().getInitParameter("message");
>
> It should work.

A couple of things:

ServletConfig.getInitParameter() should be the same as
GenericServlet.getInitParameter() (the latter is a convenience method
for the former).  It gets servlet initial parameters, as specified by
init-param's in web.xml.

ServletContext.getInitParameter() gets context initial parameters, as
specified by context-param's in web.xml.

So they are different things.

(See http://java.sun.com/products/servlet/2.2/javadoc/index.html.)
...

Milt Epstein
//*******************************************************

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to