wood burning stoves 2.0*
The moose likes Servlets and the fly likes Struggling With Passing Data Between Data Access Class, Business Bean, and Controller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Struggling With Passing Data Between Data Access Class, Business Bean, and Controller" Watch "Struggling With Passing Data Between Data Access Class, Business Bean, and Controller" New topic
Author

Struggling With Passing Data Between Data Access Class, Business Bean, and Controller

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
The root of my problem is that I do not know how to pass data that are wrapped in a bean between classes.
1. I have a controller Servlet that reads the "username" that is supplied by a visitor of the web site. I want to search the database by the "username" and get some information about that user; such as userrole, category, etc. A business bean sits in between the controller servlet and the database. Here is the partial code of the controller servlet:

2. I have this EditorBean class. This EditorBean is a business bean. I am struggling with how the EditorBean pass the data in between the controller and the database.

3. Here is my data access class:

[ November 27, 2003: Message edited by: JiaPei Jen ]
[ November 27, 2003: Message edited by: JiaPei Jen ]
Rufus BugleWeed
Ranch Hand

Joined: Feb 22, 2002
Posts: 1551
The only data you can pass between classes is primitives and that's often not enough. You have to pass classes between classes.
I am trying to work within your conceptual context. I am probably neglecting a bunch of possible exceptions too.
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
No, it is not what I am asking. I know how to wrap information in a session object and pass the session object.
My question is I want to wrap data that are found in the database in a business bean, and pass this bean from the database via the business bean back to my controller servlet.
Rufus BugleWeed
Ranch Hand

Joined: Feb 22, 2002
Posts: 1551
Are you familiar with javax.servlet.ServletContext.setAttribute();?
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I am not getting answer to the exact question I am asking.
Rufus BugleWeed
Ranch Hand

Joined: Feb 22, 2002
Posts: 1551
Sure, I speak in riddles and I often ramble.
You can pass data between servlets in the ServletContext. With JSPs there is a scope limiting factor that would be very useful to you. Without the limiting feature I'm not sure how to avoid problems with concurrency between multiple instances of your servlet. To overcome the problem suppose you would have to assign a unique key to the attribute in the servlet before the call to the DAO.
<jsp:useBean id="name" scope="page|request|session|application" typeSpec />
The business bean ( that's the business delegate pattern - BD ) and the controller are both servlets. They can perform inter-servlet communication via the ServletContext. The the business delegate gets a bean from the DAO. Sort of a factory pattern, I suppose. The BD stores it in the context. The front controller then retrieves it from the ServletContext.
I don't understand why you want to couple your Editor to your controller though ...
[ November 27, 2003: Message edited by: Rufus BugleWeed ]
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
The EditorBean acts as a business delegate. Doesn't business delegate couple with controller servlet?
Rufus BugleWeed
Ranch Hand

Joined: Feb 22, 2002
Posts: 1551
I suppose I see the controller as a servlet that directs traffic to where it belongs. Whether the drivers of the cars are doctors, editors or lawyers makes no difference to the traffic cop. Now if different editors have different privileges then I would see editor delegating responsiblities to perhaps Subject.
I'm not sure that servlets have incorporated JAAS yet. But it's going there and you might be ahead of the game. ServletRequest has three methods to deal with that issue getUserPrincipal, isUserinRole, and getRemoteUser.
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
The user authentication part has been handled. I am asking what I am asking. The answers I got are drifting away.
[ November 28, 2003: Message edited by: JiaPei Jen ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I must be missing something... (your code in italics, mine in bold)

Why are you passing in the second parameter?
EditorBean editor = EditorBean.findEditor( username, editorData );

Why not just:
EditorBean editor = EditorBean.findEditor(username);


public static EditorBean findEditor( String username, EditorBean editorData )
throws EditorDAOSysException {

// What do I do here?
ud.find???
// I am not sure what to return
return editorData;
}


why return the same object you passed in? This becomes:

public static EditorBean findEditor( String username )
throws EditorDAOSysException {

return ud.findEditor(username);

}


And finally in the ud class, you have:
public EditorBean findEditor( String username, ??? )

This becomes:
public EditorBean findEditor( String username )

Is this too simple?
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I followed your suggestion, and got 5 compilation errors:

Apology by Mike C:
I edited this post, and there is a bug in UBB that eats anything that is in the UBB quote tags, when you edit a post. I didn't notice JiaPei had used quote tags before editing! The errors were:
2 errors related to not finding EditorBean class methods
1 error regarding EditorBean
2 errors related to not finding EditorBean class methods


The code for the EditorBean.java:

The code for MySQLEditorDAO.java

[ November 28, 2003: Message edited by: JiaPei Jen ]
[ November 28, 2003: Message edited by: Mike Curwen ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I think if you fix the 3rd error, the others may go away.

I won't outright tell you the solution, just think for a minute.

The error says: EditorBean.java:42: non-static variable ed canot be referenced from a static context
return ed.findEditor( username );


Go to line 42 of EditorBean.java.

I don't have the code in an editor, but I bet line 42 is:

The method is static... is the 'ed' variable?
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Okay, to fix the problem, I have to say:

Now, please help me with my core Java conceptual problem:
I once made findEditorData( String username ) non-static; i.e.

I got this compilation error for the calling class (LogonAction) of this Editor Bean:
LogonAction:125: non-static method findEditorData(java.lang.String) cannot be referenced from a static context"
EditorBean editor = EditorBean.findEditorData( username );
CORE JAVA QUESTION: Nothing has a statice modifier in the LogonAction. Why the method in the bean has to be static?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

LogonAction:125: non-static method findEditorData(java.lang.String) cannot be referenced from a static context"
EditorBean editor = EditorBean.findEditorData( username );

The 'static context' does not have to do with the LogonAction. The static context is the EditorBean class. I will bold the static reference...

EditorBean editor = EditorBean.findEditorData( username );

The problem is, you are trying to call the findEditorData method from a class reference, instead of an object reference.

If you had done something like:
Then you would not have gotten the compiler error, because now you're calling the non-static method from an instance of the class.

But because you don't want to have to instantiate an instance of EditorBean, in order to use its finder method, you should be making the findEditorData method static.
[ November 30, 2003: Message edited by: Mike Curwen ]
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks a lot, Mike, for the explanation.
 
Don't get me started about those stupid light bulbs.
 
subject: Struggling With Passing Data Between Data Access Class, Business Bean, and Controller