• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

JSP Three Tier

Hi,

I want to create a simple jsp page which shows data from a table on the page. There will be buttons on the form to insert/update and delete records from the table.

I need to break the logic up into a 3 tier model, effectively I want the user interface seperate from the business logic, seperate from the database logic.

I need some ideas on how to accomplish this.

Can I use a normal bean to hold the business logic? This business logic bean will then be called directly from the form if the user presses say the add record button. From the business logic bean, the database bean will be called which will run the stored proc / queries etc, and return the resultsets in an arraylist back to the logicbean, which will in turn return the arraylist back for display to the jsp page?

Any ideas please.

Regards
0
barnarp
Asked:
barnarp
  • 4
  • 2
1 Solution
 
bloodredsunCommented:
>>I need to break the logic up into a 3 tier model, effectively I want the user interface seperate from the business logic, seperate from the database logic.

This means that you need to break up the webapp into a presentation layer, a business layer and a database layer. Without the complication of struts (which is a 3 layer MVC framework) you can easily do this.

Basic flow

HTML/JSP form (presentation layer)
-->Servlet (business layer) which looks at the form parameters submitted and decides which business delegate/helper class to call to access the database.
-->-->Data Access class(database layer) connects to database (or better yet gets a connection from a connection pool, q.v. DataSource) and returns the results as a javabean (Data Transfer Object) to the servlet
-->Servlet puts javabean into request scope and uses RequestDispatcher to forward the request onto the presentation layer
JSP form (presentation layer) gets the javabean from the request scope and outputs the results.


Your Q's:
>>Can I use a normal bean to hold the business logic?
Yes

>>This business logic bean will then be called directly from the form if the user presses say the add record button
No. Put this in a controller servlet.

>>From the business logic bean, the database bean will be called which will run the stored proc / queries etc, and return the resultsets in an arraylist back to the logicbean, which will in turn return the arraylist back for display to the jsp page?
More or less yes. I prefer to wrap the results as a Javabean rather than put an object directly into the request or session as it's better hygenie.
0
 
barnarpAuthor Commented:
Thanks,

Can I use a normal jsp for the first page and then pass the result to a "business" bean and then unto a "db" bean? or am I not getting the picture here?

0
 
bloodredsunCommented:
>>Can I use a normal jsp for the first page and then pass the result to a "business" bean and then unto a "db" bean? or am I not getting the picture here?

Absolutely. You could have an HTML page if you wanted. The index page is the start of the "user journey", so at the time it is called, nothing has happened so there is no state/information to be held. Only when the user starts a process such as making a booking do you need to go into MVC properly.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
barnarpAuthor Commented:
Another question

I have an imgae on the first JSP page which if clicked, should update/delete a record.

Can I then from the first JSP page click on an image (which should act like a button on a form submit), then the image will call the onclick method and in javascript it will call a business bean, which will call a db bean?

Is it possible? Or is there a better method?
0
 
bloodredsunCommented:
>>Is it possible? Or is there a better method?

Almost but not quite. You can click an image which calls a javascript which submits the page to another page ot servlet which then calls the business bean etc.....

Remember that javascript is client side and JSP is server side, so you can make Javascript with JSP but you can't make JavaScript to make JSP unless you submit the page back to the server.
0
 
bloodredsunCommented:
Cheers :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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