Combine drop down and a text field to a submit button and display "result" using JSP

Posted on 2003-03-27
Medium Priority
Last Modified: 2012-06-27
Hi guys, I'm using JSP to create a web based shopping system. I've never done any JSP before, so I hope you guys can help me out.

What i have done so far is to fill the drop down list with values from a SQL database. I have created a text field and a submit button.

The thing is, when a user makes a choice from the drop down list, the user should then provide the quantity of this product in the text field, and when the choice(drop down) AND the quantity(textfield) has been entered, the user press the submit button called "add to order", and a table should then appear on the same page, providing information about the selected product(prod_code,description), the quantity, the price for the product and total cost. So, for each product the user select, a new entry should be added to the table. The user should also be able to delete selected products one by one.

When the user checks out using a submit buttion, this order should be stored in a database and the user should be redirected to another site giving a summary of the products bought. When the user then exit this page(submit button), the session will be terminated and the tables in the database should be updated.

Tables in the database:

Code for this part so far:
     if (submitValue.equals("newOrder") ) {
     <H1> Rocky Concrete </H1>
     <p><b> Catalogue: </b>
     Connection connection = null;
     Statement stat = null;
     ResultSet rs = null;
     String queryProducts = null;
     connection = DriverManager.getConnection("jdbc:odbc:rocky");
     stat = connection.createStatement();
     queryProducts = "Select prod_code, description, list_price From Products Order by prod_code";

     rs = stat.executeQuery(queryProducts);
     ResultSetMetaData rsmd = rs.getMetaData();
<select name=CAT>
     while (rs.next())
          String prodCode = rs.getString("prod_code");
          String des = rs.getString("description");
          int price = rs.getInt(3);
          <option><%=prodCode%> | <%=des%> | <%=price%>

<b>Quantity: </b> <INPUT type="text" id="quantity"> <INPUT type="submit" value="Add to order">


I hope i did not scare you away from answering this question, i know there is quite a few details and it's a time consuming task, but if anyone are willing to help as soon as possible i would be extremly grateful, and i will for sure give you the best grade and maximum points for helping me out.

Kind regards,
Question by:Schron
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 19

Expert Comment

ID: 8218001
what do you need help with?  The entire thing?  We can't code the entire thing for you b/c that is not what this site is about.  Its about helping and guiding.  We can tell you approaches and guide you with sample/pseudo code but you must make an effort and we can help you along the way.  Sound fair?


Author Comment

ID: 8222566
How can i combine the choice made in the drop down list with the number entered in the textfield and show the result?


Accepted Solution

nimaig earned 2000 total points
ID: 8223068
One way you can do is ... I have explained the steps ... you need to implement the similar.

1. Create a new java class (Value Object)
public class OrderData implements java.io.Serializable

     private Long orderId;
        private Long productId;
        private String productName;
        private long quantity;
        private double price;
        private double unitPrice;

//Create all getter and setter methods for these variables.

2. While loading the product from the db to the dropdown, load the prices of each item and store in a hashtable with productId as the key and Price as the value and store this hashtable in the session object.

3. In the JSP page add one more field
<INPUT type="hidden" id="productName"> to store the selected product name.

4. In the JSP page add one more field
<INPUT type="hidden" id="action"> to store the current action.

5. Make you product option something like this
    <option value="<%=prodCode%>"><%=prodCode%> | <%=des%> | <%=price%></option

6. While submitting the jsp page populate the productName field using javascript. Call this method in onsubmit event of the form (i.e. "onsubmit = validateForm(this)")
  <SCRIPT language="javascript">
     function validateForm(formObj){
       var productNameStr = formObj.CAT.options[selectedIndex].text;
       var nameArr = productNameStr.split("|");
       formObj.productName.value = nameArr[1];
       return true;

7. On the top of the jsp page get the orderHash from session and add/update the selected product to it and put it back to session.

      String action = request.getParameter("action");
      if(action != null && action.equals("add"){
          Long productId = new Long(request.getParameter("CAT"));
          String productName = request.getParameter("quantity");
          long qty = Long.parseLong(request.getParameter("productName"));
          double price = 0;
          Hashtable priceHash = session.getAttribute("priceHash");
          if(priceHash != null){
            price = Double.parseToDouble(priceHash.get(productId));
          Hashtable orderHash = session.getAttribute("orderHash");
          OrderData ord = null;
          if(orderHash != null){
            ord = (OrderData)priceHash.get(productId);
          if(ord == null){
            ord = new OrderData();
          double prevPrice = ord.getPrice();
          ord.setPrice((qty * price) + prevPrice);


8. While displaying do the following
      <TD>Unit Price</TD>
      double totalPrice = 0;
      Hashtable orderHash = (Hashtable)session.getAttribute("orderHash");
      if(orderHash != null && orderHash.size() > 0){
        Enumeration orderEnum = orderHash.elements();
          OrderData ord = (OrderData)orderEnum.nextElement();
          totalPrice += ord.getPrice();
         <TD colspan=4>total : <%= totalPrice%></TD>


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 8223074
You can put a button called done in your JSP page which will submit the page and you can actually add the data from the hashtable(orderHash) in session to the database.

Expert Comment

ID: 8223085
One more thing
 For quantity also
replace the following
     long prevQty = ord.getQuantity();
     ord.setQty(qty + prevQty);

Expert Comment

ID: 8233811
Hmmm i might use some of this for my ITB262 assignment as well :p

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New style of hardware planning for Microsoft Exchange server.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question