You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by jackling <ja...@singnet.com.sg> on 2001/05/14 16:15:47 UTC
Help: init parameters fail.
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>
Re: init parameters fail.
Posted by Milt Epstein <me...@uiuc.edu>.
On Mon, 14 May 2001, Jeff Kilbride wrote:
> I had the same problem when I first started using init
> parameters. What I found is that you can't use the ServletConfig
> object to retrieve parameters from your web.xml file. You must use
> the ServletContext object instead, so if you change your code to
> this:
>
> message = getServletContext().getInitParameter("message");
>
> It should work.
A couple of things:
ServletConfig.getInitParameter() should be the same as
GenericServlet.getInitParameter() (the latter is a convenience method
for the former). It gets servlet initial parameters, as specified by
init-param's in web.xml.
ServletContext.getInitParameter() gets context initial parameters, as
specified by context-param's in web.xml.
So they are different things.
(See http://java.sun.com/products/servlet/2.2/javadoc/index.html.)
See Bo Xu's comments about named/defined servlets (in web.xml), and
what URL you are using to call a servlet. That has a great impact on
whether/how initial parameters (both servlet and context) work.
> ----- Original Message -----
> From: "jackling" <ja...@singnet.com.sg>
> To: <to...@jakarta.apache.org>
> Sent: Monday, May 14, 2001 7:15 AM
> Subject: Help: init parameters fail.
>
>
> > 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>
> >
> >
>
Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
mepstein@uiuc.edu
Re: init parameters fail.
Posted by Jeff Kilbride <je...@kilbride.com>.
I had the same problem when I first started using init parameters. What I
found is that you can't use the ServletConfig object to retrieve parameters
from your web.xml file. You must use the ServletContext object instead, so
if you change your code to this:
message = getServletContext().getInitParameter("message");
It should work.
Thanks,
--jeff
----- Original Message -----
From: "jackling" <ja...@singnet.com.sg>
To: <to...@jakarta.apache.org>
Sent: Monday, May 14, 2001 7:15 AM
Subject: Help: init parameters fail.
> 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>
>
>
Re: Help: init parameters fail.
Posted by Bo Xu <bo...@cybershop.ca>.
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