Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1090
  • Last Modified:

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
0
anna_suchodolska
Asked:
anna_suchodolska
  • 4
  • 4
1 Solution
 
copyPasteGhostCommented:
>>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?

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
0
 
anna_suchodolskaAuthor Commented:
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?
0
 
copyPasteGhostCommented:
>>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.jdbc.Driver").newInstance();
          connection = DriverManager.getConnection(connectionURL);
        } catch(Exception e) {
           System.err.println("Connection 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(sqlQuery);
    } 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(sqlQuery);
    } 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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
anna_suchodolskaAuthor Commented:
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(sqlQuery);
    } 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(sqlQuery);
    } 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?)Property 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.

0
 
copyPasteGhostCommented:
you would do something like this..

<%@ page session = "true"  import="java.util.*, java.lang.*"%>

<jsp:useBean id="customer" class="business.Customer" scope="session" />    

<html>
...
<td align="right">Company name</td>
<td align=top><input type="text" name="company" size="40"  value="<jsp:getProperty name='customer' property='company' />" />
...

that should do it.

that code I showed you was a javabean

on your jsp page you would do this..
<jsp:useBean class="dbCode.DBConnect" id="dbBean" scope="page" />
<%
ResultSet rs = null;
String sqlQuery = "":

sqlQuery = "Select * from myTable";
rs = dbBean.getResults(sqlQuery );

and then you do what you want with the results since they are in a result set.
hope that helps,
Cheers,
Ghost
0
 
anna_suchodolskaAuthor Commented:
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
0
 
copyPasteGhostCommented:
the property name is the name of the varible in your bean.
it must be this way
hope that helps
Ghost
0
 
anna_suchodolskaAuthor Commented:
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...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now