Hi,
I'm relatively new to
JSP and
Java and have a question which has been bugging me and I can't seem to find a good solution.
I use a javabean to store customer information entered on a JSP page. This information is then validated and stored in a database. Currently I'm using a session level bean instantiated in the JSP to manage the database connection. This means (or does it, this is my question) that I have to pass the session as a parameter to the save() method of the Customer bean so that it can then get the database connection.
Is there a way for the bean to get it's session context without passing it to a method.
My code currently looks something like this.
JSP Page ***************************
<jsp:useBean id="customer" scope="page" class="com.mwp.CustomerDetails" >
<jsp:setProperty name="customer" property="*"/>
</jsp:useBean>
<jsp:useBean id="dbm" scope="page" class="com.mwp.DatabaseManager" >
</jsp:useBean>
<%
String display;
String message = null;
HashMap v = customer.validate(dbm.getConnection(session));
session.removeAttribute("validation");
if ( v.isEmpty() ) {
try {
customer.save(dbm.getConnection(session));
session.setAttribute("message",message);
String custId = customer.getCustomerID();
session.setAttribute("custId",custId);
session.removeAttribute("customer");
display="customerAdded.jsp";
} catch (SQLException e) {
session.setAttribute("message",e.getMessage());
session.setAttribute("customer",customer);
display="customerEdit.jsp";
}
} else {
message = "Errors in form see below for details";
session.setAttribute("message",message);
session.setAttribute("customer",customer);
session.setAttribute("validation",v);
display = "customerEdit.jsp";
}
%>
<jsp:forward page="<%= display %>"/>
DatabaseManager Bean code *************************
public class DatabaseManager
{
String driverName;
String dbURL;
String dbUser;
String dbPassword;
String session;
public DatabaseManager()
{
// Default database parameters here.
driverName = "oracle.jdbc.driver.OracleDriver";
dbURL = "jdbc
racle:thin:@192.168.0.4:1521
ELLXP8I";
dbUser = "mwp";
dbPassword = "mwp";
}
public String getDriverName()
{
return driverName;
}
public void setDriverName(String newDriverName)
{
driverName = newDriverName;
}
public String getDbURL()
{
return dbURL;
}
public void setDbURL(String newDbURL)
{
dbURL = newDbURL;
}
public String getDbUser()
{
return dbUser;
}
public void setDbUser(String newDbUser)
{
dbUser = newDbUser;
}
public String getDbPassword()
{
return dbPassword;
}
public void setDbPassword(String newDbPassword)
{
dbPassword = newDbPassword;
}
public String getSession()
{
return session;
}
public void setSession(String newSession)
{
session = newSession;
}
public Connection getConnection(HttpSession session)
throws Exception, SQLException
{
Connection conn = (Connection)session.getAttribute("conn");
if ( conn == null ) {
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL,dbUser,dbPassword);
session.setAttribute("conn", conn);
}
return conn;
}
}
Customner Java bean save method. *************************
public void save(Connection conn) throws SQLException
{
String sql = null;
PreparedStatement insertStmt;
conn.setAutoCommit(false);
if (getUpdateStatus().equals("NEW")) { // It's a new Customer.
sql = "INSERT INTO CUSTOMER"
+ "( CustomerID, UserID, Password, BusinessName, FirstName, Surname, MiddleName, Salutation, Sex, DOB, DefaultAddressID, CustomerTypeID)"
+ " VALUES (?,?,?,?,?,?,?,?,?,TO_DATE(?,'DDMMYYYY'),?,1 )";
try {
insertStmt = conn.prepareStatement(sql);
// Get sequences
customerID = OracleSequence.getNextVal(conn, "CUSTOMER_SEQ" );
mailingAddressID = OracleSequence.getNextVal(conn, "ADDRESS_SEQ" );
billingAddressID = OracleSequence.getNextVal(conn, "ADDRESS_SEQ" );
...
}
Any ideas would be appreciated.
Thanks