• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use javabeans or servlet

 
Alan Yap
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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 ]
 
Vinod K Singh
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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)
 
Ashimashi Kabashi
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • 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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic