Solved

jsp to bean, bean to database, bean to jsp confirming insert

Posted on 2004-04-14
8
1,066 Views
Last Modified: 2012-06-21
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
Comment
Question by:anna_suchodolska
  • 4
  • 4
8 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
>>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
 

Author Comment

by:anna_suchodolska
Comment Utility
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
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
>>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
 

Author Comment

by:anna_suchodolska
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 30 total points
Comment Utility
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
 

Author Comment

by:anna_suchodolska
Comment Utility
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
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
the property name is the name of the varible in your bean.
it must be this way
hope that helps
Ghost
0
 

Author Comment

by:anna_suchodolska
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maven scope 1 120
maven project in eclipse 11 57
getting param value 7 51
thymeleaf natural templating vs JSP 2 23
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now