This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

passing init-param to servlets from web.xml

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't figure out why this won't work. I don't get any errors or exceptions but no parameters are read into the Enumeration.
Help!

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">;
<web-app>
<display-name>Get method example</display-name>
<description>
This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide.
</description>
<servlet>
<servlet-name>getExample</servlet-name>
<servlet-class>Attributes</servlet-class>
</servlet>
<servlet>
<servlet-name>getInitializationParameters</servlet-name>
<servlet-class>Init_param</servlet-class>
<init-param>
<param-name>version </param-name>
<param-value> best</param-value>
</init-param>
</servlet>

<servlet>
<servlet-name>getBasketParameters</servlet-name>
<servlet-class>MyBasket1</servlet-class>
<init-param>
<param-name>Socks</param-name>
<param-value>4</param-value>
</init-param>
<init-param>
<param-name>Shirt</param-name>
<param-value>16</param-value>
</init-param>
<init-param>
<param-name>Trousers</param-name>
<param-value>30</param-value>
</init-param>
<init-param>
<param-name>Jumper</param-name>
<param-value>20</param-value>
</init-param>
<init-param>
<param-name>Shoes</param-name>
<param-value>25</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>getExample</servlet-name>
<url-pattern>/send</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>getInitializationParameters</servlet-name>
<url-pattern>/init</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>getBasketParameters</servlet-name>
<url-pattern>/shop</url-pattern>
</servlet-mapping>
</web-app>

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class MyBasket1 extends HttpServlet{
public void init(){
System.out.println( "Servlet initialised" );
}
public String startWebPage()
{
return "<HTML><HEAD><TITLE>Shop</TITLE></HEAD>" +
"<BODY BGCOLOR='#FFFFFF' TEXT='#000000' LINK='#0000FF' VLINK='#800080' ALINK='#FF0000'>" +
"<P></P><FORM ACTION='../servlet/MyBasket' METHOD='post'>" +
"<TABLE ALIGN='center' BGCOLOR='#D3D3D3' BORDER='1' CELLSPACING='5' CELLPADDING='5' WIDTH='70%' HEIGHT =50%'>" +
"<TR><TH ALIGN='left' VALIGN='middle' COLSPAN='2' ROWSPAN='1'>Item</TH>" +
"<TH ALIGN='center' VALIGN='middle' COLSPAN='1' ROWSPAN='1'>Price</TH>" +
"<TH ALIGN='center' VALIGN='middle' COLSPAN='1' ROWSPAN='1'>Quantity</TH></TR>";
}
public String endWebPage()
{
return "<TR ><TD ALIGN='center' VALIGN='middle' COLSPAN='4' ROWSPAN='2'>"+
"<INPUT TYPE='submit' VALUE='Continue' > </TD></TR><TABLE></FORM></BODY></HTML>";
}
public void doGet( HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter aPW = response.getWriter();
Enumeration enum = this.getInitParameterNames();
aPW.println( startWebPage() );
while( enum.hasMoreElements() )
{
String name = (String)enum.nextElement();
String value = this.getInitParameter(name);
aPW.println( "<TR><TD ALIGN='left' VALIGN='middle' COLSPAN='2' ROWSPAN='1'>" + name + "</TD>" +
"<TD ALIGN='center' VALIGN='middle' COLSPAN='1' ROWSPAN='1'>4</TD>" +
"<TD ALIGN='center' VALIGN='middle' COLSPAN='1' ROWSPAN='1'><SELECT NAME='" + name + "#" +
value + "'>" +
"<OPTION VALUE='0' >0</OPTION>" +
"<OPTION VALUE='1' >1</OPTION>" +
"<OPTION VALUE='2' >2</OPTION>" +
"<OPTION VALUE='3' >3</OPTION>" +
"<OPTION VALUE='4' >4</OPTION>" +
"<OPTION VALUE='5' >5</OPTION>" +
"<OPTION VALUE='6' >6</OPTION>" +
"<OPTION VALUE='7' >7</OPTION>" +
"<OPTION VALUE='8' >8</OPTION>" +
"</SELECT></TD></TR>" );
}
aPW.println( endWebPage() );
}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see a couple of things I would change.
1. You should put the servlet in a package so that instead of:
<servlet>
<servlet-name>
getInitializationParameters</servlet-name>
<servlet-class>Init_param</servlet-class>
you would have:
<servlet>
<servlet-name>getInitializationParameters</servlet-name>
<servlet-class>mypackage.Init_param</servlet-class>
Tomcat is known to get odd results if the servlet class is not in a package.
2. Test the presence of the init parameters in the init() method. It is easier than mixing in all the HTML code. In fact, if the parameters don't change, you could create an instance variable Hashtable of the name - value pairs in the init method.
Bill
 
Ranch Hand
Posts: 115
 
mattcol
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, it worked perfectly once i'd put the servlet in a package.
 
    Bookmark Topic Watch Topic
  • New Topic