wood burning stoves 2.0*
The moose likes JSP and the fly likes JSP form to javabean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP form to javabean" Watch "JSP form to javabean" New topic
Author

JSP form to javabean

Sam Smith
Greenhorn

Joined: Oct 20, 2003
Posts: 7
I have the following JSP page for collecting bill information, the page
validates the data itself but I want to then pass the entered details into a
javabean so I can then store them for later use. My javabean just has simple
get and set methods for storing the info. But how do I get the data into
it???
I have tried using <jsp:setProperty name="myBillBean" property="b1"
param="b1" /> at different points on this page and the start of the next but
either nothing is passed to the Javabean or I just get null.
Have included the Javabean below the JSP page
Any ideas?
S

<%@page import="java.io.*"%>
<%@page import="com.bill.BillBean"%>
<jsp:useBean id="myBillBean" class="com.bill.BillBean" scope="session" />
<%!
// Define constants that represent our states
public static final int DATA_ENTRY = 1;
public static final int INVALID_MISSING_DATA = 2;
public static final int SUBMIT_DATA = 3;
// keep track of our current state
int current_state;
// Variables for storing the HTML form data
String action, b1 ;
%>
<%
action = request.getParameter("action");
b1 = request.getParameter("b1");

// Figure out current state of system, and next permissible action
current_state = DATA_ENTRY; // this is the default
if (action != null && action.equals("Submit Data")) {
current_state = SUBMIT_DATA;
if (mon == null ||!mon.matches("[0-9]*\\.[0-9]{2}")) {
current_state = INVALID_MISSING_DATA;
}
}
%>
<%
if (current_state == DATA_ENTRY || current_state == INVALID_MISSING_DATA) {
%>
<table width="500" border="0"
cellpadding="4" cellspacing="0">
<tr>
<td bgcolor="lightred">
<font color="white" face="Arial"><b>Submit Data</b>
</font></td>
</tr>
<%
if (current_state == INVALID_MISSING_DATA) {
%>
<tr>
<td bgcolor="yellow">
<font color="blue" face="Arial">
<b>You have not entered data correctly </b>
</font></td>
</tr>
<% } %>
<tr>
<td>
<form method="post" action="first2.jsp">
<input type="hidden" name="action" value="Submit Data" />
<table>
<tr><td><b>Monday:</b></td><td><input type="text" name="mon" size="25"
value="<%= b1==null ? "" : b1 %>"></td></tr>
</table><br />
<input type="submit" value="Submit Data">
</form>
</td>
</tr>
</table>
<% } else if (current_state == SUBMIT_DATA) {
response.sendRedirect("next.jsp");
%>

<% } %>
</body>
</html>
Javabean
package com.bill.;
import java.util.*;
/**
A bill JavaBean
*/
public class BillBean {
private String b1;
//New address bill bean with null values (default) for fields.
public BillBean() {
}
//Clears the fields
public void clear() {
b1 = null;
}
//Sets the b1 variable
public void setB1 (String b1) {
this.b1 = b1;
}
// Gets b1
public String getB1() {
return b1;
}
}
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Welcome to javaranch "Smithy "-

have a look at
this article, it explains how you can set the values to the bean.
As you can read, an option is this:
<jsp:setProperty name="yourId" property="*"/>
The container will take all the values from the request and set it to the bean automatically, invoking every setter method you define in your bean. In order to use this "*", your request parameter names must match your properties name in the bean. In your case, you seem to have only b1 in your bean, so it is ok, as long as you have a request parameter b1.
hope this helps


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
By the way, you can also do this if the name of the request parameters match the name of the properties in your bean:
<jsp:setProperty name="yourId" property="yourProperty">
this is the same as:
<jsp:setProperty name="yourId" property="yourProperty" param="yourParam">
Sam Smith
Greenhorn

Joined: Oct 20, 2003
Posts: 7
Andreas,
I know how to set information to a bean using things like <jsp:setProperty name="yourId"....... The problem here is initially I am posting the form to itself for validation once this is done it then needs to post to the bean, and I'm unsure where in the code to put the <jsp:setProperty name="yourId"...... so that it will work. I have tried several places and I just get null values or nothing, but the form valiadates itself perfectly.
Any ideas
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61227
    
  66

Welcome to the Ranch Smithy!
You'll find this forum a great place to seek help on JSP pages, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
Thanks!
bear
JSP Fourm Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sam Smith
Greenhorn

Joined: Oct 20, 2003
Posts: 7
Sorry Bear, all changed
Cheers
Sam Smith
Greenhorn

Joined: Oct 20, 2003
Posts: 7
Figured it out
Thanks for the replies though
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Sam Smith:
Figured it out
Thanks for the replies though

can you share the error so all of us know?
Sam Smith
Greenhorn

Joined: Oct 20, 2003
Posts: 7
OK
Here is what I did, firstly this line of code was wrong
<tr><td><b>Monday:</b></td><td><input type="text" name="mon" size="25"
name actually should ="b1"
and secondly it was just a matter of placing <jsp:setProperty name="myBillBean" property="b1" param="b1" /> in the correct place. If you step through the sequence of the code and think what is happening each step of the way you can figure out where it needs to be placed.
Cheers
 
 
subject: JSP form to javabean