Java Server Pages And Java Beans

(This is all covered in much more depth in an article that I will have published in the August '99 JavaReport)

The model that we work with when using JavaServlets, JavaServerPages and JavaBeans together is that you begin by calling a JavaServlet from a URL. This servlet looks up some information (say from a database) and creates a JavaBean.

Your goal is to display the information in this bean in the JavaServerPage. You have two choices as to where you can put this information. The first "bucket" is called the HTTPSession, which has a lifetime that crosses multiple HTTP request/response pairs -- information placed here can be accessed later by other servlets and JSP's kicked off from other URL's, as long as the client remains the same (e.g. the key to determine which session to use is stored in a client-side cookie).

The second "bucket" is the HTTPRequest context. This lasts only for the current HTTP request/response pair. Information placed here is thrown away at the end of the response.

JavaServerPages support a special HTML tag called the <BEAN> (or <USEBEAN> in JSP 1.0) tag that allow them to access beans placed in these "buckets". So (in JSP 0.91, supported by IBM WebSphere) you could have a page that did the following:

<bean name="myBean" type="com.ibm.examples.MyBeanClass?" introspect="no" create="no" scope="session"></bean>

This means "take the bean of type MyBeanClass? out of the Session by using the key "myBean". Place it in a variable called "myBean". (I'm not going to talk about the rest of it -- it's too complicated for this short intro).

Later you could have a tag that said:

<%= myBean.calculateThis() %>

Which means "print out the result of sending the calculateThis() method to myBean into this page". There is another tag (called <INSERT> in JSP 0.91, <DISPLAY> in JSP 1.0) that allows you to print bean properties directly without having to write scriptlets (the little Java bit I've shown above).

KyleBrown


I'm having trouble understanding the need for special tags, like <BEAN>, and in another example I've seen <LOOP>. When do you need a special tag and when can you just write some script, ie. <% ... %>?


The goal is to keep the roles of content provider (e.g. programmer) and web page designer separate. By using special tags we can build tools (like the new version of WebSphereStudio? that allow point-and-click development of JSP pages that include Java beans pulled from a palette. No programming knowledge necessary.

KyleBrown

See AlarmBellPhrase. See ScriptletsCanBeBetterThanTags.


Oh please. There's a difference between someone embedding a truly static thing like a Java bean acting as a dumb data holder in an HTML page and trying to do "visual programming" like VisualAge. Not every problem requires programming, and this one doesn't. Now, on the other hand I consider the <LOOP> tag an abomination -- that SHOULD require code.

KyleBrown

I'm not saying that the phrase is always bogus, but I saw it and it did make me think there should be a page about phrases that set off alarm bells. Plus, it did seem at first glance that you were referring to <LOOP> when you said it. --GlennVanderburg


CategoryJavaPlatform

EditText of this page (last edited February 18, 2006) or FindPage with title or text search