• 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

When to use javabeans or servlet

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before I posted this topic, I've googled and javaranch-seached regarding the said topic, and found some insights on MVC model that explains beans are part of the "model",while servlets acts as the controllers. But I have been presented with limited sample codings.

Anyway, I've started to overhaul all the class files that I've coded for use in my web apps. I'm almost sure some of them might be implemented inappropriately (though they still work).

Consider this example:

test.jsp



formformatter.java




foodmanager.java



As you can see, formformatter.java contains only utility methods that format the input parameter into something displayable by the jsp.

foodmanager.java contains methods that takes the input parameter,and does database update/access.

Both of them does not make use of methods like doGet() or doPut(). So it is wise of me to have them to extends HttpServlet? What are the suggested changes and best practice I should follow ?

thanks.

[ October 13, 2008: Message edited by: Alan Yap ]
[ October 13, 2008: Message edited by: Alan Yap ]
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Utility classes should not extend HttpServlet, they should be simple class only focusing on business logic. Java beans are usually simple POJO having variables and their setter/getter without any business logic. Also instantiating servlets like-

Is not a good practice. Instances of Servlets are not supposed to be created by developer, they are supposed to be managed by container.
 
Alan Yap
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vinod K Singh:
Utility classes should not extend HttpServlet, they should be simple class only focusing on business logic. Java beans are usually simple POJO having variables and their setter/getter without any business logic.



So both formformatter.java and foodmanager.java should be simple POJOs ?

Originally posted by Vinod K Singh:

Also instantiating servlets like-

Is not a good practice. Instances of Servlets are not supposed to be created by developer, they are supposed to be managed by container.



I'm thinking of converting all of them to something like below. Do elaborate (with examples if can) if I've misunderstood,thanks.

 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you should be using JSp standard actions instead of scriptlet as in your original post.
Also a Google with Java beans should clear your doubts about beans (not enterprise Java beans or EJBs)
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are using Servlets for your Beans. it allows the "Tier Leakage" anti pattern to manifest itself into your application.
 
Alan Yap
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ashimashi Kabashi:
When you are using Servlets for your Beans. it allows the "Tier Leakage" anti pattern to manifest itself into your application.



I think it already did

I got the whole concepts of servlets mixed up. I thought servlets are whatever java classes compiled to extend httpServlet dumped into /WEB-INF/classes folder,besides the session bean. And JSP pages are free to instantiate any of them at will for any kind of processing, whether it involves simply calculations, acqusition of data from database, and rendering them. I was dead wrong. :roll:


Anyway, if conventional POJO does the trick, I might as well apply it.

Referring to the link below, I found out servlets is not compulsory in a web app. Many things can be achieved by JSP + beans alone (see the Model 1 architecture diagram inside) , at the moment.

http://java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp/
[ October 14, 2008: Message edited by: Alan Yap ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These days my number one rule on "helper" classes - JavaBean or otherwise - is that they should be testable outside the servlet environment.

This means passing data in with collections and getting output in Strings or output streams rather than using servlet specific object references. The servlet/jsp environment has too many things that can go wrong and obscure the functioning of helper classes.

Bill
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alan Yap:

Referring to the link below, I found out servlets is not compulsory in a web app. Many things can be achieved by JSP + beans alone (see the Model 1 architecture diagram inside) , at the moment.

http://java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp/

[ October 14, 2008: Message edited by: Alan Yap ]




Better than Model 1 is the MVC pattern (sometimes called Model 2. Take a look at Bear's FrontMan article. Having your JSP do the work of the controller (as Model 1 dictates) ends up making your JSPs a huge mess.

Also, you have the formformatter class generating HTML strings from a list of values. Generating HTML is what JSPs are good at. You should definitely not be using a helper class to make the HTML output. What you should do is use the JSP to iterate over the list and produce the HTML output as needed. This keeps all your display in one place - the JSP - which is why the JSP is considered the View part of MVC.

You should also look into JSTL (google it, there are some JARs to download and a ton of tutorials on it) and EL. These tools, along with the jsp standard actions, a good Model structured with JavaBeans, and a good Servlet used for the control will help you keep those hard-to-manage scriptlets out of your code.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Referring to the link below, I found out servlets is not compulsory in a web app. Many things can be achieved by JSP + beans alone (see the Model 1 architecture diagram inside) , at the moment.



Alan, take note that Java Server Pages (JSP) are Java servlets.

A JSP Engine is a fundamental component of a Java web server. The JSP engine compiles the JSP file and generates source code for a servlet. It then compiles this source code and executes the servlet in web server's Servlet container.
 
reply
    Bookmark Topic Watch Topic
  • New Topic