Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-04-14
8
1,073 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
ID: 10825605
>>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
ID: 10827802
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
ID: 10828140
>>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
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.

 

Author Comment

by:anna_suchodolska
ID: 10828522
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
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 30 total points
ID: 10828587
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
ID: 10828991
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
ID: 10829214
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
ID: 10831920
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to reset your Windows 10 password when you've forgotten it.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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