• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you familiar with javax.servlet.ServletContext.setAttribute();?
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting answer to the exact question I am asking.
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The EditorBean acts as a business delegate. Doesn't business delegate couple with controller servlet?
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Mike, for the explanation.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic