File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Could i use an EJB as a controller just like we use servlet in MVC???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Could i use an EJB as a controller just like we use servlet in MVC????" Watch "Could i use an EJB as a controller just like we use servlet in MVC????" New topic
Author

Could i use an EJB as a controller just like we use servlet in MVC????

sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
HI!
I have following senario

Swing client (send/receive java object)-----Internet/JNDI----Controller at server(send/ receive java object)

Now since i have to send and receive java objects on client and server end so should i use a stateless session bean to act as a controller and allow my swing client to access that ejb controller's remote interface(through JNDI) and send receive java object through it or is there anyother good approach to do that...

NOTE:-
I have been trying to do that senario using servlet on server end...You can found discussion abut taht topic on following link
http://forum.java.sun.com/thread.jspa?threadID=627481&tstart=0
but i have found that i cann't send/receive jaav object at client/servlet end .....
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I believe that you have been misinformed that you cannot send serialized java objects to a servlet. You should certainly be able to write a String object from your client which the servlet can obtain (by using HttpServletRequest.getParameter()).


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
HI!
It's not only that i need to send serialized java object to servlet but i also have to get a serialized object from servlet in response could i do that as well...
if so then please send me any good coding example of doing that ...it would hellp me a lot .......
thanks in advance
sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
b/w....
it would not be always the case when i have to send a String object toa servlet ....sometimes i even want to send java.lang.Object to servlet then what will i do....
although i need an example to understand your point here but could we be able to send a serialized java object (other then String) from servlet back to Swing client......
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Sajjad,


I have following senario

Swing client (send/receive java object)-----Internet/JNDI----Controller at server(send/ receive java object)

Accessing your beans directly from your swing clients over the internet, is not very common and it might not work. Unlike usual web clients that use the HTTP protocol for accessing web components (jsp/servlets that talk to ejbs), your swing clients will use the RMI/IIOP protocol. The well-known problem with IIOP is that it doesn’t work easily with firewalls, or at least it’s not easy configurable. I know there are firewalls that allow pure IIOP calls to go through, but you should check twice in order to go with this architecture. Even worse most of the companies won’t even allow iiop calls to go through (good security policies). Your scenario is more feasible for a LAN where clients are not behind the firewall.

Now since i have to send and receive java objects on client and server end so should i use a stateless session bean to act as a controller and allow my swing client to access that ejb controller's remote interface(through JNDI) and send receive java object through it or is there anyother good approach to do that...

You can do that, but it might result in a very high number of unnecessary rmi calls. You might consider implementing the controller as a POJO. This basically means that every client will have a local copy of the controller, which will dispatch the calls to the remote server. You’ll achieve better performances while you’ll still benefit from all advantages that the MVC paradigm provides.

I have been trying to do that senario using servlet on server end...You can found discussion abut taht topic on following link
http://forum.java.sun.com/thread.jspa?threadID=627481&tstart=0
but i have found that i cann't send/receive jaav object at client/servlet end .....

With servlets you have to use the stateless HTTP protocol: you always send httpRequests and get back httpResponses. If your clients are swing based, then you need to get the stream from the server, parse it and map it to some OO model. The same when sending messages: get the user input, translate it to an http stream, etc. This without adding the extra complications if some server side default objects (like httpSession or app context) must to be accessed. You’ll mostly get into a procedural approach and your application will be hard to maintain and enhance.
Regards.


I think, therefore I exist -- Rene Descartes
sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
HI Valentin Tanase!

You can do that, but it might result in a very high number of unnecessary rmi calls.


Hmm but i don't there could be a lot of those rmi calls when i am simply accessing a my controller EJB and calling it's only remote execute method...what do you say


You might consider implementing the controller as a POJO. This basically means that every client will have a local copy of the controller, which will dispatch the calls to the remote server. You�ll achieve better performances while you�ll still benefit from all advantages that the MVC paradigm provides.


very good that seems to be a very good approach but unfortunatly i havent done that thing before so if you can provide me any example code or atleast a good source to learn then it would help me a lot....


With servlets you have to use the stateless HTTP protocol: you always send httpRequests and get back httpResponses. If your clients are swing based, then you need to get the stream from the server, parse it and map it to some OO model. The same when sending messages: get the user input, translate it to an http stream, etc. This without adding the extra complications if some server side default objects (like httpSession or app context) must to be accessed. You�ll mostly get into a procedural approach and your application will be hard to maintain and enhance.


yeah i am already trying to do that... i can write an object to the ObjectOutPutstream in servlet but once i get that stream in client how i can get the same object from that stream ... i have no idea about that can you please tell me what should i do at client end to get the same object from server stream(which i did set in my servlet at server end)

thank for replying in that much detail and i am hoping same kind of good response from you this time
reagard
sajjad ahmed paracha
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I don't understand any of this complexity, unless you are developing something for learning purposes.

All you need to do is have the Swing client prepare a data transfer object (DTO), get the stub to a stateless session bean's EJBObject and pass the DTO in as the arg to a method call (using RMI-IIOP). The bean can act as the controller and delegate the work to another bean or POJO. The bean can send back another DTO for the client to process.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
As far as MVC I wouldn't call the stateless session bean a controller. the controller's job is to "interpret user gestures" into some action on the model. I'd continue to think of a controller as part of my Swing app. You might have a model as part of your Swing app or not, I suppose.

The stateless session bean might be a Facade, tho. That would give the client a simple, unified interface rather than revealing the structure of the classes inside the server.

If your client and server teams are not the same people, it would be nice of the server folks to provide Business Delegates to the client, too. Again that gives a simple POJO interface rather than forcing the client team to learn about remote EJBs. Whether they are the same people or not, the BD also makes it much simpler to change protocols from remote EJB to web services (or whatever comes along next) if you so choose.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704

Hmm but i don't there could be a lot of those rmi calls when i am simply accessing a my controller EJB and calling it's only remote execute method...what do you say

In a nutshell your controller translates interactions with the view into actions to be performed by the model. What that means is that for each button, or menu item that the view displays, the system must be able to build kind of command object and send it to the controller. Now imagine that you have a simple help menu item that displays a local resource file. Following a truly MVC approach you have to send the corresponding action object to the controller, which will return the corresponding view (the help file in this case). Why should you perform a server round trip for that?

very good that seems to be a very good approach but unfortunatly i havent done that thing before so if you can provide me any example code or atleast a good source to learn then it would help me a lot....

Let me draft you the design I used when I worked on my SCJD certification. The idea comes from Struts, I only simplified it and adjusted it to my swing needs. First I designed the Action interface. It has only one method:

The controller is not very complicated either and has also only one important method:

What the controller does it�s pretty straightforward. It gets the command name from the client as a string (like search, find, update, etc; it�s a very simplified model and no command object was really necessary), and delegates the job to the right action. Actions in turn know how to talk to the model (a database file in this case) get the data, fill up the right JPanel view and return it to the client. Here there is how an action class looks like:

Finally the client calls look like this:

Please notice that every client request is forwarded to the controller.

yeah i am already trying to do that... i can write an object to the ObjectOutPutstream in servlet but once i get that stream in client how i can get the same object from that stream ... i have no idea about that can you please tell me what should i do at client end to get the same object from server stream(which i did set in my servlet at server end)

You need to pass the stream and write some utility classes to construct your object from that stream.

thank for replying in that much detail and i am hoping same kind of good response from you this time

I hope I provided you the kind of details you�ve expected
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could i use an EJB as a controller just like we use servlet in MVC????