This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 1871
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: 1871
sorry..
i meant- getServletConfig().getInitParameter()...
-maulin
 
jQuery in Action, 2nd edition
 
subject: problem reading the initialization parameters
 
Similar Threads
who can help me in a example
package bizarre error !
Initparameter not working
servlet problem
problem with web.xml and the getinitParameter() func