wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getInitParameter method in ServletContext and ServletConfig Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "getInitParameter method in ServletContext and ServletConfig" Watch "getInitParameter method in ServletContext and ServletConfig" New topic
Author

getInitParameter method in ServletContext and ServletConfig

Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
Why is there a getInitParameter method in ServletContext and ServletConfig.Cant we get it using getAttribute methods in both?
PRavi kumar
Ranch Hand

Joined: May 13, 2007
Posts: 69
HI,

The getInitAttribute method in servlet ZConfig to get the servlet Init parameters defined in DD.

Example
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>coom.abc.y</servlet-class>
<init-param>
<param-name>xyz</param-name>
<param-value>abc</parama-value>
</init-param>
</servlet>

The getInitparameter method the Servlet Context will be used for get the initial parameters of servlet Context.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Remember that the init parameters are available to your servlet/jsp when those servlet/jsp's are being initialized...i.e. when their init() methods runs. On the other hand your attributes are available only when the service method runs. So if you want any information along with your servlet initialization, then initParameters to the rescue...Hope you understood...


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

ServletContext's getInitParameter is just here for convenience. It simply calls ServletConfig's. Attributes is a different story. You set attributes in the context, so it should not be in ServletConfig. Init parameters are set in web.xml, so they can be considered both as being part of the config and the context. (that's how I see it )


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The getInitAttribute method in servlet ZConfig

There's no getInitAttribute
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41188
    
  45
Why is there a getInitParameter method in ServletContext and ServletConfig.

Because they return different init parameters. The context configuration (of which there is a single one for a web app) is not the same thing as the servlet configuration (of which there is one for each servlet).

Cant we get it using getAttribute methods in both?

Init parameters are not attributes, and thus can't be accessed via getAttribute.
[ June 25, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
ok..so cant I use getattribute in the init method???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41188
    
  45
Posted by Renu Radhika
so cant I use getattribute in the init method?

Again: init parameters are not attributes.

Posted by Satou kurinosuke
ServletContext's getInitParameter is just here for convenience. It simply calls ServletConfig's.

This is not correct; see my post above.

Posted by Jothi Shankar Kumar Sankararaj
Remember that the init parameters are available to your servlet/jsp when those servlet/jsp's are being initialized...i.e. when their init() methods runs.

Yes, but they can also be accessed later, so that doesn't differentiate them from attributes.
[ June 25, 2007: Message edited by: Ulf Dittmer ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

This is not correct; see my post above.

Dooohhh, that's right. I got mixed up with GenericServlet#getInitParameter
 
Consider Paul's rocket mass heater.
 
subject: getInitParameter method in ServletContext and ServletConfig
 
Similar Threads
How do you set a servlet context attribute?
getMethod()
Getting file not found exception.
getInitParamater() method
TC4-TC5.5 with context-param