jackling wrote:
> I was trying an example of java servlet book. In the example, the prog. (see
> excerpt below) should read the parameter values from web.xml and use the
> values accordingly. However, it seems it is unable to read the values & thus
> only able to give me default values the prog. use.
>
> I can't find what's wrong.
> advice pls.
> jackling.
>
> The ShowMessage classes is part of the mypackage package under the mypackage
> folder. Below is the excerpt: -
>
> public void init(ServletConfig config)
> throws ServletException {
> super.init(config);
> message = config.getInitParameter("message");
> if (message == null) {
> message = defaultMessage;
> }
> try {
> String repeatString =
> config.getInitParameter("repeats");
> repeats = Integer.parseInt(repeatString);
> } catch(NumberFormatException nfe) {
>
> }
> }
> public void doGet(HttpServletRequest request,
> HttpServletResponse response)
> throws ServletException, IOException {
> response.setContentType("text/html");
> PrintWriter out = response.getWriter();
> String title = "The ShowMessage Servlet";
>
> public void doGet(HttpServletRequest request,
> HttpServletResponse response)
> throws ServletException, IOException {
> response.setContentType("text/html");
> PrintWriter out = response.getWriter();
> String title = "The ShowMessage Servlet";
> out.println(ServletUtilities.headWithTitle(title)
> "<BODY BGCOLOR = \"#FDF5E6\">\n" +
> "<H1 ALIGN=CENTER>" + title + "</H1>");
> for (int i = 0; i<repeats; i++) {
> out.println(message + "<BR>");
> }
> out.println("</BODY></HTML>");
> }
>
> I've edited the web.xml file in the webapps\examples\web-inf directory and
> include the followings: -
> <servlet>
> <servlet-name>
> ShowMsg
> </servlet-name>
> <servlet-class>
> mypackage.ShowMessage
> </servlet-class>
>
> <init-param>
> <param-name>message</param-name>
> <param-value>Sembawang</param-value>
> </init-param>
>
> <init-param>
> <param-name>repeats</param-name>
> <param-value>5</param-value>
> </init-param>
> </servlet>
Hi :-) I am not sure, I guess the reason is:
did you use your <servlet-name>(not the "default Servlet") to invoke your
Servlet? i.e.
- I think you need to use something like the following:
http://xxx.xxx.xxx.xxx:8080/yourwebapp/ShowMsg
now you ask the container to use that servlet-defination defined by you in
WEB-INF/web.xml:
whose name is "ShowMsg"
which has <init-param> defined by you
so now you can get the initParameter with:
ServletConfig.getInitParameter(...)
GenericServlet.getInitParameter(...)
(from the Servlet API doc, the are similar)
- but if you use the following or something else:
http://xxx.xxx.xxx.xxx/yourwebapp/servlet/mypackage/ShowMessage
because now you don't tell container the <servlet-name> you want, so
now container use another one(the default one), so I think now you can
not get the initParameter
Bo
may.14, 2001