Using beans, please help me clarify

Please, anyone know how beans work in JSP? i mean i m trying to use a bean but its properties are not initialized within the jsp page that calls it, what i have is some method inside the bean that when i call it it initializes all of the properties... for example lets say i have bean called TestBean.java, and inside of it i have 2 properties private String test1, test2; Then i have the method called

public class TestBean {

private String test1, test2

public TestBean (){

test1 = "";

test2 = "";

}

public void init(String s) {

test1 = s; test2 = s;

}

...

now in my jsp application i got <jsp:useBean id="testing" class="TestBean" scope="session"> </jsp:useBean>

<% testing.init("some string"); %>

... <%= testing.getTest1() /*returns blank value instead of a string "some string" */ %>

///now when i try and access the bean properties all i get is blank string that was initialized by constructor... why doesnt the 'init(String)' method initialize all my properties? Thanks in advance

[1106 byte] By [amulla02a] at [2007-9-19]
# 1
I am new here, this is just a test, please forgive me:)
daFeia at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2
check your getTest1() method. sometimes one would take it for granted...what you were doing was ok. it is very much the same as when you use beans in regular coding.
daFeia at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3
all the methods are properly set i double checked everything, just for some reason the instance keeps getting initialized by constructer eventhough i specify the initializing method inside the jsp code...
amulla02a at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

Ok so here is my code and I haven抰 got a slightest idea how come I cant initialize these guys, database and other aspects all work fine since I did other apps with this?br>

PersonalInfoBean.java

-

import java.util.*;

import java.sql.*;

public class PersonalInfoBean {

//private String ssn

private String fname;

private String lname;

private String street;

private String aptNumber;

private String city;

private String state;

private String zipcode;

private String dayPhone;

private String nightPhone;

private Hashtable errors;

public PersonalInfoBean(){

//ssn = "";

fname = "test1";

lname = "test2";

street = "test3";

aptNumber = "";

city = "";

state = "";

zipcode = "";

dayPhone = "";

nightPhone = "";

}

//public void setSsn(String s){

//ssn = s;

//}

public void setFname(String name1) {

fname = name1;

}

public void setLname(String name2) {

lname = name2;

}

public void setStreet(String st){

street = st;

}

public void setAptNumber(String apt){

aptNumber = apt;

}

public void setCity(String c){

city = c;

}

public void setState(String s){

state = s;

}

public void setZipcode(String z){

zipcode = z;

}

public void setDayPhone(String day){

dayPhone = day;

}

public void setNightPhone(String night){

nightPhone = night;

}

//public String getSsn(){

//return ssn;

//}

public String getFname(){

return fname;

}

public String getLname(){

return lname;

}

public String getStreet(){

return street;

}

public String getAptNumber(){

return aptNumber;

}

public String getCity(){

return city;

}

public String getState(){

return state;

}

public String getZipcode(){

return zipcode;

}

public String getDayPhone(){

return dayPhone;

}

public String getNightPhone(){

return nightPhone;

}

public void existingUser(String ssn) {

try{

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library",

"access", "granted");

Statement stmt = conn.createStatement();

ResultSet rs;

rs = stmt.executeQuery("SELECT * FROM client WHERE ssn LIKE " + ssn + 搾?;

fname = rs.getString("Fname");

lname = rs.getString("Lname");

rs = stmt.executeQuery("SELECT * FROM address WHERE ssn LIKE '" + ssn + "'");

setStreet(rs.getString("street"));

setAptNumber(rs.getString("aptNumber"));

setCity(rs.getString("City"));

setState(rs.getString("State"));

setZipcode(rs.getInt("Zipcode") + "");

rs = stmt.executeQuery("SELECT * FROM phonenumber WHERE ssn LIKE '" + ssn + "'");

setDayPhone(rs.getString("DayPhone"));

setNightPhone(rs.getString("EveningPhone"));

}

catch (Exception e){

e.getMessage();

}

}

}

info.jsp

<%@ page import="java.util.*" %>

<html>

<head></head>

<jsp:useBean id="privateInfo" class="PersonalInfoBean" scope="session">

</jsp:useBean>

<% String SSN = (String)session.getAttribute("SSN"); %>

<% privateInfo.existingUser(SSN); %>

<body> Your personal Information:

<table cellpadding=2 cellspacing=2 border=1>

<tr>

<td><b>First Name</b> <jsp:getProperty name="privateInfo" property="fname"/> </td>

<td><b>Last Name</b> <jsp:getProperty name="privateInfo" property="lname"/> </td>

</tr>

<tr>

<td><b>Street</b> <jsp:getProperty name="privateInfo" property="street"/> </td>

<td><b>Apartment</b> <jsp:getProperty name="privateInfo" property="aptNumber"/> </td>

</tr>

<tr>

<td><b>City</b> <jsp:getProperty name="privateInfo" property="city"/> </td>

<td><b>State</b> <jsp:getProperty name="privateInfo" property="state"/> </td>

<td><b>Zipcode</b> <jsp:getProperty name="privateInfo" property="zipcode"/> </td>

</tr>

<tr>

<td><b>Day Phone</b> <jsp:getProperty name="privateInfo" property="dayPhone"/> </td>

<td><b>Evening Phone</b> <jsp:getProperty name="privateInfo" property="nightPhone"/> </td>

</tr>

</table>

</body>

</html>

amulla02a at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5

you need to add a line before extracting data from rs:

rs = stmt.executeQuery("SELECT * FROM client WHERE ssn LIKE " + ssn + 搾?;

//--

while (rs.next())

{

fname = rs.getString("Fname");

lname = rs.getString("Lname");

}

//--

fname = rs.getString("Fname");

lname = rs.getString("Lname");

daFeia at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6
hmm i see, this should solve the problem, here is some duke dollars for ya :) thank you
amulla02a at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7
i shall collect dollars when i return...
daFeia at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...