Trainers' thoughts on Java and related technologies

One oft-touted benefit of JSP is that it’s much easier to write than straight Java code; however, most JSP programmers end up having to dig deeper into Java at some point. That said, if JSP applications are designed properly, it is possible to separate the hardcore Java programming from the presentational aspects of you application. I call this the MVC benefit of JSP.

There are several common ways to include Java source code in JavaServer Pages (JSP):

  • Put the code directly in each JSP as a declaration or scriptlet.
  • Use an include statement to reference another file with the code.
  • Put the code in a JavaBean.

So what’s the best way to include Java code in your JSP?

Including Java directly in a JSP is the most straightforward, but it limits code reuse and requires that even small changes to the document are made by a Java programmer. Using include statements lends itself to some code reuse, but it still requires a Java programmer. Both of these methodologies have another and more important problem: they normally wind up blurring the lines between the Model/View/Controller (MVC) design pattern tiers.

JavaBeans and JSP tags provide everything these methodologies do while separating business logic (Model) from the generation of the display (View). This always leads to better design and more maintainable applications.

The following is a small example of a JSP that uses scriptlets to display a message if the text field contains a name or no message if it’s blank:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <title>Hello with Scriptlets</title>
</head>
<body>
   <%
      String name = request.getParameter("name");
   %>
   <form method="get" name="form">
      <p>Enter your name please: <input type="text" name="name"/></p>
      <p><input type="submit" value="Run Me"/></p>
   </form>
   <%
      if(name == null)name = "";
      else name = "<h1>Hello, " + name + "!</h1>";
      out.println(name);
   %>
</body>
</html>

The JavaServer Page dynamically generates a display and handles user interaction, but reuse, maintenance, and MVC suffer. A JavaBean would solve those problems. First let’s look at the bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.webucator.beans;
public class YourName {
   private String name;
   public YourName() {}
   public String getName() {
      if(name == null) name = "";
      else name = "<h1>Hello, " + name + "!</h1>";
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

Now let’s look at the JSP that uses the JavaBean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <title>Hello with Bean</title>
</head>
<body>
   <jsp:useBean id="bean" class="com.webucator.beans.YourName"/>
   <jsp:setProperty name="bean" property="*"/>
   <form method="get" name="form">
      <p>Enter your name please: <input type="text" name="name"/></p>
      <p><input type="submit" value="Run Me"/></p>
   </form>
   <jsp:getProperty name="bean" property="name"/>
</body>
</html>

Changes to the JavaBean will be reflected in all the JSPs that use it, the page can be maintained easily, and MVC is preserved. A final point is that a project can be split between a Java development team and HTML experts. These benefits can be leveraged further by using JSP tag libraries. We’ll take a look at that topic in the next article.

We cover these concepts in our Introduction to JavaServer Pages course. The next class is scheduled for May 24, 2010.

No TweetBacks yet. (Be the first to Tweet this post)

3 Responses to “Reuse, Maintenance, and MVC Benefits of JSP and JavaBeans”

  1. in <jsp:usebean id="bean"…
    and <jsp:setProperty name="bean"
    <jsp:getProperty name="bean".. does these three names are same different…and what are they actually..means

  2. Great question. The tag is central in creating an instance of a class and providing a name by which that instance can be references.

    The id attribute provides the instance name.

    The class attribute identifies the class to be instantiated.

    The tag uses the name attribute to reference the class instantiated by the tag. The property attribute references the variable to access.

    The same idea applies to the tag.

  3. It is explained in a good way but what about those applications that uses datacases

Tweetbacks

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

© Webucator, Inc. All rights reserved. | Toll Free: 877-932-8228 | UK: 0808-101-3484 | From outside the USA: 315-849-2724 | Fax: 315-849-2723