Ack, here I go being a writing critic and leave in a glaring typo . . .

Erik Weber wrote:

Well, I have to criticize my own advice. I just read over the Servlet 2.4 spec. It doesn't explain this well at all, in my opinion. It still uses the same old, nearly meaningless, obscure language like "used by the Servlet container to communicate with a Servlet", and stuff like that, which doesn't help anyone who actually wants to write a program. It does mention the getInitParameter methods of ServletContext and ServletConfig, but nowhere that I could find does it simply say, use one with this element in web.xml and the other with that element in web.xml, and/or why you would choose one or the other.

Why can't the spec just say what I wrote below instead of trying to be so "conceptual"? Is what I wrote wrong? This is the stuff that .NET people criticize and that frustrates new programmers (in my experience).


Here is a partial outline of how it should be explained:

Initializing a Servlet
-------------------------

I. Initialization parameter that is global to your Web application (all Servlets)

A. web.xml

<web-app>
   . . .
   <context-param>
       <param-name>globalDateFormatString</param-name>
       <param-value>yyyy-MM-dd</param-value>
   </context-param>
   . . .
</web-app>

B. Java

// store the date format String, but not a DateFormat,
// as an instance variable or class variable
// (DateFormat can't always be used concurrently)
protected String globalDateFormatString;
. . .
public void init() throws ServletException, UnavailableException {
globalDateFormatString = getServletContext().getInitParameter("globalDateFormatString");
// try to instantiate a SimpleDateFormat with the String;
// if the String is invalid, throw an Exception to make the Servlet unavailable
}




II. Initialization parameter that is specific to one Servlet

A. web.xml

<servlet>
   . . .
   <init-param>
       <param-name>specificDateFormatString</param-name>
       <param-value>EEE, d MMM yyyy HH:mm:ss Z</param-value>
   </init-param>
   . . .
</servlet>

B. Java

protected String specificDateFormatString;
. . .
public void init() throws ServletException, UnavailableException {
globalDateFormatString = getServletConfig().getInitParameter("specificDateFormatString");


should be:

specificDateFormatString = getServletConfig().getInitParameter("specificDateFormatString");



Erik


}



Please correct me if I have it wrong, or show me the light if I missed something in the spec. I know I learned this somewhere, but can't remember where (the tutorial maybe?).

Shouldn't this be the way the spec reads, or does this type of example belong somewhere else?

Erik

P.S. The spec is still required reading. ;-)




Bill Siggelkow wrote:

Thanks for the clarification, Erik.

Erik Weber wrote:

I think you want ServletContext.getInitParameter here. That is used with the "context-param" element (global to your web app). ServletConfig.getInitParameter is used with the "init-param" element (specific to one Servlet).

In addition to the JavaDocs, you should read the Servlet specification, which tells you how Servlets and Servlet containers are supposed to work. Trust me, it is required reading:

http://www.jcp.org/aboutJava/communityprocess/final/jsr154/

Erik




Bill Siggelkow wrote:

MyServlet extends HttpServlet {
public void init (ServletConfig config) {
String paramValue = config.getInitParameter("myContextParamName");
}
}

Personally, I have found the JavaDocs invaluable for this kind of stuff; you can find the Servlet 2.4 Javadocs online at http://jakarta.apache.org/tomcat/tomcat-5.0-doc/servletapi/index.html.

-Bill Siggelkow

Nishant wrote:

hi
can anyone tell me how can i get context-param parameter in my servlet .
thanks in advance
Regards
Nishant Patil
Software Engineer
Cybage Softwares Pvt. Ltd. (A CMM Level 3 Company)
West Avenue, Kalyaninagar
Pune - 411006
Ph. +91-20-4044700/4041700 Extn 355
[EMAIL PROTECTED]
www.cybage.com
"There is difference between knowing the Path and walking on the Path"






---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to