Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Bean to bean communication

Hi!

I am running a JSP web application which displays data from a mySQL database. I have developed a collection of Beans to take care of a lot of the calculations, databaseaccess and so on.

Fx. I have a DatabaseBean with the method :  

                   ResultSet executeQuery(String query, String username, String password)

I can then from the JSP use this method like this:

<%
ResultSet rs = DatabaseBean.executeQuery("Select username from user where userID=3","myname","mypw");
%>

I would very much like to be able to let the beans communicate with each other, but I don't know how! Fx. it would be nice to use the allready written DatabaseBean-methods in my other beans, or just to exchange variables between beans.

At the moment I can only make beans communicate via the jsp-pages, and it is troublesome!

Can you help?


0
NicolaiKrog
Asked:
NicolaiKrog
  • 2
  • 2
  • 2
  • +1
1 Solution
 
thanassisCommented:
A bean is a java class. So you can make other classes that use your bean classes.
For example you have a bean that collect passengers (constracted from the file passList.java).
You have another that has airplanes(airplanesList.java)
and one more that has airseats(airseatsList.java)

You can make a java class that use the above to constract a trip:

class Trip{
 PassList;
 AirPlaneList;
 AirSeatsList;
 DbConnection;
 
}

andin your jsp page you can use only the trip bean!
0
 
allahabadCommented:
There is two way to do this :
1. You can create  your class instance (which is your bean) for all your beans with <%jsp:useBean ..> by giving it relevant scope., and use then bean name(id) to invoke any method on that class.

<jsp:useBean id="yourBeanName" class="xx.yy.YourBeanClass" scope="request"/>
// give full qualified path name
// Here yourBeanName , reference of YourBeanClass will be created and destroyed for each request.

2. Other way to import that class in page directive, and create the instance in the JSP when you need that, and then call its method.
<%@ page import="xx.yy.YourBeanClass" %>
...
...
<%  //for ex.
   YourBeanClass bean  = new YourBeanClass();
   String name = bean.getName();
%>
0
 
NicolaiKrogAuthor Commented:
Hi, thanks for the comments!

I fear that I have not made my question perfectly clear, - I am new in this environment, but I am not sure that your comments actually are answers to my question.
Let me try with a concrete (but invented) example:

First we have a DatabaseBean:

class DatabaseBean {
    public ResultSet exectuteQuery(String query){
         ResultSet rs=null;
         // code that connects to a mySQL database.
         // executes the command "query" and puts
         // the result in "rs".
         return rs;
    }
}

Then we want to create an InformationBean, that also uses
the method executeQuery that is implemented in DatabaseBean, - something like this:

class InformationBean{
import DatabaseBean;
public String getUsername(int id){
  ResultSet rs=DatabaseBean.executeQuery
  ("Select username from persons where userID="+id);
  rs.next();
  return rs.getString("username");
}

This does of course NOT work,- but can you tell me how to make it work?? This is my question!

Lets just for now forget all about jsp; this problem is about how the beans can communicate without the JSP-pages!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
kennethxuCommented:
I guess you closed the connection in exectuteQuery() method. once you close your connection, ResultSet is closed too.
0
 
allahabadCommented:
class InformationBean{
dbBean DatabaseBean;
public String getUsername(int id){
  dbBean = new DatabaseBean(); // get DatabaseBean object
ResultSet rs=DatabaseBean.executeQuery(
 ("Select username from persons where userID="+id); // call the method
}
0
 
kennethxuCommented:
may be you want to look into disconnected RowSet:
http://java.sun.com/j2se/1.4/docs/api/javax/sql/RowSet.html
0
 
NicolaiKrogAuthor Commented:
THANK YOU!

Yes, it was that simple! - Embarrasing......
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now