aspose file tools*
The moose likes Servlets and the fly likes problem reading the initialization parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem reading the initialization parameters" Watch "problem reading the initialization parameters" New topic
Author

problem reading the initialization parameters

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Hi,
I have a servlet which reads the initialization parameter & displays it.If there is no initialization parameter it displays the default message.
but in my case even though there is an initialization parameter defined in the web.xml my servlet is displaying the default message.
what can be the error??
I store the .java files under myServlet directory which is set in the classpath.All the .class files are saved under install-dir/webapps/root/WEB-INF/classes directory.

below is the servlet code..

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ShowMessage extends HttpServlet
{
private String message;
private String defaultMessage = "no message";
private int repeats = 1;

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)
{}

}//--init
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>");
}//---for
out.println("</body></html>");

}//---doGet


}//--end of class
below is the web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>ShowMessage</servlet-name>
<servlet-class>ShowMessage</servlet-class>

<init-param>
<param-name>message</param-name>
<param-value>Hello there</param-value>
</init-param>
<init-param>
<param-name>repeats</param-name>
<param-value>5</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ShowMessage</servlet-name>
<url-pattern>/ShowMessage</url-pattern>
</servlet-mapping>
</web-app>
thanks in advnce..
trupti
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi
try ,
getServletContext().getInitParameter("message")
...
i'm not sure but when u call super.init() and if its emptying the config object...
i'll try to do something like this tommorrow and 'll let u know..
regards
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
sorry..
i meant- getServletConfig().getInitParameter()...
-maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem reading the initialization parameters