wood burning stoves 2.0*
The moose likes Servlets and the fly likes When and why use getServletConfig()??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "When and why use getServletConfig()???" Watch "When and why use getServletConfig()???" New topic
Author

When and why use getServletConfig()???

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
All methods of ServletConfig are implemented in GenericServlet class. (e.g. getInitParameters() ). Why do I need to call getServletConfig() to get a ServletConfig object and use this object to call these methods?
Can I say this
public void init() {
String parm1 = getInitParameters(“p1” ;
}
instead of
public void init() {
ServletContext c = getServletContext();
String parm1 = c.getInitParameters(“p1” ;
}

Any idea?
Thanks.


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
One thing that pops up off the top of my head is if you want a helper class to have access to those parameters. You wouldn't want to pass a reference to the servlet since it exposes methods that you do not want the helper class calling. Passing the ServletConfig to the helper class exposes only the methods associated with the ServletConfig.


Matthew Phillips
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks Matthew.
All text books I read say that in a servlet you should get ServletConfig first then use it to get init param etc.
You seemed to imply that if helper class is not involved, there is no need for getting servletConfig obj.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Originally posted by Bruce Jin:
You seemed to imply that if helper class is not involved, there is no need for getting servletConfig obj.

Someone more experienced than me would have to discuss the advantages/disadvantages to using getServletConfig() to call the methods of the ServletConfig within the servlet itself.
Khurram Shahood(SCJP2)
Greenhorn

Joined: Jun 08, 2002
Posts: 18
hi
i think this method is used for get the servlet configuration and change the configuration.


khurram shahood(Java Farmer)
jawwad ahmed
Ranch Hand

Joined: Dec 01, 2001
Posts: 179
Well getServletConfig can be used such as u have put databaseconnection url or dsn name or say driver class name or any type of initialization of that particulat servlet u put in servlet <init-param> and get values from getInitPaameter.
While getServletContext like(In i have shopping cart application)I use it command pattern and in that commad execute method i return destinatrion of view(jsp) from taking servlet contex parameter because it can be puuten in particular servlet.

Jawwad Ahmed
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
The question was that all methods in ServletConfig have been implemented in GenereicServlet and one can do all those things (getInitParameter() ) without getServletConfig().
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
It's worth pointing out that invoking getInitParameter() on an instance of ServletContext and on an instance of GenericServlet are two different things; one yields the context's init parameter, the other, the servlet's init parameter.
So, the method names may be the same, but they have different meanings.
Try putting a <context-param> in your webapp's descriptor and an <init-param> for one of its servlets, using the same <param-name>.
Call getInitParameter() on the context, then the servlet, passing that parameter name and you'll see what's going on.
Different 'scope', if you want to conceptualize it that way.
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
Ok, go ahead, somebody hit me with a board. ;-)
All that blabbering only to realize (too late) that I'd misread the Q as referring to ServletContext, rather than ServletConfig...
Errr... I'd say it's a matter of style if you're inside a method a a GenericServlet ( or subclass ) as to whether you use the ServletConfig instance or just invoke the methods as implemented by GenericServlet.
Some would say it's clearer to avoid the sugar and explicitly use the context. Some would disagree and say use the context only when you need to ( ie: not inside a GenericServlet descendant ).
In the Tomcat implementation, I believe that the ServletConfig and GenericServlet each present a facade over a common object for this servlet info anyway; amounts to the same thing.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks Ken:
I wrote code and verified that methods in ServletConfig can GenericServlet are the same thing with TOMCAT. Is it possible that they amount to the different things with other containers?
Thanks.
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
The way I understand the spec, they're both going to give you the same result - for all compliant implementations.
GenericServlet implements ServletConfig merely as a convenience - so that you don't have to get the config instance first.
My point about Tomcat was really more that, looking at the source code, you can see that there's really no material difference in how those accessors are implemented in servlet vs. config.
You can also be guaranteed that the ServletConfig implementation inside GenericServlet is safe to use inside a servlet's init() methods - the config is guaranteed to be set up first, and the GenericServlet implementation of ServletConfig methods just forwards to the config anyway.
Soooo... it comes down to a pure matter of style, I suppose.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
It seems that I can use getInitParameter anywhere in the servlet (outside init()) without use ServletConfig. I was little confused that all servlet books that I read use ServletConfig inside init() to get init parameters. Maybe I should check out a few more books
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
No, I think you can use those methods inside a servlet with confidence - they are guaranteed to be exactly the same as when using with the ServletConfig - per the spec.
You only need to get the config if you need to pass it elsewhere, or if you prefer that style.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks Ken.
I read the spec:
public java.lang.String getInitParameter(java.lang.String name)Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist. See ServletConfig.getInitParameter(String) .
This method is supplied for convenience. It gets the value of the named parameter from the servlet’s ServletConfig object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When and why use getServletConfig()???
 
Similar Threads
why init() method is not final?
Overriding the equals ( ) method
getServletConfig()
init method of servlets
init() and http related questions