![]() |
![]() |
e-docs > Tuxedo > Using Jolt with WebLogic Server > Simple Servlet Example |
Using Jolt with WebLogic Server
|
Simple Servlet Example
This example demonstrates how to use BEA Jolt to connect to BEA Tuxedo from a WebLogic servlet. It uses the WebLogic Server to deliver an HTML FORM front end in a standard Web browser.
Text entered by a user into the FORM is sent back to the WebLogic Server via the HTTP POST method that is serviced by a registered WebLogic HTTP Servlet, which calls a Tuxedo service using BEA Jolt. The text received by the servlet is sent to a Tuxedo service, where it is transposed to uppercase before being returned to the servlet. The form is compiled into a dynamically-generated HTML page by the servlet, then sent back to the Web browser, where the uppercase version of the original text is displayed.
This topic includes the following sections:
Example Components and Prerequisites
There are two parts to the simpapp example for Jolt for WebLogic Server:
The source code for the Jolt servlet simpapp example is located in the /samples/jolt/wls/servlet/ directory in the Tuxedo distribution.
The simpapp sample directory contains the following files:
A complete listing of the Tuxedo server-side source code of the simpapp application service is located in $TUXDIR/samples/atmi/simpapp on UNIX systems and in %TUXDIR%\samples\atmi\simpapp on Windows 2000 systems (where TUXDIR is the Tuxedo home directory).
To run this example, you should be familiar with:
Using the Example
The simpapp example is easy to follow. Just launch the simpapp.html page from the WebLogic Server. The simpapp.html page loads an HTML form which contains a text field for entering the string. Type in a string and click the Post button to submit the string as a post request. The SimpAppServlet formats the string you typed for use with the Jolt for WebLogic class libraries, and then dispatches the request to the Tuxedo TOUPPER service, which transposes the string to uppercase and returns it for display in the browser.
Configuring the simpapp servlet example requires the following steps:
Step 1. Perform Preparatory Steps
The simpapp example directory has a simpapp.rep file that contains the TOUPPER service definition. Your system administrator should use the Jolt Repository BulkLoader to add this service definition to the existing Jolt Repository on the Tuxedo server. The Jolt Repository BulkLoader package is supplied with the Jolt distribution for Tuxedo. Refer to Using BEA Jolt for details on how to install this.
On the Tuxedo server, the following code example uses the Jolt BulkLoader to add the TOUPPER service definition:
$ java bea.joltadm.jbld //host:port simpapp.rep
where host and port are the hostname and port number of your Jolt Server Listener (JSL), and the simpapp.rep is the BulkLoader file provided by BEA Jolt, located in one of the following locations:
$TUXDIR/samples/jolt/wls/servlet/ on UNIX
%TUXDIR%\samples\jolt\wls\servlet\ on Windows 2000
Step 2. Start the WebLogic Server
If you are using a Windows 2000 system, you can start the WebLogic Server from the Start menu. Otherwise, use the startWebLogic script on the command line, in the root directory of the WebLogic Server distribution.
For more information on starting the WebLogic Server, see "Starting and Stopping the WebLogic Server" in the BEA WebLogic Server Administration Guide.
Step 3. Configure the Servlet in WebLogic Server
Configuration of the Jolt connection pool and startup class MBeans for WebLogic Server 6.0 or later is done through Administration Console.
Step 4. Stop and Restart the WebLogic Server
In order to start the Jolt session pool, you must shut down the WebLogic Server, and then restart it. For more information on restarting the WebLogic Server, see "Starting and Stopping the WebLogic Server" in the BEA WebLogic Server Administration Guide.
Step 5. Compile the Servlet
After restarting the WebLogic Server, compile the SimpAppServlet file, as follows:
javac -d %WL.HOME%\config\mydomain\applications\simpapp\WEB-INF\classes SimpAppServlet.java
This step also copies the necessary java classes into a WEB-INF\classes directory.
Step 6. Display the simpapp.html Form
http://localhost:port/simpapp/simpapp.html
If you have problems displaying the form, be sure that the simpapp.html file is in the WebLogic document root.
Step 7. Post the FORM Data from the Browser
Enter some text into the text field on the HTML page and submit it by clicking the POST button. Along with the text you entered, other parameters are submitted to the simpapp servlet class running in WebLogic Server.
The following is the relevant section from the simpapp.html file that describes the HTML form:
<form name="simpapp" action="simpapp" method="post">
<input type="hidden" name="SVCNAME" value="TOUPPER">
<table bgcolor=#dddddd border=1>
<tr>
<td>Type some text here and click the Post button:
<input type="text" name="string">
</td></tr>
<tr>
<td align=center><input type="submit" value="Post!">
</td></tr>
</table>
</form>
This HTML form specifies two input fields: the text you enter and a hidden field. In this example, the value of the hidden field actually specifies the name of the Tuxedo service to be invoked. Although putting the name of the Tuxedo service within the HTML page is flexible and efficient, it is not recommended for production use for security reasons. In this HTML page, you can submit an HTTP request specifying a different service name as the hidden field.
Note: Tuxedo service names are case-sensitive.
When the WebLogic Server receives the HTTP form request, it invokes the doPost() method of the simpapp servlet and passes the form data into an HttpServletRequest.
Step 8. Process the Request
Before the first request to the simpapp servlet, WebLogic initializes the servlet by calling its init() method. The Jolt session pool is established in the following manner:
ServletSessionPoolManager b_mgr =
(ServletSessionPoolManager).SessionPoolManager.poolmanager;
Next, the servlet's doPost() method is executed. This method contains the code to get a connection from the simpapp session pool that was created during the startup of the WebLogic Server. The following code snippet shows the code that is used to retrieve the simpapp session pool.
// Get the "simpapp" session pool
ServletSessionPool session =
(ServletSessionPool) b_mgr.getSessionPool("simpapp");
The Tuxedo service that will be called is identified in a hidden field, which is retrievable from the request object. Retrieve the service name parameter as follows:
String svcnm[] = req.getParameterValues("SVCNAME");
You retrieve the value of the SVCNAME field in a string array that contains a single value; use only the first element of the array. The value set for the SVCNAME hidden field in the form is TOUPPER. This is the name of the Tuxedo service that the servlet invokes, which is passed to the call() method as follows:
// Invoke a service and get the result.
result = session.call(svcnm[0], req);
The session object in this example is a ServletSessionPool that can accept the HttpServletRequest object directly. Internally, it converts the data into a Jolt DataSet object, which contains the parameters for the TOUPPER service.
Note: The TOUPPER service expects a case-sensitive parameter called "STRING", so it is essential for the text field within the HTML form to be labeled exactly the same, that is, "STRING". Note also that the other data fields, such as the SVCNAME, are not relevant as parameters but don't disrupt the Tuxedo service.
The form parameter is used to actually name the service, which you don't have to pass as a service parameter. It is passed automatically because it is already contained in the HttpServletRequest object.
The TOUPPER service converts the text in the "STRING" parameter to uppercase text and passes it back to the servlet in a ServletResult object that contains the results of an executed call, as well as details about exceptions if any are thrown during the service call.
Step 9. Return the Results to the Client
The final step constructs and sends an HTML page, which contains the results of the service call, back to the client through the HttpResponse output stream. The uppercase result is retrieved from the ServletResult object using the result.getValue() method.
The following is a simple example of passing this data back as HTML that the browser can display:
out.println("<p><center><font size=+1><b>"+
result.getValue("STRING", "")+
"</b></font></center><p><hr
width=80%>");
The output stream produces a page similar to the one shown in Figure B-2:
Figure B-2 Output Stream Results Example
![]() |
![]() |
![]() |
![]() |
||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |