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 Servlet Initialization Parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Initialization Parameters" Watch "Servlet Initialization Parameters" New topic
Author

Servlet Initialization Parameters

len oke
Greenhorn

Joined: Mar 19, 2007
Posts: 4
Am trying to send information from web.xml to MyBasket.java servlet to SimpleShoppingBasket servlet. The information from the web.xml file is passed successfully to the first servlet, but I get an error from the log:....Servlet initialised
Servlet destroyed,choices null 0 0....when trying to send to the second servlet.


Could anyone help? I have included my code if anyone fancies giving me a few tips!!

FIRST SERVLET:

code:


package Basket;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class MyBasket extends HttpServlet{
public void init() throws ServletException{
System.out.println( "Servlet initialised" );
}

private String startWebPage(){
return "<HTML><HEAD><TITLE>My Basket</TITLE></HEAD><BODY>" +
"<FORM action='../servlet/SimpleShoppingBasket' method='POST'>" +
"<TABLE ALIGN='center' BORDER='1' WIDTH='50%' CELLSPACING='3' CELLPADDING='3'>" +
"<TR><TH ALIGN='center' VALIGN='top'>Item</TH>" +
"<TH ALIGN='center' VALIGN='top'>Unit Price (�)</TH>" +
"<TH ALIGN='center' VALIGN='top'>Quantity</TH></TR>";
}

private String endWebPage(){
return "<TR><TD ALIGN='center' VALIGN='top' COLSPAN=3>" +
"<INPUT TYPE='submit' VALUE='Continue'>" +
"<INPUT TYPE='reset' VALUE='Clear'></TD></TR></TABLE></FORM></BODY></HTML>";
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{

response.setContentType("text/html");
PrintWriter out = response.getWriter();
Enumeration enum = this.getInitParameterNames();
out.println(this.startWebPage());

while (enum.hasMoreElements()){
String name = ( String )enum.nextElement();
String value = this.getInitParameter( name );
out.println("<TR><TD>" + name + "</TD><TD>" + value + "</TD>");
out.println("<TD ALIGN='left' VALIGN='top'>" +
"<SELECT NAME='choices'>" +
"<OPTION>0" +
"<OPTION>1" +
"<OPTION>2" +
"<OPTION>3" +
"<OPTION>4" +
"</SELECT></TD></TR>");
}
out.println(endWebPage());
}
}

WEB.XML FILE:

code:


<!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>

<servlet>
<servlet-name>getBasketParameters</servlet-name>
<servlet-class>MyBasket</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>getBasketParameters</servlet-name>
<url-pattern>/cart</url-pattern>
</servlet-mapping>





<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>


Main servlet
public class SimpleShoppingBasket extends HttpServlet {

public void init() throws ServletException{
System.out.println("Servlet Initialised");
}

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String parm;
int cost = 0;
int totalcost = 0;
int cost1 = 0;
int totalcost1 = 0;
int cost2 = 0;
int totalcost2 = 0;
int cost3 = 0;
int totalcost3 = 0;
int cost4 = 0;
int totalcost4 = 0;
int grandtotal = 0;


response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();


// Get the price parameter value

parm = request.getParameter("price");
int price = Integer.parseInt(parm);
parm = request.getParameter( "price1");
int price1 = Integer.parseInt(parm);
parm = request.getParameter( "price2");
int price2 = Integer.parseInt(parm);
parm = request.getParameter( "price3");
int price3 = Integer.parseInt(parm);
parm = request.getParameter( "price4");
int price4 = Integer.parseInt(parm);


// Get the item parameter value

String item = request.getParameter("item");
String item1 = request.getParameter("item1");
String item2 = request.getParameter("item2");
String item3 = request.getParameter("item3");
String item4 = request.getParameter("item4");

// Get the quantity parameter value

parm = request.getParameter("choices");
int choices = Integer.parseInt(parm);
parm = request.getParameter( "choices1");
int choices1 = Integer.parseInt(parm);
parm = request.getParameter( "choices2");
int choices2 = Integer.parseInt(parm);
parm = request.getParameter( "choices3");
int choices3 = Integer.parseInt(parm);
parm = request.getParameter( "choices4");
int choices4 = Integer.parseInt(parm);



//calculate the price for each unit
//setting the max quantity of each item
int Sock_quantity = 4;
int Shirt_quantity = 2;
int Trousers_quantity = 1;
int Jumper_quantity = 2;
int Shoes_quantity = 1;



// calculate cost & total for each item

if(price!= 0){

cost += price / Sock_quantity;
totalcost += choices*price;
}

if(price1!= 0){

cost1 += price1 / Shirt_quantity ;
totalcost1 += choices1*price1;

}

if(price2!= 0){

cost2 += price2 / Trousers_quantity ;
totalcost2 += choices2*price2;

}

if(price3!= 0){

cost3 += price3 / Jumper_quantity;
totalcost3 += choices3*price3;

}

if(price4!= 0){

cost4 += price4 / Shoes_quantity ;
totalcost4 += choices4*price4;

}

//Adding the Grandtotal

grandtotal += totalcost + totalcost1 + totalcost2 +totalcost3 +totalcost4;


out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ShoppingCart</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Your Shopping Order</h1>");

out.println("<table BORDER='3' >");
out.println("<th>Item:</th>");
out.println("<th>Cost per unit (�):</th>");
out.println("<th>totalcost:</th>");
out.println("<TR><TD>" + item + "</TD><TD>" +cost + "</TD><TD>" +
totalcost + "</TD></TR>");
out.println("<TR><TD>" + item1 + "</TD><TD>" +cost1 + "</TD><TD>" +
totalcost1 + "</TD></TR>");
out.println("<TR><TD>" + item2 + "</TD><TD>" +cost2 + "</TD><TD>" +
totalcost2 + "</TD></TR>");
out.println("<TR><TD>" + item3 + "</TD><TD>" +cost3 + "</TD><TD>" +
totalcost3 + "</TD></TR>");
out.println("<TR><TD>" + item4 + "</TD><TD>" +cost4 + "</TD><TD>" +
totalcost4 + "</TD></TR>");
out.println("<th>Grandtotal</th>");
out.println("<td></td>");
out.println("<TD ALIGN='right'>" + grandtotal + "</TD>");
out.println("</table>");
out.println("</body>");
out.println("</html>");

out.close();
}



/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39544
    
  27
It looks like all the SELECT elements would be named "choices", instead of "choices0", "choices1", ...


Ping & DNS - updated with new look and Ping home screen widget
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
If this was my problem I would first do a "view source" on the HTML that is actually being written to the browser.
Bill


Java Resources at www.wbrogden.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Initialization Parameters
 
Similar Threads
Error while running jsp
Passing web.xml info to servlets
Forms in servlet
Storing of Data
how to insert multiple value in database