By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,736 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,736 IT Pros & Developers. It's quick & easy.

JSP help

P: 1
Hello, I have a project using JSP and JSTL. I have to forward one JSP to another. The problem that I am having is understanding what JSTL tags to loop through and get the information from the java class by using the
<c:when>, <c:choose> tags. I have index.jsp, plants.jsp and a Customers.java file. The customer has to what type of plant they want, enter a quanity, and put in their id number. I have 2 errorr messages, one if that they need to order over a quarnity of 24 and one is not a valid id. When I get to retrieving the customer id I don't know how to use the correct tags to get the customer Id. It just keeps giving me the error message. Can someone tell me how I would write the the tags to get the customer id and list the name of the customer on the output page.

Here is my code for index.jsp
Expand|Select|Wrap|Line Numbers
  1.  
  2. %@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3.   <%@ taglib prefix="pl" uri = "/jsp/plants.tld" %>
  4.  
  5. <c:choose>
  6.   <c:when test="${empty param.action}">
  7.      <jsp:forward page="plants.jsp"/>  
  8.   </c:when>
  9.  
  10.  
  11.   <c:when test="${param.action eq 'purchased'}">
  12.  
  13.      <jsp:useBean id="mistake" class="plants.Mistake" scope="request">
  14.         <jsp:setProperty name="mistake" property="msg" value="Sorry, you must buy at least two dozen. Please enter again."/>
  15.      </jsp:useBean>
  16.  
  17.      <c:if test="${(param.number <= 24) }">        
  18.          <jsp:forward page="plants.jsp"/> 
  19.      </c:if> 
  20.  
  21.      <c:set var= "present" value = "${param.custId}" scope="application" /> 
  22.  
  23.     ${pl:unloadCust()}
  24.     ${pl:loadCust()}
  25.     ${pl:checkCust(present)}
  26.  
  27.     <c:if test="${!pl:checkCust(present)}"> 
  28.         <jsp:setProperty name="mistake" property="msg" value="Sorry, invalid ID. Please enter again."/> 
  29.          <jsp:forward page="plants.jsp"/>   
  30.      </c:if>
  31.  
  32.  
  33.      <jsp:useBean id="purchase" class="plants.Purchase" scope="request">
  34.         <jsp:setProperty name="purchase" property="*"/> 
  35.      </jsp:useBean>
  36.  
  37.      <jsp:forward page="showPurchase.jsp"/>     
  38.  
  39. </c:when>
  40. </c:choose>
  41.  

Here is my code for plants.jsp

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <html>
  4. <head>
  5. <link rel=stylesheet type="text/css" href="sales.css">
  6. <title>Plant Sales</title></head>
  7. <body>
  8. <table class="mainBox" width="386">
  9. <tr>
  10. <c:if test="${!(empty mistake)}">
  11.   <tr>
  12.     <td class="errorText" colspan="2">
  13.        ${mistake.msg}
  14.     </td>
  15.   </tr>
  16. </c:if>
  17.  
  18. <td width="352" colspan="2" class="boxTitle" align="center"><h1>Plant Sales</h1></td>
  19. </tr>
  20. <tr><td>
  21. <form  action="index.jsp" method="get">
  22. <table style="height: 215px; width: 405px">
  23. <tr>
  24. <td width="145" style="width: 177px">Plants to purchase :<td width="175">
  25. <select name="item">
  26.   <option>Hosta</option>
  27.   <option>Orchid</option>
  28.   <option>Alyssum</option>
  29. </select>
  30. <input type="hidden" name="action" value="purchased"/>
  31. </td>
  32. </tr>
  33. <tr>
  34. <td>Number desired:</td>
  35.  
  36. <td><label>
  37.   <input type="text" name="number" id="number">
  38. </label>
  39.   </td></tr>
  40. <tr>
  41. <td>Customer Id :</td>
  42.  
  43. <td><label>
  44. <c:forEach var="present" items="${param.custId}" >
  45. <c:choose>
  46. <c:when test="${pl:checkCust(present)}">
  47.         </c:choose>
  48. <c:otherwise>
  49.         <c:if test="${!pl:checkCust(present)}">
  50.         </c:if>
  51.       </c:otherwise>
  52.     </c:choose>
  53.   </c:when>
  54.   </c:forEach>
  55.  
  56.  
  57.  
  58. <input type="text" name="action" id="${param.custId}">
  59. </label>
  60.   </td></tr>
  61.  
  62.  <tr style="height: 51px"><td colspan="2" align="center">
  63. <input type="submit" value="OK"/>
  64. </td></tr>
  65. </table>
  66. </form>
  67. </td></tr></table>
  68. </body>
  69. </html>
  70.  
