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().

___________________________________________________________________________
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