Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

An Introduction to IBM Rational Application Developer: A Guided Tour
An Introduction to IBM Rational Application Developer: A Guided Tour
An Introduction to IBM Rational Application Developer: A Guided Tour
Ebook837 pages6 hours

An Introduction to IBM Rational Application Developer: A Guided Tour

Rating: 5 out of 5 stars

5/5

()

Read preview

About this ebook

IBM Rational Application Developer is a very important tool for developers, but it is also a complex product. An Introduction to IBM Rational Application Developer, A Guided Tour is designed to jumpstart the learning process with its focus on interactive hands-on learning through a wide variety of useful, practical, end-to-end tutorials. Hands-on exercises and in-depth explanations form chapter "modules" within the book, thereby providing a complete step-by-step guide to each specific topic while allowing readers to pick and choose which tutorials they want (or need) to study without necessarily going in chapter order. And, because of its modular structure, each tutorial's sample code is independent of any earlier tutorials, freeing readers to skip from tutorial to tutorial, based on their specific knowledge requirements. Because of this design, this book continues to be a useful reference as a reader's "need to know" increases. Using this book's method, readers quickly learn IBM Rational Application Developerregardless of their previous level of experience or inexperience. For beginners, there are tutorials that teach how to create Web, EJB, JMS, and Web Services applications using Rational Application Developer. For the more advanced readers, there are tutorials on security, publishing, testing, team development, profiling, and logging. Written to appeal to as broad an audience as possible, the tutorials run on common databases, including IBM Cloudscape, IBM DB2« Universal Database, Microsoft« SQL Server, Sybase« Enterprise Systems, and Oracle« Database.
LanguageEnglish
PublisherMC Press
Release dateMay 1, 2012
ISBN9781931182331
An Introduction to IBM Rational Application Developer: A Guided Tour

Related to An Introduction to IBM Rational Application Developer

Related ebooks

Programming For You

View More

Related articles

Reviews for An Introduction to IBM Rational Application Developer

Rating: 5 out of 5 stars
5/5

1 rating1 review

What did you think?

Tap to rate

Review must be at least 10 words

  • Rating: 5 out of 5 stars
    5/5
    I will check

Book preview

An Introduction to IBM Rational Application Developer - Valentina Birsan

technologies!

1

Introduction

When IBM acquired Rational Development Corporation, it seemed only natural to move the WebSphere Studio Application Developer family of tools under the IBM Rational brand. This integrates the great capabilities of Rational Application Developer with the quality tools in Rational to provide a seamless development environment. IBM Rational Application Developer is a follow-on product for WebSphere Studio Application Developer and includes many of the best development features of the Rational products.

This introduction contains:

A brief description of the other chapters in this book

An overview of Rational Application Developer

A summary of the help and support information for Rational Application Developer

An overview of the Product Updater tool that can be used to update and add new features to Rational Application Developer

Who This Book Is For

This book is aimed at beginners who want to quickly and easily learn to use Rational Application Developer. Rational Application Developer is a very complex product, and the learning curve involved in using it can be quite steep. Although this book helps introduce you to Rational Application Developer, it will be helpful if you already have some familiarity with it or its predecessor, WebSphere Studio Application Developer. Also, some of the tutorials are written with the expectation that you have at least some knowledge of Java coding.

To help you get started using Rational Application Developer quickly and effectively, tutorials in this book cover the key components in Rational Application Developer. These tutorials give you a hands-on feel for Rational Application Developer and let you learn interactively. As you work through the tutorials, you will start to understand how to use Rational Application Developer to create practical applications without going through pages and pages of background material. We have tried to use real-world examples that reflect the kind of everyday tasks you need to perform.

Many other books use only one database as the basis for their examples. We know that everyone uses different databases; therefore, this book covers five: IBM Cloudscape, IBM DB2 Universal Database, Microsoft SQL Server, Sybase Enterprise Systems, and Oracle Database. If you do not already have a database, you can use the embedded Cloudscape database that ships with Rational Application Developer.

