JSP and Beyond

a pragmatic primer on building web-based solutions with Java technologies

Next Steps

Hopefully at this point you should have a good handle on the various components that have been covered in this book. The benefits of Java for the web, the history of the tools that have been used to generate dynamic web content, the ins and outs of application servers, how JSPs are processed, an understanding of what development tools can help deliver solutions, interaction with databases, email and other basics of Java web application development should now seem familiar.

From here it is possible to become acquainted with and explore additional technologies that build on top of the foundation that has been laid. These additional technologies continue to make Java a mainstay in enterprise application development.

The following sections were designed to help bring you up to speed on these additional tools at a conceptual level. I hope that it acts as a launch pad for you to dive into and leverage these additional tools in your development efforts.

Jakarta Struts
Jakarta Struts is maintained by the Apache Foundation. The Struts concept is somewhat similar to the MVC example discussed in the previous chapter, but it leverages an XML configuration file which maps actions to JSPs and “action” classes. This makes it a much better choice for managing applications of any real complexity.

More information about Jakarta Struts can be found at:

Java Server Faces
As you now know, JSP has many tools that allow you to help separate developer and designer tasks during a web application project. Java Server Faces allows web applications to be developed using “components” that make accessing information between requests much easier, because the information is made accessible through objects that represent the components in a “backing bean”. It is very similar to the way that ASP.NET works. The example below highlights a scenario where Java code in a special class handles the press of a button component in a JSP represented by the snippet below…

<ui:button action=”#{MyJSFSample.btnPressMe_action}” binding=”#{MyJSFSample.btnPressMe}” id=”btnPressMe” text=”Press Me!”/>

…and based on that event, a label component’s value is changed. The code below shows a component called “lblMyLabel” that registers the change based on the way the button press was handled in a special Java class which is detailed in the next code sample.

<?xml version=”1.0″ encoding=”UTF-8″?>
<jsp:root version=”1.2″ xmlns:f=”http://java.sun.com/jsf/core” xmlns:h=”http://java.sun.com/jsf/html” xmlns:jsp=”http://java.sun.com/JSP/Page” xmlns:ui=”http://www.sun.com/web/ui”>
<jsp:directive.page contentType=”text/html;charset=UTF-8″ pageEncoding=”UTF-8″/>
<ui:page binding=”#{MyJSFSample.page1}” id=”page1″>
<ui:html binding=”#{MyJSFSample.html1}” id=”html1″>
<ui:head binding=”#{MyJSFSample.head1}” id=”head1″>
<ui:link binding=”#{MyJSFSample.link1}” id=”link1″ url=”/resources/stylesheet.css”/>
<ui:body binding=”#{MyJSFSample.body1}” id=”body1″>
<ui:form binding=”#{MyJSFSample.form1}” id=”form1″>Our JSF Example<br/>
<ui:label binding=”#{MyJSFSample.lblMyLabel}” id=”lblMyLabel”/>
<ui:button action=”#{MyJSFSample.btnPressMe_action}” binding=”#{MyJSFSample.btnPressMe}” id=”btnPressMe” text=”Press Me!”/>

In order to make the above JSP page work and the value for “lblMyLabel” change, the following class assists that page with its processing.

* MyJSFSample.java
package myjsftest;

import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.rave.web.ui.component.Body;
import com.sun.rave.web.ui.component.Button;
import com.sun.rave.web.ui.component.Form;
import com.sun.rave.web.ui.component.Head;
import com.sun.rave.web.ui.component.Html;
import com.sun.rave.web.ui.component.Label;
import com.sun.rave.web.ui.component.Link;
import com.sun.rave.web.ui.component.Page;
import javax.faces.FacesException;

* <p>Page bean that corresponds to a similarly named JSP page. This
* class contains component definitions (and initialization code) for
* all components that you have defined on this page, as well as
* lifecycle methods and event handlers where you may add behavior
* to respond to incoming events.</p>
public class MyJSFSample extends AbstractPageBean {

private int __placeholder;

private void _init() throws Exception {

private Page page1 = new Page();

public Page getPage1() {
return page1;

public void setPage1(Page p) {
this.page1 = p;

private Html html1 = new Html();

public Html getHtml1() {
return html1;

public void setHtml1(Html h) {
this.html1 = h;

private Head head1 = new Head();

public Head getHead1() {
return head1;

public void setHead1(Head h) {
this.head1 = h;

private Link link1 = new Link();

public Link getLink1() {
return link1;

public void setLink1(Link l) {
this.link1 = l;

private Body body1 = new Body();

public Body getBody1() {
return body1;

public void setBody1(Body b) {
this.body1 = b;

private Form form1 = new Form();

public Form getForm1() {
return form1;

public void setForm1(Form f) {
this.form1 = f;

private Label lblMyLabel = new Label();

public Label getLblMyLabel() {
return lblMyLabel;

public void setLblMyLabel(Label l) {
this.lblMyLabel = l;

private Button btnPressMe = new Button();

public Button getBtnPressMe() {
return btnPressMe;

public void setBtnPressMe(Button b) {
this.btnPressMe = b;

public MyJSFSample() {

public void init() {
try {
} catch (Exception e) {
log(”MyJSFSample Initialization Failure”, e);
throw e instanceof FacesException ? (FacesException) e: new FacesException(e);


public void preprocess() {
public void prerender() {

public void destroy() {
protected ApplicationBean1 getApplicationBean1() {
return (ApplicationBean1)getBean(”ApplicationBean1″);

protected SessionBean1 getSessionBean1() {
return (SessionBean1)getBean(”SessionBean1″);

protected RequestBean1 getRequestBean1() {
return (RequestBean1)getBean(”RequestBean1″);

public String btnPressMe_action() {
lblMyLabel.setText(”You pressed the button. Amazing!”);
return null;

The line highlighted above manages the lblMyLabel label component and updates its value.

We are only scratching the surface of JSF here, but the above example illustrates some very basic elements of its operation. For more details on JSF please visit:

Remember all of the lines of code that were needed to write in the Dealing with Data – JDBC section? Hibernate creates an object layer over your database that makes all of the connection creation, querying, adding, updating, deleting and more a breeze because it removes the need to write any SQL code. This abstraction methodology is called Object Relational Mapping. Using Hibernate, you are able to use plain old Java objects (POJOs) to manipulate any information in the database as Hibernate will automatically create the POJOs that will map to your database structure.

The example below shows a theoretical situation where we are trying to store a new user.

With Hibernate

User user = new User();
Transaction saveUser = session.beginTransaction();

Without Hibernate

// Obtain a statement object
Statement stmt = cnDB.createStatement();

// Execute the block of SQL code
stmt.executeUpdate(”INSERT INTO tblUser VALUES (’Doug’)”);

// Close the result set

// Close the connection

After reviewing the above examples, it is obvious that the Hibernate approach is much more elegant, especially when you start to work with more complex transactions and data structures.