File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Initparameter not working

sangita kandel

Joined: Jul 09, 2002
Posts: 12
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 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 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
if (message== null)
message = defaultMessage;
String repeatString = config.getInitParameter("repeats");
repeats = Integer.parseInt(repeatString);
catch(NumberFormatException nfe)
{ }
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
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>");
and i added the following to the xml file

<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?
anay mulay

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?
I agree. Here's the link:
subject: Initparameter not working
It's not a secret anymore!