aspose file tools*
The moose likes JSP and the fly likes Beans, JSP, Servlets how? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Beans, JSP, Servlets how?" Watch "Beans, JSP, Servlets how?" New topic
Author

Beans, JSP, Servlets how?

Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Hi!

I have a problem to solv. I'm developing 4 diffrent websites. They will all have diffrent looks..... meny, colors etc diffrent IP but will get data from same database. All data will be shared. Products , price will be the same on all 4 websites but all products must not be available on all 4 websites.

The websites will be adminstrated via an desktop application.

I will use JSP,Servlets,beans.

I have some problem in how to do this. I want the Beans to access the mysql database and the data can only be retreived thru beans. No direct access from servlets and jsp. How can Beans be used to get data thru JDBC with connection pooling. I can get it to work with pooling with servlets but what if only the beans should access the data?

Beans -> get, set all data to database
Servlet -> serialize objects to desktop application and save data to database thru beans.
JSP only view.

// Mathias


SCJP1.4
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

"beans" aren't responsible for getting and setting data in the database. That is the job of a Data Access Object or DAO. The DAO will typically populate your beans (Model) which get forwarded for use in your JSP (View) by your servlet (Controller).


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

The fact that you are talking about "beans" to do the processing leads me to believe that you are planning a Model 1 architecture, which I would advise against. If by "beans" you mean POJOs in a Model 2 architecture, that's the way that I would go. Especially for something that's going to see the type of reuse that you are talking about right off the bat.

I'd start off by creating your business layer in such a way that you can test it outside of the servlet environment using a command line program. That way you know you have a good separation of the Model from the Controller, and have a great way to test the model classes.

With regards to the connection, I'd use JNDI connecton pooling. That way all you have to pass around is the JNDI string.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
I'm not quite sure I'm following everything you say here.

How can I use Plain Old Java Objects and let them interact with servlets and get the desktop application to manipulate the objects?

If I create a class Lib to get and set data in my mysql database then can I use this lib in the tomcat container later with connection pooling?

Should I start with a plain application and then add the library to the webapplication?

I haven't done any JSP,Servlet, Beans programming just small program to test. So I have problem in seeing the whole picture even though I have read a book or two. It's always. A JSP or Servlet example but not all together.
[ March 26, 2007: Message edited by: Mathias Nilsson ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Have you read this article?
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Thanks! I'm printing it now to get my eyes of the screen for a moment.

I thought I knew how to implement a webapplication. I have programmed asp, php, a little .net but there is too many EJB, JavaBeans, Servlets , POJO, JSP, JSTL, Tomcat, JBOSS for me right now.

The pressure is on.
[ March 26, 2007: Message edited by: Mathias Nilsson ]
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Ok I have read your article about the model 2. A good article but.... perhaps my english is poor because when I look at the diagrams and read the article the Beans have the responsebility to get data from the database. Doesn't that mean that the bean connects to the database and update, delete, insert data?

Please help me to get this straight. When I'm coding right know I let the servlet connect to database via JNDI and all sql calls are made directly in the servlet. I do not want this.

If I have understood it right from here


JSP. Here I use custom taglibraries, JSTL and beans just to view the data. When say an item is purchased, deleted the JSP post to servlet. Servlet handles the request and forwards to JSP. I need to make the request to the servlet so that It can be made many times without crashing data.

I need to get hold of the objects via serialization from the desktop application. The webapplications will not be on the same server as the desktop applications. Do I use request to servlets and response to desktop here or what is the "normal" approach?

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Originally posted by Mathias Nilsson:
and read the article the Beans have the responsebility to get data from the database. Doesn't that mean that the bean ...


Again it depends what you mean by "bean". To many people it means beans instatiated from the page via <jsp:useBean>. In Model 2, that's not the case. Again, think in terms of an API for your business logic (which encapsulated any DB access) that can be used from either a GUI or a command line program.

Please help me to get this straight. When I'm coding right know I let the servlet connect to database via JNDI and all sql calls are made directly in the servlet. I do not want this.


You're a wise man. That way lies anguish and pain.

JSP. Here I use custom taglibraries, JSTL and beans just to view the data.


Bingo! The beans used should be DTO's that carry data from the controller to the JSP. They should be simple and perform little to nothing in the way of processing.

When say an item is purchased, deleted the JSP post to servlet.


Right. You should rarely, if ever, post directly to a JSP.

Do I use request to servlets and response to desktop here or what is the "normal" approach?


The "normal" approach is for the web app and the desktop to have nothing to do with each other. The database is the sole point of confluence.
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Thanks for all the responses.

Where do the beans fit in. I do not mean beans as you put them in the model 2 but the <jsp: usebean> way. If I look at the model 2 are they in there somewere?

The "normal" approach is for the web app and the desktop to have nothing to do with each other. The database is the sole point of confluence.


The mysql is closed for direct access because of the attach agains port 3306 and other attacks. Every article I've read about letting the mysql be opened for the outside worlds is wised againsed. So I have to use some layer between to get and set all the data to the database from web and desktop. As I see it right know without having to open ports etc the port 80 with http request and responses seems right.

Post from desktop to servlet and response from servlet to desktop.
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Is there any short example out there using the model 2 architecture with POJO?

// Mathias
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beans, JSP, Servlets how?