ABOUT THIS BOOK Getting Started with JRun is for anyone using JRun to develop applications consisting of Java Servlets, JavaServer pages, Enterprise JavaBeans, and Web Services. Part I of this book provides a general introduction to JRun and J2EE. Part II contains tutorials to show you how to build a simple J2EE application.
Page 8
Developer resources Macromedia, Inc. is committed to setting the standard for customer support in developer education, documentation, technical support, and professional services. The Macromedia website is designed to give you quick access to the entire range of online resources. The following table shows the locations of these resources. Resource Description Information on JRun...
Page 9
Provides JMC users with usage notes, procedures, and concepts. Accessing online documentation All JRun documentation is available online in HTML and Adobe Acrobat formats. To access the documentation, open the following file on the server running JRun: /docs/dochome.htm. is the directory into which you installed JRun.
Other resources You can consult the following resources for more information on topics described in JRun documentation. Books Servlets, JavaServer Pages, and Tag Libraries JRun Web Application Construction Drew Falkman Macromedia Press, 2001 ISBN: 0789726009 Java Server Pages Application Scott M. Stirling, et al. Development Sams, 2000 ISBN: 067231939X...
Page 11
JavaServer Pages Hans Bergsten O’Reilly & Associates, 2000 ISBN: 156592746X JSP Tag Libraries Gal Schachor, Adam Chace, and Magnus Rydin Manning Publications Company, 2001 ISBN: 193011009X Core JSP Damon Hougland and Aaron Tavistock Prentice Hall, 2000 ISBN: 0130882488 JSP: Javaserver Pages (Developer’s Barry Burd Guide) Hungry Minds Inc., 2001...
Designing Enterprise Applications Nicholas Kassem with the Java 2 Platform, Enterprise Addison-Wesley, 2000 Edition ISBN: 0201702770 (free download at http://java.sun.com/j2ee/download.html#blueprintsh ttp://java.sun.com/j2ee/download.html#blueprints/a Building Java Enterprise Systems Paul Perrone and Venkata S.R. “Krishna” .R. with J2EE Chaganti Sams, 2000 ISBN: 0672317958 J2EE: A Bird's Eye View (e-book) Rick Grehan Fawcette Technical Publications, 2001 ISBN: B00005BAZV...
Page 13
Server Side http://www.theserverside.comhttp://www.theserversid e.com/a Dot Com Builder http://dcb.sun.comhttp://dcb.sun.com/a Servlet Forum http://www.servletforum.com/http://www.servletforum .com/a Other resources xiii...
Page 14
Contacting Macromedia Corporate Macromedia, Inc. headquarters 600 Townsend Street San Francisco, CA 94103 Tel: 415.252.2000 Fax: 415.626.0554 Web: http://www.macromedia.com www.macromedia.com/a Technical support Macromedia offers a range of telephone and web-based support options. Go to http://www.macromedia.com/support/http://www.macromedia .com/support//a for a complete description of technical support services.
PART I Introduction This part contains an introduction to JRun and J2EE. Welcome to JRun......................3 JRun Programming Model ..................15 Introduction to J2EE....................29 Using Servlets and JSP.................... 37 Introduction to EJB .....................47 Developing Web Applications ................55...
CHAPTER 1 Welcome to JRun This chapter contains an overview of Macromedia JRun and JRun architecture. It describes many of the features and tools that you use during application development and deployment. It also includes a description of different types of JRun users and pointers to the JRun documentation where each type of user can find additional information.
About JRun JRun is a complete Java application server for developing and deploying reliable, scalable, and secure server-side Java 2 Enterprise Edition (J2EE) applications. JRun supports the latest industry standards for developing applications composed of Java Servlets, JavaServer pages (JSP), Enterprise JavaBeans (EJB), Java Message Service (JMS), Macromedia Flash files, HTML pages, images, and other resources.
Benefits of using J2EE The J2EE platform represents a single standard for implementing and deploying multitier, enterprise applications. J2EE applications take advantage of Java’s inherent portability and can be run on all platforms supported by JRun. J2EE offers a number of benefits for application programmers, including the following: Rich set of standard APIs and services High performance Integration with backend infrastructure, such as Relational Database Management...
J2EE standards compliance Macromedia JRun is a J2EE-compliant application server. JRun passed the J2EE Compatibility Test Suite (CTS) and delivers the full capabilities of the most recent Sun J2EE 1.3 platform specification. The following table describes the many enhancements and new features in the 1.3 specification: Feature Description EJB 2.0...
For detailed information on the J2EE platform, see Chapter 3, “Introduction to J2EE” on page JRun architectural model JRun has a service-based architecture, a modular design that contains pluggable services (components) in a core server backbone. The server backbone facilitates communication among all components or services that plug into it.
Because services are independent of each other and can be restarted individually, the JRun service-based architecture ensures high application availability. It also provides a highly customizable and easily extensible platform. You can unplug unneeded services to avoid the overhead of unused functionality. Administrators, advanced developers, and Original Equipment Manufacturers (OEMs) can also create their own custom services (MBeans) and plug them into the JRun kernel.
Page 23
You enable dynamic compiling and reloading by creating a WEB-INF/jrun-web.xml file that contains the following text, setting the reload and compile values to false or true: <jrun-web-app> <reload>true</reload> <compile>true</compile> </jrun-web-app> For more information, see JRun Assembly and Deployment Guide. Dynamic creation of database tables for entity beans When you deploy an entity bean, and its required database tables do not yet exist, JRun can generate them for you when you have configured the appropriate Java Database Connectivity (JDBC) data source in JRun.
Page 24
in the cluster, eliminating the risk inherent to a single point of failure. For more information, see JRun Administrator’s Guide. Security JRun supports the JAAS 1.0 specification, which includes the following levels of security: JRun uses JAAS as the security framework. JAAS is JRun administration security a set of packages that lets JRun authenticate users and enforce access controls in a modular fashion.
Page 25
The Enterprise Deployment Wizard features a context-sensitive online Help system. For usage information, see the online Help. XDoclet integration JRun provides integration with XDoclet, a popular open-source Java utility that greatly simplifies the development of J2EE modules (such as EJBs, servlets, and custom tags) by letting you maintain your component metadata using Javadoc-style tags in a single source file.
Application Description World music An e-commerce application that sells music. This application also contains an admin module. The world music application illustrates many common design patterns using JSP, servlet, and JavaBean programming. The world music sample application uses the worldmusic database, accessing it directly through JDBC. Web services Programming techniques used in JRun web services programming.
Development tools Macromedia provides development tools (available separately) to work with JRun. Additionally, you can use JRun with your Java IDE. Dreamweaver MX Dreamweaver MX is an HTML editor for visually designing and managing websites and JSPs created with JRun. Dreamweaver MX increases JSP developer productivity with the following coding tools and features: Accelerate development with powerful two-way visual Visual programming...
Page 28
What to do next The remaining chapters in Part I provide a general introduction to JRun and J2EE. These chapters include the following: Chapter 2, “JRun Programming Model” on page 15 Chapter 3, “Introduction to J2EE” on page 29 Chapter 4, “Using Servlets and JSP” on page 37 Chapter 5, “Introduction to EJB”...
CHAPTER 2 JRun Programming Model JRun is a complete Java application server for developing and deploying J2EE enterprise applications. This chapter describes the JRun programming model, the major components of JRun, and JRun support for J2EE enterprise applications. Contents Enterprise application architecture................. 16 JRun programming environment................
Enterprise application architecture The standards for enterprise application development on the web are based on the J2EE specification. JRun supports the J2EE application model and provides a runtime environment for executing enterprise applications. Enterprise application design Enterprise applications share several common design goals, including the following: It should be easy to add and maintain new Extensibility and maintainability functionality.
Page 31
The business logic of a website. The middle tier contains both the Middle tier presentation logic (web tier) and the business rules (EJB tier) that define a website. You use JRun to implement applications for the middle tier. Makes application functionality available on the web. It accesses data Web tier and business functionality from other tiers and often encapsulates some user interaction control.
JRun support for the three-tier model You use JRun to implement J2EE applications for the middle tier of the three-tier model. JRun completely supports the latest industry standards for developing enterprise applications composed of the following: Java Servlets are server-side components written in Java that let you Java Servlets add custom functionality to a web server.
JRun programming environment JRun lets you develop dynamic J2EE applications that include servlets, JSPs, and EJBs. Your applications can be hosted by a third-party web server, such as IIS or Apache, or by the integrated JRun Web Server (JWS). The following figure shows a system using JRun to develop and deploy applications: This figure shows the four main components of a JRun system: Provides the necessary services and web services to process J2EE JRun server...
JRun provides a runtime environment for EJB components. The EJB specification defines a software component model whereby you can deploy server-side application logic (enterprise beans) using EJB-compliant application servers. The JRun EJB container provides automation of services, including component lifecycle, naming, transaction management, messaging, resource management, security, distribution, state management, and persistence.
Windows services considerations A JRun server responds to requests only after it is started. During JRun installation on a Windows NT or 2000 system, you can configure JRun as a Windows NT service. If you choose services, JRun starts all three servers when you start your Windows NT or 2000 system.
Using the command line You can start and stop JRun using the jrun.exe and jrun shell script command-line utilities. Use the following syntax: jrun {options} {server-name} The following table describes the options: Option Description -start Starts JRun. -stop Stops JRun. -restart Restarts JRun.
Page 37
This figure differs from the previous JRun system figure because it shows a process view of JRun—that is, it shows the services that run within each of the JRun server processes. As the figure shows, the JWS associated with each server runs within that server’s process. When you install JRun, it dynamically assigns a web server port number that is not in use, starting at the default location.
Page 38
By default, when a JRun server starts, the corresponding JWS also starts. However, to configure a third-party web server to communicate with the default JRun server, see Installing JRun. Samples server The samples server hosts the following JRun sample applications at http://localhost:8200: Application Description...
Web servers JRun extends a web server to enable it to process J2EE applications that deliver dynamic content generated by servlets, JSPs, and EJBs hosted by the JRun server. Because a web server acts as a client that communicates with JRun, the web server must establish a connection to JRun.
JRun support for J2EE applications JRun completely supports the latest industry standards for developing J2EE applications, including the following J2EE modules: Web applications EJBs Enterprise resource adapters Enterprise applications JRun lets you deploy J2EE modules from expanded directories or from archive files. The following table lists these components and files: Component Archive file...
In addition to the standard deployment descriptors, JRun creates JRun-specific deployment descriptor files for web applications, EJBs, resource adapters, and J2EE client applications that let you configure features that are unique to JRun. JRun-specific deployment descriptor names are made up of the corresponding standard deployment descriptor filenames preceded by the prefix jrun-.
XDoclet (for EJBs and web applications) JRun provides integration with XDoclet, a popular open-source tool that generates EJB interfaces and deployment descriptors based on special Javadoc comments in an enterprise bean implementation source file. You can also use XDoclet to generate web application deployment descriptors and JSP tag library descriptors based on comments in servlet or tag library source files.
CHAPTER 3 Introduction to J2EE This chapter familiarizes you with the Java 2 Platform, Enterprise Edition (J2EE) and its related technologies. Contents J2EE environment ....................30 J2EE platform technologies ................... 31 J2EE APIs......................33 Additional resources....................36...
J2EE environment The J2EE platform represents a single standard for implementing and deploying multitier enterprise applications. Typically, such applications are configured as a client tier to provide the user interface, one or more middle tiers that provide client services and business logic for an application, and a data tier consisting of backend enterprise information systems (EIS) that provide data management.
J2EE platform technologies The J2EE platform specifies technologies to support multitier enterprise applications. These technologies fall into the following three categories: Developers use component technologies to create the essential parts Components of an enterprise application: the user interface and the business logic. You develop components as reusable modules that you can share among multiple enterprise applications.
For more information on J2EE application assembly and deployment, see JRun Assembly and Deployment Guide. Containers provide all application components with the J2EE platform Application Programming Interfaces (APIs). For detailed descriptions of the APIs, see “J2EE APIs” on page Communication Communication technologies provide mechanisms for communication between clients and servers and between collaborating objects hosted by different servers.
J2EE APIs J2EE builds on the existing technologies in the Java 2 Platform, Standard Edition (J2SE). J2SE includes the base Java support and various libraries with support for applets and applications. A J2EE compliant application implements both the J2EE and J2SE APIs. The following figure shows the technologies included with J2EE: The following sections describe the J2EE platform specifications.
Java Database Connectivity 2.0 The Java Database Connectivity (JDBC) API is a standard Java extension for data access that lets Java programmers code to a unified relational database API. By using JDBC, you can issue SQL statements and process database results from Java programming language methods.
Java Transaction API (JTA) to demarcate where the entire transaction, including both operations, begins, rolls back, and commits. To facilitate transactions, J2EE has two APIs: JTA and Java Transaction Service (JTS). JTA is a high-level transaction interface that your applications use to control transactions. JTS is a set of low-level transaction interfaces that EJBs use behind the scenes (your client code does not directly interact with JTS).
Java Authentication and Authorization Service 1.0 Java Authentication and Authorization Service (JAAS) provides a way for a J2EE application to authenticate and authorize a specific user or group of users. JAAS is a Java programming language version of the standard Pluggable Authentication Module (PAM) framework that extends the Java 2 platform security architecture to support user-based authorization.
CHAPTER 4 Using Servlets and JSP This chapter introduces servlet development using Java and JSPs. It highlights concepts and objects common to both environments. It includes sample code for a Java Servlet and a JSP, as well as a summary of changes in the servlet API. Contents Using Java Servlets ....................
Using Java Servlets Java Servlets are server-side components written in Java that let you add custom functionality to a web server. A servlet runs on a web server and features high performance, database connectivity, stability, and security. Unlike CGI scripts, which are often written in C++ and Perl for a specific web server implementation, servlets are written in Java and, therefore, include all the benefits of the Java programming language, including portability—the “write once, run anywhere”...
Servlet benefits Servlets offer many benefits to web developers over existing server-side application development technologies. Some of these benefits are associated with the Java programming language and others are associated with servlet technology. This section discusses several benefits of using servlets and Java. Benefits of using servlets Servlet technology offers the following benefits to web developers: Because servlets are invoked through the web server, your business logic...
Creating servlets JRun provides two methods for creating servlets: writing Java programs and writing JSPs. When you write Java programs, you have full access to the data-processing capabilities and benefits of Java. Typically, you use Java to write servlets that perform complex data manipulation such as database access.
Servlets and JSPs With JRun, you can create server-side Java applications using servlets and JSPs. Because JRun translates JSPs into servlets, many of the objects available to native servlets (as follows) are also available natively to JSPs: HTTP requests and responses Output Filters Event Listeners...
Servlets answer a request by constructing an HTTP response and sending that response back to the client. Within your servlet, you access the HTTP response to write information within the response that is sent back to the client. You can access HTTP response information in the following ways: In a Java Servlet, use the object, which javax.servlet.HttpServletResponse...
Using event listeners One of the major new features of the Java Servlet 2.3 specification is the addition of servlet event listeners. The specification provides listener classes for web applications. Also known as event handlers, event listeners provide call-back methods that JRun calls when certain events occur.
Working with sessions HTTP is a stateless protocol. A web server receives a request, sends a response, then terminates the connection to the client. The web server does not maintain information about the client, so it cannot determine when another request comes from the same client.
You can access application information in the following ways: In a Java Servlet, use the object. javax.servlet.ServletContext In a JSP, use the implicit JSP application object. Accessing configuration information JRun passes configuration information to a servlet when it is initialized. This configuration information includes name-value pairs, which describe initialization parameters, and the servletConfig object, which describes the context within which the servlet is running.
Writing servlets as JSPs The previous section includes an example of a servlet written in Java. JRun also supports a second method for developing servlets that relies less on Java coding: JSPs. JSPs let you create servlets from text files containing a combination of HTML and scripting code. The scripting code within a JSP is a combination of JSP syntax and, typically, JavaScript (a subset of ECMAScript) or Java.
CHAPTER 5 Introduction to EJB This chapter describes fundamental Enterprise JavaBean (EJB) elements and concepts and JRun-specific EJB features. JRun Programmer’s Guide explains these features in detail. Contents EJB Overview ......................48 Container services....................49 EJB types....................... 49 Using EJB in JRun....................52...
EJB Overview EJBs provide an architecture for building distributed, component-based, enterprise-level Java 2 Platform, Enterprise Edition (J2EE) applications. Enterprise JavaBeans components, called enterprise beans, are scalable, transactional, and secure. EJBs written using only features in the EJB specification are portable to other J2EE application servers.
Container services The EJB specification requires that EJB containers support a variety of services, each of which provides advanced functionality that EJBs can use with little or no coding in the EJB. The following table describes EJB container services: Service Description Lifecycle JRun automatically increases and decreases the number of available...
provide a high-level interface facade to business processes, in which session bean methods perform a unit of work, calling one or more methods in one or more entity beans. For more information, see JRun Programmer’s Guide. Stateful session beans You can perform some business processes in a single business request, such as computing the price of goods or verifying a credit card account.
Support for EJB 2.0 CMP EJB 2.0 features major revisions to CMP, including the following: The EJB implementation class is defined as an abstract class. Persistence is managed using EJB query language (EJBQL). Relationships let you maintain relationships between entity beans. CMP beans can use select methods in addition to finder methods.
Using EJB in JRun This section describes features and functionality unique to the JRun EJB architecture. Stubless deployment One of the main JRun EJB features is stubless deployment. There is no JRun EJB deployment tool. You compile the EJBs, package them in a JAR file (optional), copy the compiled EJB interfaces to the client, and define this location in the client classpath.
XDoclet JRun provides integration with XDoclet, an open source tool that generates code and deployment descriptors for EJBs, web applications, and JSP tag libraries. Many application servers are adding support for XDoclet and EJBs that are written using basic XDoclet tags, which are portable across servers. For EJBs, you can use XDoclet to do the following tasks: Generate required EJB callback methods for a bean implementation using an abstract class containing business methods as input.
CHAPTER 6 Developing Web Applications This chapter describes the structure of a web application and how JRun processes a web application and the resources in it. It also describes how to create, package, and deploy a web application. Contents Introduction to web applications ................56 About the web application directory structure ............
Introduction to web applications A web application consists of servlets, JSPs, HTML pages, images, a standard deployment descriptor, optionally, JavaBeans and custom tag classes, and other resources, known as web components. You place these resources in a standard, predefined directory structure so that you can deploy them on any web application server.
When you distribute a web application on JRun, you can distribute it as the expanded directory structure, or you can distribute it as a single, compressed file called a Web ARchive (WAR) file. A WAR file contains the complete directory structure and all the files that define the application.
About the web application directory structure The following figure shows the directory structure of a web application: The application root directory, web_app in the figure, functions as the document root for serving application files. The web_app directory is often the WAR filename. This directory includes JSPs and HTML pages that you develop as part of a web application.
Directory Description WEB-INF/jsp Contains the files generated by JRun when translating a JSP. WEB-INF/sessions Contains serialized JRun sessions used by the default session persistence mechanism. Enterprise applications conform to a similar directory structure as web applications. The application root directory, web_app, is often the EAR filename. The enterprise application deployment descriptor, application.xml, is located in the jrun_root/servers/jrun_server/web_app/META-INF directory.
In addition to the standard deployment descriptors, you can use the following JRun-specific deployment descriptor files: Contains web application elements that are specific to the WEB-INF/jrun-web.xml JRun application server. This deployment descriptor lets you set a context-root for the web application, enable and disable automatic servlet compilation and reloading, set virtual paths, configure servlet session persistence and cookies, and set values for JNDI names.
Each web application running in a JRun server can have one context root mapping and multiple servlet mappings. You can define the context root in the jrun-web.xml file or let JRun substitute one for you. JRun determines the context root in the following order: 1 Checks the /WEB-INF/jrun-web.xml file for a context root mapping.
Developing web applications This section describes how to create a web application, and then describes how to add resources and components to the web application. Creating a web application JRun provides an empty web application on the default JRun server that you can use for developing web applications.
Page 77
Adding HTML pages The application root directory functions as the document root for serving application files. Add HTML pages for your application under the application root, or in any subdirectory of the application root except under the WEB-INF directory. For example, for a web application located at jrun_root/servers/jrun_server/web_app, you could include the default welcome file at jrun_root/servers/jrun_server/web_app/index.html.
Page 78
When a request URI has a servlet path that matches a specified URL pattern, JRun invokes the associated servlet. If you do not explicitly define a servlet, you can use the /servlet implicit mapping to request a servlet that you store in the WEB-INF/classes directory.
Adding additional resources Resources that you can add include image directories, JavaBeans, and class files for resources such as database drivers. When adding these resources to a web application, you must place them in the correct location in the web application directory structure. If the resource is represented by a .class or JAR file, it must be located in a directory included in the web application classpath (typically WEB-INF/classes or WEB-INF/lib).
Packaging a web application for deployment Prior to deploying a web application, you must create the application deployment descriptors and package its parts in the appropriate directory structure. Although there are many ways to prepare a web application for deployment, the basic web application deployment procedure includes the following steps: 1 Create a staging directory tree containing the JSPs, HTML files, images, and other referenced files that must be accessible by the user’s web browser.
PART II Tutorials Part II contains tutorials to show you how to build a simple J2EE application. In the tutorial lessons, you add a JRun server and write the code for Java Servlets, JSPs, JavaBeans, and EJBs. You learn how to combine these elements to produce a flexible and scalable J2EE application.
LESSON 1 Servlet Tutorial The following tutorial lessons show you how to develop, deploy, and run a simple J2EE application consisting of servlets, JavaServer Pages (JSPs), JavaBeans, and Enterprise JavaBeans (EJBs). In this lesson, you set up the development environment, add a JRun server for the tutorial, and deploy the tutorial template application.
Setting up the development environment Before beginning the tutorial lessons, ensure that you set up your development environment. To set up the development environment: 1 Install the following software: Software Requirements JRun-supplied web server (JWS) Web server Supported external web server (optional) Text editor Any text editor Java integrated development environment (IDE)
Getting started with JRun Begin working with JRun by starting the admin JRun server. To start the admin JRun server, do one of the following: Use the JRun command-line utility, from the jrun_root/bin directory (jrun_root is the directory into which you installed JRun): jrun -start admin (Windows only) Select Start >...
(Windows only) Select Start > Programs > Macromedia JRun 4 > JRun Management Console. The JMC login window appears: 2 Enter your user name and password, and click Login. Note: You set your user name and password during JRun installation. For information, see Installing JRun.
Page 87
You might add a JRun server for the following reasons: Many organizations build sites that are comprised of multiple web applications. By having multiple JRun servers you can isolate these web applications. If one JRun server stops responding or must be restarted, the other servers and their applications remain running.
Page 88
2 Keep the default Host Name (localhost), enter the JRun Server Name for the tutorial application, tutorial, and the JRun Server Directory (added automatically when you enter the server name, {jrun.home}/servers/tutorial): The server creation process replaces the symbolic variable {jrun.home} with the directory in which you installed JRun.
5 After the JMC refreshes, you see the tutorial server listed in the Available Servers table on the JMC home page and in the left pane. Start 6 Start the tutorial server by clicking its start icon. The server status changes from Stopped to Running. Deploying the tutorial enterprise application An enterprise application contains web applications and EJBs.
In the JMC, you see the compass tutorial application deployed on the tutorial server: Tutorial application The following figure shows the JRun directory structure, expanded beneath the tutorial server, containing the deployed tutorial (compass) enterprise application: jrun_root tutorial jrun_server Adding JDBC data sources Using the JMC, you can add, remove, edit, and test JDBC data sources.
Page 91
To add a new JDBC data source: 1 In the left pane in the JMC, expand the tutorial server menu, expand Resources, and click JDBC Data Sources. The JDBC Data Sources window appears: 2 In Data Source Name, enter compass. Select Pointbase Server in the Database Driver drop-down list box.
Page 92
4 Click Submit. The compass data source is added in the JDBC Data Sources table: 5 Start the compass (tutorial) database: (Windows) Open the jrun_root/pointbase directory and double-click on compassdb.bat. (Linux/UNIX) Run the compassdb shell script. 6 Restart the tutorial server in one of the following ways: In the JMC, click the Restart icon next to the tutorial server.
Page 93
8 Verify the application deployment by opening a web browser to the following URL: http://localhost:8101/compass/logon.jsp Note: Use 8101 or the web server port number that JRun assigned when you created the tutorial server. View the tutorial application login page: In subsequent tutorial lessons, you code the application pages and business logic, including logging in, accessing trip data, and booking a trip.
Compass Travel J2EE application The Compass Travel tutorial application lets users browse adventure trip descriptions, booking information, and make trip reservations. You use HTML, servlets, JSPs, Java Beans, and EJBs to create the static and dynamic portions of the application. Coding the logon servlet A servlet is a server-side Java program that extends the functionality of a web server.
Page 95
String firstName=""; String lastName=""; String sql= "SELECT password, first_name, last_name FROM user WHERE user_id=?"; Connection connection=null; PreparedStatement ps=null; ResultSet rs=null; boolean validLogon=false; try { InitialContext ctx=new InitialContext(); DataSource ds=(DataSource) ctx.lookup("compass"); connection=ds.getConnection(); ps=connection.prepareStatement(sql); ps.setString(1, userId); rs = ps.executeQuery(); if (rs.next()) { if (password.equals(rs.getString("password"))) { validLogon=true;...
3 Copy the following code and save it as jrun_root/servers/tutorial/compass-ear/ compass-war/WEB-INF/jrun-web.xml: <?xml version="1.0" encoding="ISO-8859-1"?> <jrun-web-app> <reload>true</reload> <compile>true</compile> </jrun-web-app> 4 Restart the tutorial server in one of the following ways: In the JMC, click the Restart icon next to the tutorial server. In the JRun Launcher, select the tutorial server, and click the Restart button.
LESSON 2 JSP Tutorial In this lesson, you code JavaServer Pages (JSPs) that comprise the body of the Compass Travel tutorial application. You build a JSP with embedded Java code to access database information and develop a JSP that uses a JavaBean. Contents Creating a home page JSP..................
Creating a home page JSP JSPs let you create dynamic pages containing a combination of HTML, Java, and scripting code. JSPs deliver high performance because the first time that a client requests a JSP, the page is translated into a Java Servlet and compiled. At runtime, you execute the compiled servlet.
Page 99
stmt=connection.createStatement(); trips=stmt.executeQuery(sql); while (trips.next()){ %> <!--output the trip information (notice that tripID is used for the link but name is what displays)--> <tr> <td class="contentBG"> <a href="tripdetail.jsp?tripId=<%=trips.getInt("trip_id") %>"> <%=trips.getString("name") %></a></td> <td class="contentBG"><%=trips.getString("teaser") %></td> <td class="contentBG"><%=nf.format(trips.getDouble("price")) %></td> </tr> <% } catch (Exception e) { out.println(e);...
Accessing a JavaBean A JavaBean is a particular type of a Java class that conforms to a set of design standards specified in the JavaBean API. A JavaBean does not inherit from any particular base class or interface but must provide methods for accessing its properties. A JavaBean is different from an EJB.
Page 101
To examine the JavaBean code: Open the file TripBean.java located in jrun_root/servers/tutorial/compass-ear/ compass-war/WEB-INF/classes/compass and look at the following Java code: Public class with several private fields declared TripBean Public accessor (get) methods for all of the private fields declared Public mutator (set) method for the id fields, as follows: public void setTripId(int tripId) { this.tripId=tripId;...
4 Review the code in tripsummary.jsp, also located in jrun_root/servers/tutorial/ compass-ear/compass-war. It uses a JSP action to instantiate and reference the JavaBean to provide TripBean trip summary information: <jsp:useBean id="trip" scope="session" class="compass.TripBean"/> The reservation page reuses the JavaBean to get the trip name, start date, TripBean end date, and price, and presents the data in a different format: Summary...
LESSON 3 EJB Tutorial In this lesson, you use EJBs to coordinate the process of booking a trip reservation, including validating a credit card payment and creating an order. You learn how to invoke an EJB from a JSP and how to write an EJB that invokes other EJBs.
Using EJBs EJBs provide a component architecture for building distributed object-oriented business applications using Java. An application server that supports EJBs allows Java developers to concentrate on implementing the business logic of an enterprise application as reusable components. The EJB server handles the complexity of lower-level services, such as component life cycle, state management, persistence, multithreading, connection pooling, transaction management, and security.
Invoking an EJB from a JSP In the next procedure, you code a JSP scriptlet to access the reservation EJB, a session bean. Then you examine the code for the remote interface, the remote home interface, and the bean’s implementation class. To access an EJB from a JSP: 1 Open the file reservationaction.jsp located in jrun_root/servers/tutorial/compass-ear/ compass-war.
Page 106
4 Select and book a trip reservation: Note: You must login using a defined user (either john, password john, or mary, password mary) to make a trip reservation. Visa credit cards are accepted; all other credit cards are declined. Enter any number in the Number field. Enter any MM/YY in the Expiration field.
Booking a trip reservation To book a trip reservation, you must validate a credit card and create an order for the trip. The reservation EJB is a workflow session bean that coordinates the trip booking transaction. The reservation EJB calls the credit card EJB and the order EJB: The credit card EJB is a session bean that has a method.
Examining the code for the credit card EJB You can review the Java source code for the credit card EJB located in the tutorial_root/ compass-ear/creditcard/compass directory. Open and look at the code in the following files: This EJB has only one business method called CreditCardRemote.java validate that validates the user’s credit card.
Summary In this tutorial lesson, you used EJBs to coordinate the process of booking a trip reservation, including validating a credit card payment, and creating an order. You learned how to invoke an EJB from a JSP and how to write an EJB that invokes other EJBs.
LESSON 4 Web Services Tutorial In this tutorial lesson, you deploy a web application called TravelNet that uses a web service to access and book adventure trips from the Compass Travel sample application. You can do the web services tutorial lesson without having completed the servlet, JSP, or EJB tutorial lessons in the preceding chapters.
Using JRun web services JRun lets you publish and consume web services. Web services provide platform- and location-independent computing using XML and standard Internet protocols such as HTTP. By allowing previously incompatible applications to interoperate over the web regardless of language, platform, or operating system, web services create new business opportunities and help companies adapt to changing business relationships.
Setting up the development environment You can do the web services tutorial lesson without having completed the servlet, JSP, or EJB tutorial lessons in the preceding chapters. However, if you completed Lesson 1, “Servlet Tutorial” on page 69, you can proceed directly to “Deploying the TravelNet enterprise application”...
Getting started with JRun Begin working with JRun by starting the admin JRun server. To start the admin JRun server, do one of the following: Use the JRun command-line utility, from the jrun_root/bin directory (jrun_root is the directory into which you installed JRun): jrun -start admin (Windows only) Select Start >...
(Windows only) Select Start > Programs > Macromedia JRun 4 > JRun Management Console. The JMC login window appears: 2 Enter your user name and password, and click Login. Note: You set your user name and password during JRun installation. For information, see Installing JRun.
Page 116
To add a JRun server: 1 In the top menu bar of the JMC, click Create New Server. The Creating a New JRun Application Server window appears: 2 Keep the default Host Name (localhost), enter the JRun Server Name for the tutorial application, tutorial, and the JRun Server Directory (added automatically when you enter the server name, {jrun.home}/servers/tutorial): Lesson 4 Web Services Tutorial...
Page 117
The server creation process replaces the symbolic variable {jrun.home} with the directory in which you installed JRun. 3 Click Create Server. The Port Settings window appears: Note: Make a note of the web server port number. You use it to access the TravelNet application on the tutorial server.
Deploying the TravelNet enterprise application You typically distribute an enterprise application as a single, compressed enterprise archive (EAR) file. The EAR file contains the complete directory structure and all the files that define the application. In JRun, you deploy an EAR file in one of the following ways: Place the EAR file in an autodeploy directory on the target server.
Authoring a web service The Compass Travel sample application simulates a vacation reservation system in which you can browse adventure trip descriptions and booking information, and make trip reservations. Compass Travel exposes two web services, Trip and Reservation, in its WSDD file.
Generating WSDL from a published web service You provide the information necessary to invoke a web service by creating a WSDL file, an XML document that describes the purpose, location, and information on how to access a web service. The WSDL file also describes the methods that you can invoke and their data types.
Page 121
The Save As window appears: 4 Enter the filename Trip.wsdl. Note: In the Save as type drop-down list box, select All Files (*.*) before you click Save, or .xml is appended to the filename. 5 Save the file in the jrun_root/servers/tutorial/travelnet-ear/travelnet-war/WEB-INF/ classes directory.
7 In the web browser menu bar, select File > Save As. The Save As window appears: 8 Enter the filename Reservation.wsdl. Note: In the Save as type drop-down list box, select All Files (*.*) before you click Save, or .xml is appended to the filename. 9 Save the file in the jrun_root/servers/tutorial/travelnet-ear/travelnet-war/WEB-INF/ classes directory.
Page 123
To generate proxy code: Open a command window and enter the following commands: >cd jrun_root\servers\tutorial\travelnet-ear\travelnet-war\WEB-INF\classes >setenv Note: In the setenv.bat file, JRUN_HOME is set to the default JRun installation directory (jrun_root). If you installed JRun in a location other than the default directory, edit JRUN_HOME in the setenv.bat file.
File Description TripService A factory interface that the ServiceNameLocator.java class implements. (ServiceName.java) The interface name is derived from the service name in the WSDL document. TripServiceLocator A factory for obtaining proxy instances. It implements the ServiceNameService.java interface. It is generated from (ServiceNameLocator.java) the service name in the WSDL file.
Page 125
</tr> <% Home.jsp displays a list of trips by invoking the method of the Trip web getList service provided by Compass. 2 Open the file jrun_root/servers/tutorial/travelnet-ear/travelnet-war/tripdetail.jsp. Add the following code where indicated in the file: int tripId=Integer.parseInt(request.getParameter("tripId")); // 1. Instantiate a proxy factory TripService factory = new TripServiceLocator();...
Page 126
The TravelNet home page appears containing trip information: 2 Select and book a trip reservation: Enter any First Name. Enter any Last Name. Select any credit card. Enter any number in the Number field. Enter any MM/YY in the Expiration field. Reservation.jsp displays a confirmation ID if the reservation is successful, or an error message if a problem occurred.
Summary In this tutorial lesson, you used TravelNet, a web service client application on the tutorial JRun server, to consume a web service from the Compass Travel application residing on the samples server. In the tutorial procedures, you learned how to do the following: Author a web service in a WSDD file.
INDEX deployment descriptor Enterprise ARchive (EAR) file. See application.xml file EAR files admin JRun server definition enterprise beans 33, 48 application mappings JRun specific Enterprise Deployment Wizard application object web.xml file EJBs, using with 27, 53 auto deploy development tools starting about Dreamweaver MX...
Page 130
J2EE platform jrun executable (UNIX) PrintWriter interface technologies JRun Launcher programming model tiers JRun Management Console (JMC). proxy clients JAAS See JMC about Java JRun servers JSP-based benefits of about server-side adding reference implementation, auto Java 2 Platform Enterprise Edition admin deploy (J2EE).
Need help?
Do you have a question about the 38000382 - Macromedia JRun - Mac and is the answer not in the manual?
Questions and answers