• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getServletConfig

 
jack nick
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
This seem a very curly problem. Have got some servlet specific parameters in the web.xml. For some reason when the servlet is loaded on startup I am able to get the parameters using the getInitParameter("xyz") method. But for some reason cannot seem to get this when the servlet is initialised on first visit.
The code:
public class TestServlet extends HttpServlet {
// works only if the servlet is loaded on startup but does not come by when a request is made
public void init(){
ServletConfig servletConfig = getServletConfig();
System.out.println("Servlet Config object in init "+servletConfig);
System.out.println("USERID " + servletConfig.getInitParameter("userid"));
System.out.println("USERNAME " + servletConfig.getInitParameter("username"));
}
// does not show at all
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException
{
ServletConfig sc = getServletConfig();
System.out.println("Servlet Config object in service "+sc);
System.out.println("USERID " + sc.getInitParameter("userid"));
System.out.println("USERNAME " + sc.getInitParameter("username"));

}
}

Web.xml
<servlet>
<servlet-name>TestServletName</servlet-name>
<servlet-class>TestServlet</servlet-class>
<init-param>
<param-name>userid</param-name>
<param-value>dummy</param-value>
</init-param>
<init-param>
<param-name>username</param-name>
<param-value>John</param-value>
</init-param>
</servlet>
Thanks,
Jack
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What servlet container are you using? It smells like a bug.
- Peter
 
Kevin Mukhar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jack nick:
Hi there,
This seem a very curly problem. Have got some servlet specific parameters in the web.xml. For some reason when the servlet is loaded on startup I am able to get the parameters using the getInitParameter("xyz") method. But for some reason cannot seem to get this when the servlet is initialised on first visit.

It is most likely that this is just a symptom of correct behavior. I have the J2SDKEE reference implementation. I copied your code and deployed it, then ran the server in verbose mode. Regardless of whether I set the servlet to be loaded at startup, or loaded at request time, I never saw the debug statements printed to the console. Obviously, that can't be right. J2SDKEE RI is the latest and greatest, it MUST be calling init() correctly.
So, then I changed all the System.out.println() calls to log(), and I edited the server.xml file to turn logging on for Tomcat. Then I redeployed the servlet and checked the output in the Tomcat log. This time, I could see the output from the init() method. When the servlet was loaded at startup, init was called near the end of the startup process. When I set the servlet to load at request time, init() was called right before service() was called.
So, for Tomcat and J2SDKEE1.4 at least, System.out.println() calls in the init() method do not seem to print to the console. However, init() is still called.
 
Enrico Mannarino
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, with Tomcat and win32 System.out.println is printed in the window where you started Tomcat!
I've never had any problem with this!
On Unix and Linux I'll think is it printed out in a file called catalina_out - better check!
/Enrico
 
Enrico Mannarino
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, with Tomcat and win32 System.out.println is printed in the window where you started Tomcat!
I've never had any problem with this!
On Unix and Linux I'll think is it printed out in a file called catalina_out - better check!
/Enrico
 
jack nick
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
Thanks guys for your replies, but this still does not seem very clear.
First of all we are using Tomcat. Secondly we do get the println(s) outputed but its just that the values come out as null.
This gives us to believe that the the control does go into the init method.
Heres what happens
- We have a load on startup tag for the servlet
- When the server is started, the init method is executed and the init parameters of the servlet from the web xml is outputed correctly.
- On making the first request to the servlet, for some reason the init method is exectued again and now the values are printed out as null.
Theoretically when we make a request to this servlet the init method should not be executed any more. Since the servlet is already loaded and initialised on startup.
It looks likes another instance of the same servlet is created.
Cheers
Jack
 
Srinivas M
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jack,
I tested your sample code and everything is working fine me.
Here is my environment:
OS : Windows 2000
Tomcat Version : 4.1.12
I have tested your both scenarios:
1. With "load-on-Startup" the init() method is executing during the server startup and on subsequent call to the servlet it is executing the Service method and printing both the variables properly.
2. Without "load-on-startup" the init() method as well as the service method are getting executed when the servlet is invoked for the first time and I am able to see the values properly. On subsequent call to the servlet it is executing the service method only.
FYI
[ February 10, 2003: Message edited by: Srinivas M ]
 
jack nick
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,
What I did was run a whole new clean installation of tomcat and voila it worked. Probably something wrong with my first installation, still foxed as to what I did wrong then in my test environment.
Cheers
Jack
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic