wood burning stoves 2.0*
The moose likes Servlets and the fly likes javax.servlet.ServletConfig Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "javax.servlet.ServletConfig Problem" Watch "javax.servlet.ServletConfig Problem" New topic
Author

javax.servlet.ServletConfig Problem

A.K.A.F Illayapparachchi
Greenhorn

Joined: Oct 19, 2007
Posts: 1
To get servlet init parameters, In head first book , I saw that it use the following code :

getServletConfig().getInitParameter("Name of The parameter");

add it also saying that getServletConfig() passes the Reference of the ServletConfig.

But every Servlet extends form the HttpServlet . And It extends GenericServlet(it is also the place where getServletConfig() is defines). This GenericServlet implements the ServletConfig. If Generally Speaking, GenericServlet is a ServletConfig. There for Every Servlet is a ServletConfig.

All methods in ServletConfig is implemented in the GenericServlet.

Therefor without calling getServletConfig() and getting it's reference. We can directly calls the getInitparameter(), getServletConfig(), getServletName() & getInitParameterNames().


My Problem is this

out.println(getServletConfig().getInitParameter("value name"));


out.println(getInitParameter("value name"));


both of the lines compiles properly.
both of the lines prints same output.(In the Web applications that i have made)
but second one is easy to use.


Is there any difference in this? or Is it a convention?


SCJP 1.4<br />SCWCD 5
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
No difference, just one Object has a reference to the other...

Use whatever approach you like
jasson jasson
Greenhorn

Joined: Oct 20, 2007
Posts: 6
Originally posted by Michael Ku:
No difference, just one Object has a reference to the other...

Use whatever approach you like

getServletConfig() return a ServletConfig's reference,then you can call getInitParameter(),you will get the init propertes;
but getInitParameter(),this is GenericServlet's method,the code in this method is
public String getInitParameter(String property)
{
return config.getInitParameter(property);
}
so the two method both right;
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

"jasson jasson", please check your private messages for an important message from me.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javax.servlet.ServletConfig Problem
 
Similar Threads
getServletConfig() and getServletContext()
Servlets and ServletConfig problem
Redundant methods?
GenericServlet Question
init method of servlets