 |
| JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 1st, 2004, 04:38 AM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
JSP With Multiple records being added,updated, etc
I am new to developing with Java Server Pages. I have the following requirement:
After logging in, a user will be entering a series of selections with rankings. They might enter only one record, they might enter 100 records. Here is an example:
User 1, Ranking 5, Trip 1
User 1, Ranking 1, Trip 3
User 1, Ranking 2, Trip 7
User 1, Ranking 4, Trip 11
User 1, Ranking 3, Trip 20
This would mean that the user would like Trip 3 as their first choice, Trip 7 as their second choice, etc.
All the books and tutorials I have examined only deal with creating/updating one record per page. For example:
User Name
Address Line 1
Address Line 2
City, State Zip
Phone
E-Mail
<Submit Button>
Is there a good example that demonstrates allowing the user to input a variable number of record on a JSP generated HTML Form. If anyone can point me to a book, tutorial, example, etc. that addresses this topic I would appreciate it.
TIA
Steven C. Gray
Essential Software Solutions(TM)
Providing Essential Software To Businesses Since 1992
|
|

December 1st, 2004, 10:10 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Afraid I have not seen examples of this.
The form part is easy enough, you can use loops within jsp to create as many fields as you need, naming them incrementally. You ask the user to enter a number, and have the page genereate that number of rows in the form.
In the page you send data to, you could pass the number of fields created as the first parameter, and use this to again create loops that update the database accordingly.
If you cannot find a specific tutorial, look at how loops are used in JSP to create HTML and also how they can be used to in SQL statements.
Not very specific I know, but will hopefully point you in the right direction.
|
|

December 1st, 2004, 02:12 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello again,
Here is a very simple example that shows what I mean.
This for loop will create 10 text input fields.
The fields are all named incrementaly, i.e. 1, 2, 3 ...
------------------------------------------
<%@ page import="java.util.*" %>
<html>
<body>
<form name="form1" method="get" action="actionPage.jsp">
<%
for (int i=0; i<=10; i++)
{
%>
<input type="text" name=<%= i %>><br>
<%
}
%>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
-------------------------------------------------
The html generated by this code is:
-------------------------------------------------
<html>
<body>
<form name="form1" method="post" action="">
<input type="text" name=0><br>
<input type="text" name=1><br>
<input type="text" name=2><br>
<input type="text" name=3><br>
<input type="text" name=4><br>
<input type="text" name=5><br>
<input type="text" name=6><br>
<input type="text" name=7><br>
<input type="text" name=8><br>
<input type="text" name=9><br>
<input type="text" name=10><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
-------------------------------------------------
You can see how the loop named them all seperately.
If you use a variable (in this example userInput) in the loop instead of a literal value:
for (int i=0; i<=userInput; i++)
your users can add as many rows as they want to the form.
Make sure you put the form tags, submit buttons OUTSIDE the loop, you do not want to generate 10 seperate forms!
|
|

December 1st, 2004, 09:29 PM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the advice. I will try out your idea.
Steven C. Gray
Essential Software Solutions(TM)
Providing Essential Software To Businesses Since 1992
|
|
 |