How to Use This Book and CD

Each chapter in this book covers one concept, such as Java applications or Web development. Every chapter starts with a short introduction that covers some basic background material on the concept. Then, one or more tutorials help you explore Rational Application Developer and learn more about the concept.

Each chapter in this book stands alone; that is, you do not have to complete the tutorials in one chapter to complete those in the following chapters. Also, you generally do not need to complete the first tutorial in a chapter before you do the second or third one. In other words, you can immediately proceed to any tutorial you want.

The chapters in this book are organized as follows:

Chapter 1, which you are currently reading, provides a brief introduction to Rational Application Developer.

Chapter 2 explains how to develop, run, and debug a simple Java application.

Chapter 3 covers Web development, including developing Java Server Pages (JSP) and Java servlets, and running them in the WebSphere Application Server.

Chapter 4 introduces you to database development, and covers topics such as creating database tables, accessing data using Java programs, using a datasource, and calling stored procedures.

Chapter 5 introduces you to XML development, and covers topics such as creating XML, XML schema, and XSL files. It also discusses connecting XML to database data and using the Java API for XML processing.

Chapter 6 covers Enterprise JavaBeans (EJBs), including how to use entity EJBs, session EJBs, session bean facades, and message-driven beans.

Chapter 7 covers the Java Messaging Service (JMS) and message-driven beans (MDBs). This chapter provides detailed background information and explanations of terminology, as well as tutorials that teach you how to write a JMS application and develop MDBs.

Chapter 8 covers Web services, including how to create a Web service and its corresponding client, and how to test the client proxy and publish the Web service.

Chapter 9 covers advanced topics on Java 2 Platform, Enterprise Edition (J2EE), including WebSphere Application Server v6.0 development, annotation-based programming, enhanced EARs, J2EE and EJB security, and J2EE class loading.

Chapter 10 covers creating visual Java applications, including how to use the Java editor to create an application and set up a layout for it, and how to bind a user interface to a data source in order to access server information.

Chapter 11 covers team development with Concurrent Versions System (CVS), including how to install CVS, and how to share and merge files changes.

Chapter 12 introduces you to the Rational Application Developer Profiling and Logging tool. You learn to use it to perform tasks such as finding performance problems, tracing memory leaks, and tracing application execution.

Chapter 13 introduces you to the visual modeling capabilities available in the product. This chapter explains the capabilities for architectural and application analysis through visual modeling. You learn how to visually model key design elements in an application and how to use the Rational Unified Process for J2EE.

The CD bundled with this book includes all of the applications created in the chapters’ tutorials. You can follow the instructions in the tutorials to create the applications, or you can import the completed applications from the CD and read along. The files on the CD are named according to chapters. For example, Ch1Applications.zip contains the completed application for chapter 1.

To import the applications from the CD, follow these steps:

From the workbench, select File => Import => Project Interchange. Click Next.

Browse to the zip file and click Select All to import all the projects.

Click Finish.

The CD also includes a Cloudscape database that contains all the schemas and tables used and created throughout the book. You do not have to use this database if you follow along with the steps in the tutorials, but it is included for your convenience.

To use the completed Cloudscape database, unzip the DefaultDB.zip file anywhere. For those chapters that use a datasource (chapters 4, 6, 12, and 13), you need to modify the datasource’s database attribute from the EAR deployment descriptor (on the Deployment page) to your Cloudscape database location.

Overview of Rational Application Developer

Rational Application Developer is a new development tool for applications destined for WebSphere Application Server. Support is provided for developing J2EE 1.2, 1.3, and 1.4 applications, and for deploying to WebSphere Application Server versions 5.0, 5.1, and 6.0.

Rational Application Developer is based on the Rational Software Development Platform. This platform contains the common components for Rational version 6.0 products and is based on Eclipse 3.0.

The development platform is installed only with the first Rational version 6.0 product. If any other Rational version 6.0 product is installed, only the plug-ins (components) that make up the product will be installed. In this way, all of the products on the system have the capabilties of the development platform with the benefit of a reduced installation size. Also, for maintentance, a single installation of the development platform is much easier to update.

