• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javax.servlet.ServletConfig Problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No difference, just one Object has a reference to the other...

Use whatever approach you like
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"jasson jasson", please check your private messages for an important message from me.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic