File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getInitParameter(String str)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "getInitParameter(String str)?" Watch "getInitParameter(String str)?" New topic
Author

getInitParameter(String str)?

Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
The following methods are available in both classes GenericServlet(so directly accessible within any servlet) and ServletContext.
But why the popular way to get access to them is through the ServletContext?
getInitParameter(String str)
getInitParameterNames()
Thank you for precious guidance.


Best Regards,<br />Pourang Emami
Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
I think they are used for different parameters. ServletContext.getInitParameter() will return you the <context-param> defined in web.xml, whereas Servlet.getInitParamter() will return you the <init-param> defined under the tag of <servlet> in web.xml. Hope I am right.
eric mcentee
Ranch Hand

Joined: May 02, 2001
Posts: 66
It's convention in my opinion. You aren't ever going to deploy a GenericServlet, and currently the only real option is HttpServlet. Rather than try to inherit methods from the parent class, it makes more sense to pull from a direct parent class.
Page 83 of Deshmukh/Malavia points out what you state that you can get it from both, but doesn't give a reason why one is favored over the other.
I'd be interested to hear if anyone has any other thoughts on this.
Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
Thank you so much for your replys.
Yuan, you were so right about the kind of parameters each method returns.
And dear Eric, I got this certainty exactly from the page83 of the book, as you suggested.
Thank you again and Good luck.
Pourang
Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
But, the same question still strongly persists for the ServletConfig and GenericServlet. (you may get some idea from the Wrox page#55)
Please let me know about the answer, if you could get it.
Best Regards,
Pourang
Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
Ok, one thing bears in mind. GenericServlet implements ServletConfig interface. This is the reason why GenericServlet can call methods like getServletContext or getInitParamter. Whether to use GenericServlet or ServletConfig, I think it depends on the styling and clarity of the code. In deep, they are the same. Both classes are in the domain of a specific Servlet, whereas ServletContext is in a higher level (application). That's why the same getInitParameter method will return different parameters. Hope this helps.
Amer Khan
Ranch Hand

Joined: Apr 05, 2003
Posts: 163
I think they are used for different parameters. ServletContext.getInitParameter() will return you the <context-param> defined in web.xml, whereas Servlet.getInitParamter() will return you the <init-param> defined under the tag of <servlet> in web.xml. Hope I am right.
----------------------------------------------------------
Yes you are Right.

public abstract class GenericServlet implements Servlet,ServletConfig,Serializable

Not ServletContext.

Little more detail:
Because the GS class implements the ServletConfig interface it allows the servlet developer to call the ServletConfig methods directly without having to first obtain a ServletConfig object(convenience).These methods are
1-getInitParameter
2-getInitParameterNames
3-getServletContext
4-getServletNames
each of these methods delegates the call to the respective methods in the stored ServletConfig object.
public void init(ServletConfig config)
must call super.init(config).
This method stores the ServletConfig object in a private transient instance variable called (config).

When overriding
public void init(){
} A convenience method which can be overridden so that there's no need to call super.init(config).
you can use the getServletConfig() to access ServletConfig object.
GS's
getInitParameter:This method is supplied for convenience. It gets the value of the named parameter from the servlet's ServletConfig object.

getInitParameterNames :This method is also supplied for convenience. It gets the parameter names from the servlet's ServletConfig object.
Both these methods are used to access a particular servlet's parameters configured in the web.xml.

Whereas getInitParameter,getInitParameterNames methods of ServletContext access global parameters configured in the web.xml
(All servlets can access them)example: data base driver's.
[ July 31, 2003: Message edited by: Amer Khan ]

<i>Dare to dream - everything that exists today,was once a figment of someone's imagination, nobody says tomorrow can't be a figment of your today.</i>
Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
Dear Amer
Then why we should bother ourselves to call getServletConfig() from
our servlet(as it is so popular)?
I should thank you for your precious guidance my friend.
Best Regards,
Pourang
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
Just to add to the confusion, well hopefully to clariy.:-) The Deshmukh book on page 83 is referring to ServletContext.getInitParameter(). It is saying that you can get ServletContext by using getServletConfig().getServletContext() or by calling GenericServlet.getServletContext(). But in both cases the method then called is getInitParameter of a ServletContext object.
So I guess the question is what are you really asking Pourang? If you want to know if GenericServlet.getInitParameter() and ServletContext.getInitParameter() return different values then that has been answered. They return different values.
But if you are asking why someone would prefer getServletConfig().getServletContext().getInitParameters() over getServletContext().getInitParameters() then I don't know the answer and it's not mentioned in the Deshmukh book. The second is obviously the shorter method so you'd think that that would be the preferred one.
I hope this clarifies things rather than confusing them.
Ken
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getInitParameter(String str)?