The Rational Application Developer tools are designed to provide the following benefits:

Broaden appeal–Rational Application Developer provides improved user assistance and ease of learning. It includes samples, examples, and other information that enables developers to quickly begin building enterprise-class applications. It also has a progressive disclosure of tools and capabilties. Many of the components and tools included with Rational Application Developer are initially hidden. When one of these is selected, its full capabilities are loaded, including views, perspectives, and icons.

Increase productivity–Rational Application Developer provides rapid application development. Many wizards and editors provide a visual development experience to simplify design and development, through the generation of underlying components. Rational Application Developer also provides rapid deployment to WebSphere Application Server version 6.0.

Extend integration–Rational technology has been integrated to enhance the application-development life cycle and increase productivity. You can use Rational Applicaton Developer to visually generate a component, analyze it, view the analysis results, and then debug it. In addition, WebSphere Portal Toolkit’s features are now included by default in Rational Application Developer and have been integrated with Web, Struts, and JavaServer Faces tools.

Maintain standards and middleware currency–Rational Application Developer provides enhanced support for the J2EE 1.4 specifications, as well as for WebSphere Application Server version 6.0.

Help and Support Information

This section contains information on:

The Rational Application Developer help system

Support information in the online help

The Log and Trace Analyzer tool

Log files

Rational Application Developer’s Help System

The product documentation helps you find information about Rational Application Developer’s tools and how to use them to achieve your development goals. The online help system lets you browse, search, and print online documentation for the product.

To access the online help, click Help => Rational Contents in the main workbench.

Rational Application Developer also includes context-sensitive help. To access the help, select an item (such as a button or field) and press F1. An infopop will open, containing a description of the item and links to related help files.

Support Information in the Online Help

To open Rational Application Developer’s online help, select Help => Rational Help from the workbench main menu. You will see a book called Troubleshooting and Support. This documentation is designed to serve as a self-help tool to resolve any problems you might encounter. It contains information on the following:

Getting fixes–If you encounter a problem, a product fix might be available to resolve it.

Troubleshooting checklists–Detailed checklists help you eliminate possible problem areas and find solutions for problems occuring within Rational Application Developer.

Searching knowledge bases–You can search both the Internet and the online help included with Rational Application Developer to determine whether the resolution to your problem is already documented.

Contacting IBM Software Support–Use this information to assess your problem, provide background information on it, and contact IBM Software Support.

The Troubleshooting and Support book also mentions the IBM Support Assistant. This tool helps you search IBM’s knowledge bases to see whether your problem is already documented. To access it, select Help => IBM Support Assistant from the main workbench menu.

The Log and Trace Analyzer Tool

Rational Application Developer includes a Log and Trace Analyzer tool that enables you to import various log files and symptom databases, against which the log files can be analyzed and correlated.

When attempting to isolate problems, one of the main problems is that you cannot easily determine the relationship of events captured by one product with the events captured by another. The Log and Trace Analyzer tool addresses this problem. It allows you to import and analyze log files (or trace files) from multiple products, as well as to determine the correlation between the events captured by these products.

Correlation is the process of analyzing and determining a set of related events, based on a set of rules that are used to interpret the data contained in the events. The Log and Trace Analyzer tool provides several ways of correlating information within a single log file, such as ordering the events in a log by time stamp, or filtering for only events with a specific thread ID. For more information about the Log and Trace Analyzer tool, refer to the online help.

Log Files

There are two main log files: .log and LoggingUtil.log. Both are ASCII files and can be easily read with any text editor. Both are located in the .metadata directory of your workspace directory. To locate your workspace directory, you can either search for the .metadata directory, or shut down and re-start Rational Application Developer. If you choose the second option, you will be prompted to specify your workspace, and your current workspace location will be listed.

The .log file is used to capture notifications, errors, and any uncaught exceptions from Rational Application Developer plug-ins. It is cumulative in its behavior—each new session of Rational Application Developer appends its messages to the end of the .log file without deleting any previous messages. This enables you to see a history of past messages over multiple Rational Application Developer sessions, each one starting with the !SESSION string.

The LoggingUtil.log file contains information about the actions of the Log and Trace analyzer and the events that plug-ins send to the Log and Trace analyzer for recording. A new LoggingUtil.log file is created each time Rational Application Developer is started. The previous LoggingUtil.log file is renamed and saved by appending a unique string, such as a time stamp, to its file name. At any given time, the <workspace>\.metadata directory contains one .log file, one LoggingUtil.log, and any additional LoggingUtil.log.* files that exist from previous Rational Application Developer sessions.

Events in both the log files are recorded by appending to the end of the file, so the files are in chronological order, with the oldest event at the beginning of the file and the most recent event at the end.

Product Updater

Rational Application Developer is structured as a collection of plug-ins. Each plug-in contains code that provides some of the product’s functionality. The code and other files for a plug-in are installed on your local computer and activated automatically as required.

Rational Application Developer’s plug-ins are grouped together into features. A feature is the smallest unit of separately downloadable and installable functionality.

It is easy to install additional features and plug-ins into Rational Application Developer, as well as to update the product’s existing features and plug-ins. The simplest way to do this is to use the Rational Application Developer Product Updater.

The Product Updater keeps track of all Rational software development products installed on your system and searches for and installs product updates and optional new features. The Product Updater is installed automatically with Rational Application Developer. Using the Product Updater, you can search for and automatically install the following kinds of updates:

Product updates, such as fix packs, refresh packs, and interim fixes

Optional features that provide new functionality

To locate and install product updates and optional new features with the Product Updater tool, follow these steps (which require Internet access):

Click Start => Programs => IBM Rational => Rational Product Updater to launch the Product Updater. An inventory of installed products is displayed.

Click the Find Updates button to search for available updates or the Find Optional Features button to search for available optional new features.

For product updates, the Updates page shows the type and number of available updates for each installed product or feature. Click each update to display its description and detailed information about it. Select the list of updates you want to install, and click Install Updates. When the installation is finished, the installed updates are removed from the Updates list and added to the Installed Products list.

For optional features, the Optional Features page shows the name and version of any new optional features that are available. Before installing any new feature, you can click its name to review the detailed information. Select the list of features you want to install, and click Install Features. When the installation is finished, the new items are removed from the Optional Features page and added to the Installed Products page.

You can also perform any of the following tasks using the Product Updater:

Change the locations that get searched to find product updates and optional new features.

Create a text file list of currently installed Rational software development products, including details such as product version, updates (fixes) applied, and installation dates.

Run the Product Updater from a command line.

For more information about these tasks, consult the Product Updater online help.

Summary

Rational Application Developer is a powerful and complicated tool. This book is designed to help you learn about its main features and feel more comfortable and confident while using it. We hope you enjoy working through this book, and find it helpful and informative.

Once you’re done learning about Rational Application Developer, you might want to learn more about other products in the Rational family. Here are some of these products:

Rational Web Developer is similar to Rational Application Developer, but primarily targeted toward developers who build J2EE Web applications that do not involve Enterprise JavaBeans (EJBs).

Rational Software Architect contains all the functionality of Rational Application Developer, as well as a complete set of modeling tools for applications.

Rational Software Modeler is a subset of Rational Software Architect. It is targeted at application architects who focus on the architecture of applications and do not need the functionality of the Rational Software Architect development tools.

Rational Functional Tester is focused on Java and Web application testing. It provides capabilities to update test cases as applications change.

For more information about these products, you can visit the Rational DeveloperWorks Web site at http://www-136.ibm.com/developerworks/rational/.

2

Developing Java Applications

Rational Application Developer is based on the open-source Eclipse development platform. The Eclipse platform has a pluggable design: Plug-ins can be added to the platform to add new functionalities. The Eclipse platform itself comes with a Java IDE (Integrated Development Environment) for Java application development. It provides some common tools and features for Java developers. To learn more about the Eclipse platform, visit http://www.eclipse.org.

When you first start Rational Application Developer, you will be asked to select a workspace location, as shown in Figure 2-1. A workspace can be any directory location where your work will be stored. If you are working on multiple projects at the same time, consider using different workspaces for each project, to ensure a clear code separation.

Figure 2-1: The dialog box for selecting a workspace location.

After you have chosen a workspace, you will see the Welcome page shown in Figure 2-2. The Welcome page offers some quick links to tutorials and samples.

Figure 2-2: The Welcome page of the Rational Software Development Platform.

Close the Welcome page, and you will see the J2EE perspective. A perspective is a consolidation of tools and views that a developer needs. Rational Application Developer offers a number of perspectives tailored for different type of developers. Some commonly used perspectives are Java, Web, Data, J2EE, and Debug.

Before developing a Java application, you might want to switch to the Java perspective. You can open a perspective from the menu bar or from the top-right icon. Once you are in the Java perspective, you have to create a new Java project to store the application. If you want, you can create a Java project that has separate source and binary folders, which separates the Java files from the compiled class files.

As you are typing Java code, you can take advantage of the Content Assist feature by pressing Ctrl-Space. This will bring up a list of available options, as shown in Figure 2-3.

Figure 2-3: Content Assist with Ctrl-Space.

During development, compilation errors are shown as problems in the Problem view. You must fix them before running the Java application. In addition to the Problem view, a Task view allows you to create a to-do list. You can either explicitly create a to-do list in the Task view, or add the comment //TODO in the Java code to automatically create a task in the view.

There are some other very handy features in the Java editor. One of them is code formatting, as shown in Figure 2-4. This feature allows you to format the Java code according to your preferences. You can change the formatting preferences in the Window preferences from the workbench. Another handy feature is Organize Imports, which makes sure all the import statements are current. It will automatically add any missing import statements that your code requires.

Figure 2-4: Changing the formatting preferences for code.

The workbench has a feature to compare the contents of two files by selecting them in the Package Explorer view. In addition, since the workbench saves all the editing history of a file that is edited in it, you can compare contents using Local History. You can see the changes you made yesterday, or even a week ago. Furthermore, you can replace the current version with the code you have in the local history. The local history persists even if you restart the workbench. You can set how much history the workbench should save by changing the preference in the Preference page.

Tip: To compare a file with local history, right-click it, and then click Compare With => Local History.

Each Java project in the workbench has a class path that it uses for execution. This is called the Java build path. If a Java application refers to other classes in another Java project, you can modify the Java build path to include the referred project. This avoids compilation errors. Furthermore, you can add external JAR files (Java archives) in the Java build path.

Tip: To edit the Java build path, right-click the Java project, click Properties, select Java Build Path, and either add the JAR file as an external JAR or select another Java project.

A Java application can be executed in the workbench. Right-click a Java file in the Package view and click Run => Java Application. The result of the application will be printed in the Console view.

Moreover, you can set a breakpoint in the Java editor and debug the Java application. To set a breakpoint, simply double-click on the left-hand ruler of the Java editor. When you start debugging the application, you will be prompted to switch to the Debug perspective. You normally debug applications with this perspective because it provides views to see the paused stack frames and variables. It also allows you to step over or into the Java code line-by-line.

The rest of this chapter guides you through the creation, running, and debugging of a simple Java application.

Tutorial : Creating Your First Java Application

In this tutorial, you create, run, and debug a very simple Java application.

Step 1: Create a New Java Project

Create a Java project named Ch1JavaProject, which will have source and binary folders to store the Java and class files, respectively:

From the workbench, select File => New => Project. Click Next.

Select Java => Java Project => Next.

Enter Ch1JavaProject as the project name, and click Next.

In the Source tab, click the Add Folder button and enter src as the folder name. Click OK. Click Yes when asked to create a new folder bin. The wizard will look similar to Figure 2-5.

Click Finish.

Figure 2-5: The Java Project wizard.

The JRE System Library has been added to the project automatically for you. Feel free to expand the JRE System Library to explore the common elements in more detail.

Step 2: Create a New Java Class

Create a Java class named Test inside a test package, as shown in Figure 2-6:

Right-click Ch1JavaProject and click New => Class.

Enter test as the package and Test as the file name.

Click Finish.

Figure 2-6: A new Java class.

Step 3: Modify the Java Class

Modify the Java class as shown in bold here:

As you type, try out the Code Assist (Ctrl-Space) function.

Step 4: Run the Java Application

Now it’s time to run your first application:

In the Package view, right-click the Test class and click Run => Java Application, as shown in Figure 2-7.

Figure 2-7: Running a Java application.

Switch to the Console view to see the result of the application, shown in Figure 2-8.

Figure 2-8: The Console view.

Step 5: Debug the Java Application

To debug a Java application, you need to set a breakpoint and run the application in debug mode.

In the Java editor, double-click the ruler on the first line inside the main() method to set a breakpoint, as shown in Figure 2-9.

Figure 2-9: Setting a breakpoint.

In the Package view, right-click the Test class, and click Debug => Java Application, as shown in Figure 2-10.

You will be prompted to switch to the Debug perspective. Click Yes.

Figure 2-10: Debugging the Java application.

In the Debug perspective, shown in Figure 2-11, the execution of the Java class is paused at the breakpoint. You can examine the variables and step through the code.

Press the Step Over button to see the changes in the Console.

Figure 2-11: The Debug perspective.

Summary

Rational Application Developer has an excellent IDE for developing Java application. Moreover, you can share Java applications with others using the CVS support or by exporting as a JAR file. Chapter 11 provides details about the CVS support.

Rational Application Developer also provides a Java Visual Editor (JVE) for creating visual Java applications, such as Swing applications. You will see more of that in chapter 9. In addition to Java application programming, Rational Application Developer supports Web, EJB, database, and XML developments, which you will see in the following chapters.

3

Introduction to Web Development

Rational Application Developer provides a robust Web development environment. You can develop Web applications and test them in the embedded WebSphere Application Server.

You use the Web perspective for Web application development. All Web elements, such as Java Server Pages (JSPs) and Java servlets, are stored in Dynamic Web projects. A Dynamic Web project is basically a Web component in the J2EE specification. WebSphere Application Server is a J2EE server that can support running J2EE enterprise applications. The J2EE application consists of several components: Application Client, Web, and Enterprise JavaBeans (EJBs). Each of these components is also known as a module. Each module is a self-contained unit that can be deployed and run on an application server individually, or as a group in a J2EE Enterprise application. This chapter focuses on the Web module.

The major contents of a Web project are JSPs and servlets. Other Web elements, such as HTML, style sheets, and images, are also stored in a Web project. Figure 3-1 shows the structure of a Web project, with its different Web elements in different locations.

Figure 3-1: The structure of a Web project.

After you create a Web project, you can see it in the Dynamic Web Projects folder. The Web project is set up in compliance with the J2EE standard, so that it could be exported as a WAR file for deployment. Java servlets and other supporting Java classes are stored in the JavaSource folder inside the Java Resources directory. JSP, HTML pages, style sheets, JavaScript files, and images are stored in the WebContent directory. The Web deployment descriptor, web.xml, contains the deployment configurations of the Web application. It must be present in the Web module in order to be deployed on a J2EE application server. The Web Site Navigation and Web Diagram tools are not in the J2EE specification. They are provided by Rational Application Developer to assist you in developing your Web applications. The Web Site Navigation editor, shown in Figure 3-2, allows you to organize your Web site’s structure and quickly generate a site map for easy navigation.

Figure 3-2: The Web Site Navigation editor.

Figure 3-3: The Web Diagram tool.

The Web Diagram tool, shown in Figure 3-3, helps your visualize the flow of Web applications, such as a Struts or Faces application.

