File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » JSP
Bookmark "why use beans in JSP" Watch "why use beans in JSP" New topic

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.

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

Joined: Jan 30, 2000
Posts: 3226
Your question doesn't mention using servlets also.
Sure JavaBeans are also classes. But you said:
"Like instantiating in in scriplets..."
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":

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.
I agree. Here's the link:
subject: why use beans in JSP
It's not a secret anymore!