File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem reading the initialization parameters

 
trupti nigam
Ranch Hand
Posts: 623
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry..
i meant- getServletConfig().getInitParameter()...
-maulin
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic