What Is Jakarta Struts? - dummies

By Mike Robinson, Ellen Finkelstein

Jakarta Struts is incredibly useful in helping you create excellent Web applications. When you use Jakarta Struts, your applications should work more effectively and have fewer bugs. Just as important (because your time is important), Struts should save you hours and hours of programming and debugging.

Struts is a framework that structures all the components of a Java-based Web application into a unified whole. These components of a Web application are

  • Java Servlets: Programs written in Java that reside on a Web server and respond to user requests
  • JavaServer Pages: A technology for generating Web pages with both static and dynamic content
  • JavaBeans: Components that follow specific rules, such as naming conventions
  • Business logic: The code that implements the functionality or rules of your specific application

Jakarta Struts uses a specific paradigm, or design pattern, to structure your application. You simply fill in the pieces of the structure. The design pattern is called Model-View-Controller (MVC). The MVC design pattern helps you organize the various pieces of the application puzzle for maximum efficiency and flexibility.

A Web application as a program that resides on a Web server and produces static and dynamically created pages in a markup language (most commonly HTML) in response to a user’s request. The user makes the request in a browser, usually by clicking a link on the Web page.

To build Web applications, you use Java 2 Enterprise Edition (J2EE), which provides support for Servlets, JSP, and Enterprise JavaBeans (EJB), a distributed, multi-tier, scalable component technology.

A Web container is a program thatmanages the components of a Web application, in particular JSP pages and Java Servlets. A Web container provides a number of services, such as

  • Security: Restricted access to components, such as password protection
  • Concurrency: The capability to process more than one action at a time
  • Life-cycle management: The process of starting up and shutting down a component

Apache Tomcat is an example of a Web container — an open-source implementation of the J2EE Java Servlet and JavaServer Pages (JSP) specifications. A specification is a document that describes all the details of a technology. The implementation is the actual program that functions according to its specification. In fact, Apache Tomcat is the official reference implementation for the J2EE Java Servlet and JSP specifications. As a result, Apache Tomcat is a popular Web container for Web applications that use JSP and Servlets, including applications that use Struts.

Typically, a Web container also functions as a Web server, providing basic HTTP (Hypertext Transfer Protocol) support for users who want to access information on the site. When requests are for static content, the Web server handles the request directly, without involving Servlets or JSP pages.

However, you may want your Web pages to adapt in response to a user’s request, in which the response is dynamic. To generate dynamic responses, the Servlet and JSP portion of the container gets involved. Tomcat has the capability to act as both a Web server and a Web container. However, it also can interact with a standard Web server, such as Apache Web Server, letting it handle all static requests and getting involved only when requests require Servlet and JSP service.

Using Java Servlets

Java Servlets extend the functionality of a Web server and handle requests for something other than a static Web page. They are Java’s answer to CGI (Common Gateway Interface) scripts of olden times (5 to 6 years ago). As their name implies, you write Java Servlets in Java and usually extend the HttpServlet class, which is the base class from which you create all Servlets. As such, Java Servlets have at their disposal the full functionality of the Java language, which give them a lot of power.

Servlets need to run in a Web container, an application that adheres to the Java Servlet Specification. In most cases, the container will support also the JavaServer Pages Specification. You can find a list of products supporting the Java Servlet and JSP specifications at this website. The latest Java Servlet Specification is 2.3, and the latest JavaServer Pages Specification is 1.2.

Creating JavaServer Pages

You use JavaServer Pages to present dynamic information to the user in a Web page. A JSP page has a structure like any static HTML page, but it also includes various JSP tags, or embedded Java scriptlets (short Java code fragments), or both. These special tags and scriptlets are executed on the server side to create the dynamic part of the presentation, so that the page can modify its output to reflect the user’s request.

What really happens behind the scenes is that the JSP container translates the JSP page into a Java Servlet and then compiles the Servlet source code into runnable byte code. This translation process happens only the first time a user accesses the JSP page. The resulting Servlet is then responsible for generating the Web page to send back to the user.

Each time the JSP page is changed, the Web container translates the JSP page into a Servlet.

