anna_suchodolska
asked on
jsp to bean, bean to database, bean to jsp confirming insert
Hi,
My question is: Can I store data entered on jsp in a bean and use this bean to validate them and insert them to the database? I need then to get these data on the next jsp to display confirmation. What is the method?
Do I need to use any servets to help it? What classes should I import to my bean and to my jsps?
How do I call the bean in the jsp? And how do I make sure that the data is in th edatabase?
I am not confident with using this class which I think is
import com.ora.jsp.util.*;
How else can I validate that the email, date or a card format are correct?
And why would I use "this" keyword in the bean?
Thank you,
Anna
My question is: Can I store data entered on jsp in a bean and use this bean to validate them and insert them to the database? I need then to get these data on the next jsp to display confirmation. What is the method?
Do I need to use any servets to help it? What classes should I import to my bean and to my jsps?
How do I call the bean in the jsp? And how do I make sure that the data is in th edatabase?
I am not confident with using this class which I think is
import com.ora.jsp.util.*;
How else can I validate that the email, date or a card format are correct?
And why would I use "this" keyword in the bean?
Thank you,
Anna
ASKER
Thank you Ghost,
property in jsp should be the same as private variables in the bean, am I right?
The thing is, as I enter the data on th eform (jsp) and they get stored in the bean, I would like to choose some mechanism to send them to the database, and then display a confirmation page.
I belive that it will happen like that jsp -> save servlet -> confirmation jsp(which will retrieve data from the bean).
Question: can it be done more effciently? Or is it the only way?
Can I use normal javabean to query the database and insert or retrieve data through it?
How would you go about it? Can you suggest something?
By the way it is a "customer" bean. A clerk will be creating many customers per day and login orders for them. What scope would you give to such a bean? Is session appropriate? What will happen when a clerk attempts to create a new customer. Will the old one still be in the session?
property in jsp should be the same as private variables in the bean, am I right?
The thing is, as I enter the data on th eform (jsp) and they get stored in the bean, I would like to choose some mechanism to send them to the database, and then display a confirmation page.
I belive that it will happen like that jsp -> save servlet -> confirmation jsp(which will retrieve data from the bean).
Question: can it be done more effciently? Or is it the only way?
Can I use normal javabean to query the database and insert or retrieve data through it?
How would you go about it? Can you suggest something?
By the way it is a "customer" bean. A clerk will be creating many customers per day and login orders for them. What scope would you give to such a bean? Is session appropriate? What will happen when a clerk attempts to create a new customer. Will the old one still be in the session?
>>property in jsp should be the same as private variables in the bean, am I right?
yes it must be.
if I was you I would have 2 beans.. one Customer and one called DBConnect or something like that...
the dbconnect bean would like like this..
package org.ghostteam.dbCode;
import java.sql.*;
import java.util.*;
import java.io.*;
public class DBConnect {
String connectionURL;
Connection connection;
Statement statement;
public DBConnect() {
connectionURL = "";//type in your connection string
connection = null;
statement = null;
try {
Class.forName("com.mysql.j dbc.Driver ").newInst ance();
connection = DriverManager.getConnectio n(connecti onURL);
} catch(Exception e) {
System.err.println("Connec tion to database failed");
e.printStackTrace();
}//end try
}
/**
* This method will accept a sql query string and then return the result set.
*
*/
public ResultSet getResults(String sqlQuery) {
ResultSet rs = null;
try {
statement = connection.createStatement ();
rs = statement.executeQuery(sql Query);
} catch (Exception sqlex) {
System.out.println("An execption occured while trying to get the " +
"records from the database for one Day.");
sqlex.printStackTrace();
}//end try
return rs;
}
/**
* This method will accept a sql update query and update the database.
*
*/
public void updateDB(String sqlQuery) {
int done = 0;
try {
statement = connection.createStatement ();
done = statement.executeUpdate(sq lQuery);
} catch (Exception sqlex) {
System.out.println("An execption occured while trying to get the " +
"records from the database for one Day.");
sqlex.printStackTrace();
}//end try
}
}
jsp sessions last 30 min by default (I think, pretty sure..but not enough to bet my life on it.. I know tomcat keeps them for 30 min)
The actualy customer bean can and should have session scope...
but the DBConnect bean...I would give that one page scope
Anything else?
Ghost
yes it must be.
if I was you I would have 2 beans.. one Customer and one called DBConnect or something like that...
the dbconnect bean would like like this..
package org.ghostteam.dbCode;
import java.sql.*;
import java.util.*;
import java.io.*;
public class DBConnect {
String connectionURL;
Connection connection;
Statement statement;
public DBConnect() {
connectionURL = "";//type in your connection string
connection = null;
statement = null;
try {
Class.forName("com.mysql.j
connection = DriverManager.getConnectio
} catch(Exception e) {
System.err.println("Connec
e.printStackTrace();
}//end try
}
/**
* This method will accept a sql query string and then return the result set.
*
*/
public ResultSet getResults(String sqlQuery) {
ResultSet rs = null;
try {
statement = connection.createStatement
rs = statement.executeQuery(sql
} catch (Exception sqlex) {
System.out.println("An execption occured while trying to get the " +
"records from the database for one Day.");
sqlex.printStackTrace();
}//end try
return rs;
}
/**
* This method will accept a sql update query and update the database.
*
*/
public void updateDB(String sqlQuery) {
int done = 0;
try {
statement = connection.createStatement
done = statement.executeUpdate(sq
} catch (Exception sqlex) {
System.out.println("An execption occured while trying to get the " +
"records from the database for one Day.");
sqlex.printStackTrace();
}//end try
}
}
jsp sessions last 30 min by default (I think, pretty sure..but not enough to bet my life on it.. I know tomcat keeps them for 30 min)
The actualy customer bean can and should have session scope...
but the DBConnect bean...I would give that one page scope
Anything else?
Ghost
ASKER
That was really good, thank you Ghost.
So I understand that the sqlQuery will be SELECT FROM...
public ResultSet getResults(String sqlQuery) {
ResultSet rs = null;
try {
statement = connection.createStatement ();
rs = statement.executeQuery(sql Query);
} catch (Exception sqlex) {
System.out.println("An execption occured while trying to get the " +
"records from the database for one Day.");
sqlex.printStackTrace();
}//end try
return rs;
}
and here INSERT to...
public void updateDB(String sqlQuery) {
int done = 0;
try {
statement = connection.createStatement ();
done = statement.executeUpdate(sq lQuery);
} catch (Exception sqlex) {
System.out.println("An execption occured while trying to get the " +
"records from the database for one Day.");
sqlex.printStackTrace();
}//end try
}
that's great, if I am right, but, what do I do with the result? Can I insert html code here (but that would have to be quite a large chunk of code) or should I somehow get it on another jsp and display it there? How can I do it?
One more thing which I can't get is how to set the properties into a bean from a form on jsp
let's say that I have a form like that:
<%@ page session = "true" import="java.util.*, java.lang.*"%>
<jsp:useBean id="customer" class="business.Customer" scope="session" />
<jsp:setProperty name="customer" property="*"/>
<html>
...
<td align="right">Company name</td>
<td align=top><input type="text" name="company" size="40" $$$>
...
I want the entered data to be placed in my bean
do I use get or set property for that?
Can I place this bit instead of $$$?
value="<jsp:get(set?)Prope rty name ="customer" property="companyName"/>">
I've seen code like that somewhere but I think that get is used to retrieve data from a bean not to set it. Or maybe get is getting it from the entered value? I am not sure.
So I understand that the sqlQuery will be SELECT FROM...
public ResultSet getResults(String sqlQuery) {
ResultSet rs = null;
try {
statement = connection.createStatement
rs = statement.executeQuery(sql
} catch (Exception sqlex) {
System.out.println("An execption occured while trying to get the " +
"records from the database for one Day.");
sqlex.printStackTrace();
}//end try
return rs;
}
and here INSERT to...
public void updateDB(String sqlQuery) {
int done = 0;
try {
statement = connection.createStatement
done = statement.executeUpdate(sq
} catch (Exception sqlex) {
System.out.println("An execption occured while trying to get the " +
"records from the database for one Day.");
sqlex.printStackTrace();
}//end try
}
that's great, if I am right, but, what do I do with the result? Can I insert html code here (but that would have to be quite a large chunk of code) or should I somehow get it on another jsp and display it there? How can I do it?
One more thing which I can't get is how to set the properties into a bean from a form on jsp
let's say that I have a form like that:
<%@ page session = "true" import="java.util.*, java.lang.*"%>
<jsp:useBean id="customer" class="business.Customer" scope="session" />
<jsp:setProperty name="customer" property="*"/>
<html>
...
<td align="right">Company name</td>
<td align=top><input type="text" name="company" size="40" $$$>
...
I want the entered data to be placed in my bean
do I use get or set property for that?
Can I place this bit instead of $$$?
value="<jsp:get(set?)Prope
I've seen code like that somewhere but I think that get is used to retrieve data from a bean not to set it. Or maybe get is getting it from the entered value? I am not sure.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It does help!
Just the last thing, it may be very important.
<td align=top><input type="text" name="company" size="40" value="<jsp:getProperty name='customer' property='company' />" />
the variable in my bean is called companyName, so shoud the property, as you said. I understand that you gave me an example here, but I'm not sure about one thing.
Does the name in name="company" have anything to do with the property or can it be called whatever else.
Thank you for your time
Just the last thing, it may be very important.
<td align=top><input type="text" name="company" size="40" value="<jsp:getProperty name='customer' property='company' />" />
the variable in my bean is called companyName, so shoud the property, as you said. I understand that you gave me an example here, but I'm not sure about one thing.
Does the name in name="company" have anything to do with the property or can it be called whatever else.
Thank you for your time
the property name is the name of the varible in your bean.
it must be this way
hope that helps
Ghost
it must be this way
hope that helps
Ghost
ASKER
I just remembered that at the beginning I asked about "this" keyword, in a javabean. Is it still possible to get some info about it? Why some beans use it some don't?
15/04/2004 12:05PM...
15/04/2004 12:05PM...
yes you can
example..
if this is your bean
package myPac;
public class MyBean{
private int myInt;
public MyBean(){
myInt = 0;
}
public void setMyInt(int x){
myInt = x;
}
public int getMyInt(){
return myInt;
}
}
on your jsp page you need to do something like this..
<jsp:useBean id = "myBean" scope = "session" class = "myPac.MyBean" />
you would do this to set the value into the bean
<jsp:setProperty name = "myBean" property = "myInt" value="3" />
you would do this to retrieve the value from the bean
<jsp:getProperty name = "myBean" property = "myInt" />
You must be using this directory structure
-jspfiles
-WEB-INF
-classes
-myPac
-MyBean.class
you don't need to import anything..
for validation use some javascript.
This will work.
hope that helps,
Ghost