Here is my customers.java

Expand|Select|Wrap|Line Numbers
  1.  
  2. package plants;
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. import java.util.*;
  10.  
  11.  
  12.  
  13.  
  14. public class Customers
  15. {
  16.  
  17.     private static ArrayList cust = new ArrayList();
  18.     // write an empty constructor
  19.     public Customers()
  20.     {
  21.  
  22.     }
  23.  
  24.     public static void loadCust()
  25.     {
  26.  
  27.  
  28.         cust.add(new Customer(123, "Donald", "Duck"));
  29.         cust.add (new Customer(124, "Mickey", "Mouse"));
  30.         cust.add(new Customer (125, "Minnie", "Mouse"));
  31.     }
  32.     public static void unloadCust()
  33.     {
  34.  
  35.             cust.clear();
  36.         // write code to clear out all the customers from the ArrayList
  37.     }
  38.  
  39.     public static  boolean checkCust(int id)
  40.     {
  41.         boolean found = false;
  42.         int i=0;
  43.         // note a while loop since we want to stop once we find them
  44.         // each one should be present only once!
  45.         while (!found && i<cust.size())           // two ways to end - we find it or we get to the end 
  46.         {
  47.             Customer c = (Customer) cust.get(i);
  48.             int Num = c.getCustId();
  49.             if(Num==id)                        // found it!!
  50.             {
  51.                 found = true;                         // set found to true to end the loop
  52.                 return true;   // print it out
  53.             }
  54.             else
  55.                 i++;                                // look at the next one
  56.         }
  57.  
  58.         if (!found)                                    // check to see if it ever was found    
  59.  
  60.         return found;
  61.         return false;
  62.  
  63.         }
  64.  
  65.  
  66.         // write code to look for a Customer with the id parameter
  67.         // return true or false
  68.  
  69.     public static  String getCust(int id)
  70.     {
  71.         boolean found = false;
  72.         int i=0;
  73.         // note a while loop since we want to stop once we find them
  74.         // each one should be present only once!
  75.         while (!found && i<cust.size())           // two ways to end - we find it or we get to the end 
  76.         {
  77.             Customer c = (Customer) cust.get(i);
  78.             int Num = c.getCustId();
  79.             if(Num==id)                        // found it!!
  80.             {
  81.                 found = true ;                         // set found to true to end the loop
  82.                 c.getName() ;  // print it out
  83.             }
  84.             else
  85.                 i++;                                // look at the next one
  86.         }
  87.  
  88.         if (!found);
  89.         return "Nobody";
  90.         // check to see if it ever was found    
  91.  
  92.  
  93.  
  94.         }
  95.  
  96.  
  97.  
  98.  
  99.     public static String print()
  100.     {
  101.          return print();
  102.  
  103.  
  104.     }
  105.  
  106.  
  107.  
  108.  
  109. }
  110.  
  111.  
Here is my showpurchase.jsp

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4. <link rel=stylesheet type="text/css" href="sales.css">
  5. <title>Plant Purchased</title></head>
  6. <body>
  7. <table class="mainBox" width="600">
  8. <tr><td class="boxTitle" colspan="2">
  9. Your Order
  10. </td></tr>
  11. <tr><td colspan="2">&nbsp;</td></tr>
  12. <tr><td class="tableLabel" width="30%">
  13.   Items Purchased
  14. </td>
  15. <td class="tableCell">
  16. ${purchase.item}</td>
  17. </tr>
  18. <tr><td class="tableLabel">
  19. Item Number
  20. </td>
  21. <td class="tableCell">
  22. ${purchase.number}</td>
  23. </tr>
  24. <tr>
  25.   <td>Customer Id :</td>
  26.  <td><input type="text" name="Id" id="Id"></td>
  27. </tr>
  28. </table>
  29. </body>
  30. </html>
  31.  
  32.  
Mar 4 '08 #1
Share this Question
Share on Google+
1 Reply


Dököll
Expert 100+
P: 2,364
Thanks for posting your code...

Looks like you got yourself pretty far. Perhaps a tutorial may help shed some light:Using JSTL

Not time to run your code, just passing through...
Dec 13 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.