The Web perspective offers the Gallery view, shown in Figure 3-4, which has an abundant supply of images, animations, and sounds. Another important view in the Web perspective is the Palette, shown in Figure 3-5. When you are editing an HTML file or a JSP file in the Page Designer editor, you can drag and drop the tags from the Palette onto the editor. This allows you to build the files visually.

Figure 3-4: The Gallery view.

Figure 3-5: The Palette view.

The Tutorials

The following tutorials are included in this chapter:

Tutorial 1: Introduction to Java Servlet and JSP Development

Tutorial 2: Page Template, Navigation, and Error Pages

Tutorial 3: Using JavaMail in a Web Application

Tutorial 4: Using Servlet Filters

Tutorial 5: Introduction to Struts

Tutorial 6: Introduction to Tiles

Tutorial 7: Introduction to JavaServer Faces (JSF)

These tutorials introduce you to the different aspects of Web development in Rational Application Developer.

Tutorial 1: Introduction to Java Servlet and JSP Development

This tutorial introduces you to Java servlet and JSP development in Rational Application Developer. In addition, you will run the servlets and JSPs in the embedded WebSphere Application Server.

A Java servlet is a Java class that extends the javax.servlet.http.HttpServlet class and implements the javax.servlet.Servlet interface. A servlet can provide dynamic contents to a Web application. It can also provide static contents, with some effort. A servlet can be called directly from a Web browser or indirectly from another Web resource. HTTP-based requests are sent to the servlet when the servlet is invoked. It receives the request and returns a response back to the client.

There are several methods for sending a HTTP request, such as GET, POST, PUT, HEAD, and DELETE. The GET and POST HTTP request methods are the most commonly used ones. Both GET and POST send data to the server to be processed. They differ, however, in the way the data is sent. Without getting into too much technical detail, a GET request sends the data through the URI itself, so the data is visible to users. A POST request, on the other hand, sends the data in a message body, so that it cannot be seen.

A servlet has doGet and doPost methods for handling the GET and POST requests, respectively. When a servlet is invoked from a Web browser, the doGet method is automatically called. If the servlet is called from another resource, such as an HTML form, the user can specify which of the two HTTP-based request methods to use. A servlet can obtain information from the request, such as the fields for an HTML form. It can also create a response for the client, or redirect the controls to another Web resource. A servlet can respond to the client with both static and dynamic content. The content is provided using System.out.println statements.

Unlike Java servlets, the Java Server Pages (JSP) technology allows static contents to be easily provided using the regular HTML format. The dynamic contents in a JSP page are provided by a Java scriptlet.

Both JSPs and servlets can be run on WebSphere Application Server. Before a JSP can be executed, the server compiles and translates it into Java servlets. Therefore, JSP essentially is a servlet.

JSP Java Scriptlets

Java scriptlets are Java code embedded inside a JSP. Java code in a JSP has to be enclosed by the tags <% and %>. You may supply complete Java code within the scriplet tags. For example, the following will print the word Hello in the System.out:

Client Session

A session object identifies a specific user, and it persists any application information throughout the duration of the client’s session. A session object stores the client’s specific information in the current session. JSPs and servlets running in the same server will be able to access the same session object for a specific client. The object is created by the server automatically. The following snippet shows how a servlet can access the session object and store something into the session attribute using the attribute name store:

For JSPs, an implicit variable named session is available; therefore, there is no need to obtain the session like a servlet has to. The following code snippet shows how a JSP sets an attribute:

The keyword session is only one of many implicit JSP variables; others are described in the next section.

Implicit JSP Variables

As you saw from the previous code samples, you can access the contents of the session object by simply calling getAttribute on it. The keyword session is an implicit variable in JSPs. You do not need to declare it before using it. It represents the current client HttpSession. The session variable is only implicit to the JSP—you have to get the session object from the HTTP request in a servlet. This, for example, is how you can obtain a session in a servlet:

Since they are compiled into a servlet before execution, all implicit variables have servlet equivalents. They are listed in Table 3-1.

Table 3-1: JSP Implicit Variables and Their Servlet Counterparts

Enjoying the preview?
Page 1 of 1