• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Initparameter not working

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic