*
The moose likes JSP and the fly likes why use beans in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "why use beans in JSP" Watch "why use beans in JSP" New topic
Author

why use beans in JSP

Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
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


Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
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

Joined: Jan 30, 2000
Posts: 3222
    
    5
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


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Chris Stewart
Ranch Hand

Joined: Sep 14, 2002
Posts: 184
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.
 
Consider Paul's rocket mass heater.
 
subject: why use beans in JSP