Building Java Servlets with Oracle JDeveloper Chris Schalk Oracle Corporation Introduction Developers today face a formidable task. They need to create large, distributed business applications. The actual creation of any large application is problematic. This development process is compounded when an application is distributed, or if that application resides in multiple pieces across a network. Especially when an Enterprise application must be consistent and reliable across multiple platforms that are scattered across the Web. A major issue concerns the deployment of such applications. On the one hand, the changes are being driven by the rapid growth of business on the Internet, which has created new business models, reduced time to market, and significantly changed the way corporations look at developing and deploying enterprise applications. On the other hand, the Internet also provides the medium for accelerated adoption of distributed computing. It provides standard protocols, like HTTP and IIOP, built on TCP/IP using the World Wide Web browser as its interface, and it allows universal access to these applications. The Internet has created several new classes of applications, such as electronic commerce and self-service applications that need to be integrated with traditional enterprise applications. An issue many enterprises face is the use of applications that are primarily standalone. These applications must be created and launched as quickly as possible without being locked into one platform. The ideal enterprise application would be one that was written once and which would run on all platforms. Java is currently the most dynamic development environment in the industry. It allows you to build a set of tools that can be used on any project with any client and on any platform. Prior to the introduction of Java technology, the dream of code-reuse that was unveiled by the creation of object-oriented programming was not completely fulfilled. If code cannot be reused on every platform, the benefits of code-reuse are limited. With Java technology, you can assure a prospective client that a project is already halfway to completion, and that with this headstart, they will have more time to formulate strategies that will assure them better market positioning. Now there is an ideal solution for the deployment of such applications from the server side, that realizes the goal of code reuse within a distributed environment. This solution is servlets What are Servlets? A servlet can be thought of as a server-side applet. Servlets run inside a Server in the same way that applets run inside a Web Browser. A Browser can submit a request to execute a servlet directly, or it can be stand-alone regarding its actions, just as a browser can request an applet directly. Servlets are a powerful addition to the Java environment. They are fast, safe, reliable, and 100% pure Java. Because servlets plug into an existing server, they leverage a lot of existing code and technology. The server handles the network connections, protocol negotiation, class loading, and more; all of this work does not need to be replicated! And, because servlets are located at the middle tier, they are positioned to add a lot of value and flexibility to a system. Because of their flexibility and power, servlets can play a major role in your web application architecture. They Paper 452 / Page 1
can perform the application processing assigned to the middle tier, act as a proxy for a client, and even augment the features of the middle tier by adding support for new protocols or other features. A middle tier acts as the application server in so called three-tier client/server systems, positioning itself between a lightweight client like a web browser and a data source. An HTTP servlet is a can be thought of as replacement for Common Gateway Interface (CGI) scripts. HTTP servlets are accessed by the user entering a URL in a browser or as the target of an HTML form action. For example, if a user enters the following URL into a browser address field, the browser requests a servlet to send an HTML page with the current time: http://localhost/servlet/datetimeservlet The DateTimeServlet responds to this request by sending an HTML page to the browser. Note that servlets are not restricted to generating web pages; they can perform any other function, such as storing and fetching database information, or opening a socket to another machine. Building Servlets with JDeveloper 2.0 Oracle JDeveloper 2.0 has easy to use servlet code generating Wizards. For simple non database interactive servlets a basic HTTP servlet creation wizard is provided. For creating more sophisticated servlets which provide full query, insert, update delete access to your database a DBServlet wizard is also provided. Creating an HTTP Servlet You can use the Servlet Wizard to create an HTTP servlet. The servlet created by this wizard extends the JavaSoft HttpServlet class and returns HTML to the calling client browser. Using this wizard, you can choose to generate code skeletons for the servlet s methods, including service, doget, dopost, doput, and dodelete. With these skeletons as a starting point, you can add implementation code to define the behavior of the methods. You can also use the wizard to specify parameters that will be passed to the servlet at runtime. The process of creating an HTTP Servlet is as follows: 1. Set up for HTTP servlet development. 2. Define the servlet s properties and parameters. 3. Implement the servlet methods. The following topics describe this process in detail. Setting Up for HTTP Servlet Development JDeveloper provides everything you need to build and test HTTP servlets. But, to deploy your HTTP servlets, you need a Servlet runnable webserver. For example you may use the JavaSoft Java Web Server 1.1. Download and install this server from the JavaSoft Web Site: www.javasoft.com Defining the Servlet s Properties and Parameters The process of defining the servlet s properties and parameters consists of the following steps: 1. Launch the Servlet Wizard Paper 452 / Page 2
2. Define the servlet properties 3. Enter parameters passed to the servlet To launch the Servlet Wizard: Choose the File New menu to display the Object Gallery. From the New page in the Object Gallery, select the Servlet icon. To define the servlet properties: 1. Enter the servlet s package name, class name, and file path. 2. Specify the servlet s thread model. Check the SingleThreadModel checkbox or leave it unchecked for the multithreaded model. A web server can concurrently execute a servlet s service method in multiple threads. If a servlet s service method performs an action that is not thread-safe, you should select the SingleThreadModel. This model ensures that no two threads will execute a servlet s service methods concurrently. For example, if a service method updates a servlet s field, you should choose either the SingleThreadModel or implement the service method so that it is thread-safe by synchronizing client update requests. For more information on writing thread-safe code, please see JavaSoft s Java Tutorial. 3. Select which methods the servlet will use. Check either the service method, which processes user requests and responds back to the client, or one or more of the following: doget, which handles GET, conditional GET and HEAD requests dopost, which handles POST requests doput, which handles PUT requests dodelete, which handles DELETE requests 4. Click Next to proceed to the next wizard page, or Finish to create the servlet using the default values of the next wizard page. To enter the parameters to be passed to the servlet: Enter the parameters to be passed to the servlet. The wizard will generate the code needed for the servlet to read the parameter values at runtime. Click Add Parameter to open a new parameter entry line. Click Remove Parameter to remove an existing parameter. Now that the JDeveloper wizard has created the code for your servlet, you may customize it for your own needs or you can run it as is. To run your servlet simply add your compiled class file into your webserver s environment. Using Sun s Java WebServer this is a simple one step process which can be done via a browser. Creating a Database Servlet (DBServlet) Using the DBServlet Wizard, you can create a database servlet that generates a singlwe table or a master-detail HTML form to query, browse, edit, insert, and view database information from two tables or views. Paper 452 / Page 3
The process of creating a database servlet is as follows: 1. Define the servlet type and connect to a database. 2. Select database objects for the servlet. 3. Specify the join conditions for the servlet. 4. Select master (and detail) columns for the servlet. 5. Select a theme. 6. Finish the DBServlet Wizard. The following topics describe this process in detail. To Launch the DBServlet Wizard: 1. Choose the File New menu to display the Object Gallery. 2. From the New page in the Object Gallery, select the DBServlet icon. To define the servlet properties: Specify the kind of servlet to create. This defines the the way the data is represented in the HTML form generated by the servlet. 1. To create a master-detail servlet, click Master-Detail. To name the servlet and connect to a database: 1. Enter the class name, package name, and title of the servlet. 2. Enter the database connection information for the servlet. You can select an existing database connection or define a new connection by clicking the Add... button. If you choose an existing connection, you can modify it as needed by clicking the Edit... button. Clicking either the Add... or Edit... buttons, invokes the Connection Information editor. The database connection name you specify in this step is used by JDeveloper to name a special deployment XML file for the servlet. For example, a servlet that uses database connection MyDbConnection will have a corresponding XML file named MyDbConnection.xml. To create or modify a database connection: 1. Use the Connection Information editor if you are creating a new database connection, or modifying an existing one. From the Driver page, choose a database driver for the servlet. 2. Click the Database tab to proceed to the next page. 3. From the Database page, choose a database driver for the servlet. a. Enter the user name and password for the connection. b. Click the security information prompt if you want the end user to login at runtime. Paper 452 / Page 4
c. Specify the connection protocol to use. d. Enter other database information as required, such as Host ID, Socket ID, and Port. 4. Click the Test Connection tab to proceed to the next page. 5. From the Test Connection page, click Test Connection. 6. If the connection is successful, click OK. If not, go back and correct any connection information until the connection succeeds. Optionally, you can click Save Log... to make a log record of the connection. Clicking OK returns you to the Name the Servlet window. 7. Click Next to proceed to the next wizard page. Select Database Objects 1. Select the database objects (tables, views, or both) you want to use in the servlet. Filter the available choices by choosing either UserName Schema Objects or All Available Objects. Specify Join Conditions 1. Specify the join conditions to use for the master-detail relationship. You can select an existing join condition, or click Add... to define a new condition. Click Remove... to remove a selected join condition from the list. Click to Remove All... to remove all join conditions in the list. Select Master and Detail Columns 1. For the master database object in the servlet, select the columns you want to include. 3. For the detail database object in the servlet, select the columns you want to include. 4. Click Next to proceed to the next wizard page. Select a Theme 1. Select a theme for the look and feel of the HTML form generated dynamically by the servlet. Finish the DBServlet Wizard Verify the servlet information and click Finish. Paper 452 / Page 5
About the Predefined HTML Forms If you are creating a database servlet, you can use the following predefined HTML forms to present retrieved data to end users. Browse form Edit form Insert form Query (Find) form View form Conclusion By providing a set of easy to use wizards making the task of creating database interactive servlets incredibly easy, JDeveloper provides the Java programmer with the most efficient development environment for creating scalable, sophisticated servlet database applications. Paper 452 / Page 6