Listing 1 shows an example of a JSP page, with the JSP-specific tags in bold.

Listing 1 Sample JSP Page

1 <%@ page contentType=”text/html;charset=UTF-8″language=”java” %>
2 <%– JSTL tag libs –%>
3 <%@ taglib prefix=”fmt” uri=”/WEB-INF/fmt.tld” %>
4 <%– Struts provided Taglibs –%>
5 <%@ taglib uri=”/WEB-INF/struts-html-el.tld” prefix=”html” %>
6 <html:html locale=”true”/>
7 <head>
8 <fmt:setBundle basename=”ApplicationResources” />
9 <title><fmt:message key=”loggedin.title”/></title>
10 </head>
11 <body>
12 <jsp:useBean id=”polBean” class=”com.othenos.purchasing.struts.POListBean”/>
13 <H2>
14 <fmt:message key=”loggedin.msg”>
15 <fmt:param value=’${polBean.userName}’ />
16 </fmt:message>
17 </H2>
18 </body>
19 </html>

JSP defines six types of tag elements:

  • Action: Follows the XML (eXtended Markup Language) format and always begins with <jsp:some action/>. It provides a way to add more functionality to JSP, such as finding or instantiating (creating) a JavaBean for use later. You see one example of an action tag in line 12 of the code in Listing 1.
  • Directive: A message to the Web container describing page properties, specifying tag libraries, or substituting text or code at translation time. The form is <%@ the directive %>. Listing 1 has directives on lines 1, 3, and 5.
  • Declaration: Declares one or more Java variables or methods that you can use later in your page. The tag has this form <%! declaration %>.
  • Expression: Defines a Java expression that is evaluated to a String. Its form is <%= expression %>.
  • Scriptlet: Inserts Java code into the page to perform some function not available with the other tag elements. Its form is <% java code %>.
  • Comment: A brief explanation of a line or lines of code by the developer. Comments have the form <%– the comment –%>. Lines 2 and 4 in Listing 1 are examples of comments.

Because a JSP file is just a text file, you can create it in just about any kind of text editor. Note that some editors understand JSP syntax and can provide nice features such as formatting and color coding. A few of the bigger ones are Macromedia Dreamweaver, NetBeans, and Eclipse; the last two are complete Java development environments.

Like Java Servlets, JSP pages must be run in a Web container that provides support for JSP technology.

Using JavaBeans

When you program in Java, you define or use classes that function as a template for objects that you create. A JavaBean is a special form of Java class that follows certain rules, including the methods it uses and its naming conventions.

Beans are so useful because they are portable, reusable, and platform independent. Beans are components because they function as small, independent programs. JavaBeans component architecture defines how Beans are constructed and how they interact with the program in which they are used.

You can call a JavaBean a Bean and everyone will know what you’re talking about, as long as you’re not discussing coffee.

The JavaBean documentation refers to the rules as design patterns. However, this term is more generally used to refer to design patterns such as the Model-View-Controller design pattern. Naming conventions is a more appropriate term.

As an example of the special Bean rules, look at properties. A Bean’s properties that are exposed (public) are available only through the getter and setter methods, because the actual property definition is typically private (available to only the defining class). The properties follow the naming convention that the first letter of the property must be lowercase and any subsequent word in the name should start with a capital letter, such as mailingAddress. Listing 2 is an example of a simple Bean.

Listing 2 Example of a Simple JavaBean

public class SimpleBean implements java.io.Serializable
private String name;
// public no-parameter constructor
public SimpleBean()
// getter method for name property
public String getName()
return name;
// setter method for name property
public void setName(String aName)
name = aName;

In this example, String is the type of property and name is the property.

Methods that access or set a property are public (available to anyone using the Bean) and also use a certain naming convention. You name these methods as follows:

  • To get a property’s value, the method must begin with get followed by the property name with the first letter capitalized, as in public String getName();.These methods are called getters.
  • To set a property’s value, the method must begin with set followed by the property name with the first letter capitalized and the value to set the property to, as in public void setName(String theName);. These methods are called setters.