• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why use beans in JSP

 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,
i have a query, why to use beans in my jsp page as beans are also java classes and i can use it otherwise also. Like instantiating in in scriplets. are there any specific advantages of using <jsp:useBean> tag.
thanks
 
Bhushan Jawle
Ranch Hand
Posts: 249
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaBeans follow a specific naming convention which allows jsp tags to access and retrieve values easily. You can achieve the same by scriptlets, however using scritplets extensively in JSP page is not recommended as that can lead to unmaintainable "Spaghetti Code".So the question is not "Why JavaBeans?" but why not scriptlets.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question doesn't mention using servlets also.
Sure JavaBeans are also classes. But you said:
"Like instantiating in in scriplets..."
WAIT!
Unless your app is simple, by now the recommended pattern for Java Web apps is the MVC (AKA "Model 2").
Look it up in the literature.
It uses a Java Servlet(s) as a "traffic cop" (controller) directing the "action" (request) to where it needs to be serviced and usually creates a bean or two which it then places in request scope (unless session scope is *REALLY* needed).
The JSP uses the :usebean tag and you get the picture, right?
Also by now, the framework STRUTS has pretty much simplified the problem of implementing Java Web apps.
"Read All ABout It":
http://www.amazon.com/exec/obidos/ASIN/0596003285/electricporkchop
and
http://www.amazon.com/exec/obidos/ASIN/1930110502/electricporkchop
 
Chris Stewart
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts is nice and all but I feel like there is major overhead using it. If the application is large and JSPs/Servlets begin to get too "messy", then I'll consider Struts. But for most smaller apps, simple JSPs/Servlets is fine.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic