File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Initparameter not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Initparameter not working" Watch "Initparameter not working" New topic
Author

Initparameter not working

sangita kandel
Greenhorn

Joined: Jul 09, 2002
Posts: 12
Hi,
I was trying th example of using the initialization parameters from marty hall's "coreservlets".I have changes my web.xml file accordingly but am not able to access it through the servlet.ie when i run the servlet from the browser, it doest pick the settings in web.xmll, but uses the default value given in servlet file. i placed my files under a folder called sangi and this is under jakarta../webapps/examples/web-inf/classes
code is as follows-----
package sangi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class showmsg 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.getInitParamete("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 show message servlet";
out.println("<BODY BGCOLOR=\"#FDF5E6\">\n"+
"<H1 ALIGN=CENTER>" + title+"</H1>");
for(int i=0;i<5;i++)
{
out.println(message + "<BR>");
}
out.println("</BODY></HTML>");
}
}
and i added the following to the xml file
<servlet>
<servlet-name>showmsg</servlet-name>
<servlet-class>sangi.showmsg</servlet-class>

<init-param>
<param-name>
message
</param-name>
<param-value>
sangishan
</param-value>
</init-param>
<init-param>
<param-name>
repeats
</param-name>
<param-value>
5
</param-value>
</init-param>
</servlet>
<servlet-mapping> <servlet-name>showmsg</servlet-name>
<url-pattern>/showmsg</url-pattern> </servlet-mapping>
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
Hi Sangita
Just a couple of thoughts...
How do you invoke your servlet from the browser? If you use the the <servlet-name> of 'showmsg' or the <url-pattern> of '/showmsg' in the address line then it should access your deployment descriptor correctly (and subsequently access your servlet's init parameters)
Also, is your deployment descriptor correctly deployed under your web app's WEB-INF/web.xml directory?
Mark.
anay mulay
Greenhorn

Joined: Feb 25, 2003
Posts: 1
Hi Sangita,
Just ensure following 2 checks
1. Ur web.xml is placed in the WEB-INF folder of your application.
2. As u have specified the alias for ur servlet, u must try to access ur sevlet through that alias only[like http://localhost/ur_context_name/showmsg], and NOT through the actual name of the servlet.
Init params are not accessible through http://localhost/ur_context_name/servlet/Servlet_name
Tell me, if it work?
Regards
-Anay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initparameter not working
 
Similar Threads
servlet problem
problem reading the initialization parameters
who can help me in a example
package bizarre error !
problem with web.xml and the getinitParameter() func