wood burning stoves 2.0*
The moose likes Servlets and the fly likes When to use javabeans or servlet 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 » Servlets
Bookmark "When to use javabeans or servlet" Watch "When to use javabeans or servlet" New topic
Author

When to use javabeans or servlet

Alan Yap
Ranch Hand

Joined: Feb 24, 2006
Posts: 32
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 ]

When I'm not coding.. I do THIS
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
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.


My Blog
Alan Yap
Ranch Hand

Joined: Feb 24, 2006
Posts: 32
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.

Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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)


SCJP, SCWCD.
|Asking Good Questions|
Ashimashi Kabashi
Ranch Hand

Joined: Sep 11, 2006
Posts: 84
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

Joined: Feb 24, 2006
Posts: 32
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 ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
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.
 
 
subject: When to use javabeans or servlet