473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

JSP help

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
1 1797
Dököll
2,364 Expert 2GB
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

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.