devxlogo

Stop Speculating, Start Building: A Step-by-step Guide to Building Web Services with WebLogic Workshop 8.1 and MySQL

Stop Speculating, Start Building: A Step-by-step Guide to Building Web Services with WebLogic Workshop 8.1 and MySQL

EA revolutionized the Java Web services world with WebLogic Workshop 7.x, which abstracted all the infrastructure details of the Web service and allowed the developer to simply develop his or her targeted logic. Now the next generation of this software, version 8.1, is in beta.

This 10-Minute Solution takes you through all the steps for building an application that serves content to a Web site from a MySQL database and a WebLogic Web service, beginning from a zero state through to a finished application.





How do I build a Java Web service using the latest BEA WebLogic Workshop product?



Build an application that serves content to a Web site from a MySQL database and a WebLogic Web service.Step 1. Download and Install WebLogic Workshop 8.1 Beta Software
Download the public beta of 8.1 and double click the file to launch it. Accept the defaults during installation. At the end of the installation, the wizard will give you the option to launch the WebLogic quick start to configure a domain. Accept this option, and when the screen asks which type of domain you want, select a workshop domain.

Create the domain and exit the quick start. Now launch WebLogic Workshop (WLW) by selecting its icon on the start menu. When WLW starts, you should see a screen like the one Figure 1 shows.

WebLogic Workshop 8.1
Figure 1: WebLogic Workshop 8.1

Near the center of the taskbar at the bottom of the screen, you’ll see a little red button labeled “Server Stopped.” This indicates that the WebLogic application server is not yet running.

To start the server, click Tools?>WebLogic Server?>Start WebLogic Server. The progress dialog will appear, as will a couple of DOS consoles. WLW starts the app server by issuing the following DOS command:

{BEA_HOME}samplesdomainsplatformstartWLS.cmd

After a few minutes, the application server will start. (Later, you will amend this .cmd file by adding classpaths to the JDBC drivers for MySQL.)

Once the server starts and runs successfully, close down Workshop, which shuts down the server. Should the server not start properly, uninstall WebLogic 8.1 and try again. Remember, this is a beta, so it may have some quirks.

On some machines, starting up the server may take some time. BEA has improved this drastically in versions of the beta that post-date the public beta that you downloaded.

Step 2. Download, Install, and Configure MySQL
You will download and install three products from MySQL.com for this Web service:

  • MySQL 4.X production server
  • MySQL Control Center (MySQLCC)
  • JDBC drivers for MySQL (Connector/J)
  • The following instructions apply to the Microsoft Windows versions of the software. The Linux installation and startup should be similar, but check the product documentation for further details.

    MySQL 4.X Production Server
    Download MySQL 4.0 production release, and extract the zip file to a new directory. Launch the file setup.exe to install it. Once the application is installed, reboot. If the server isn’t running (check your task list for mysqld-nt.exe), you can run it from c:mysqlin.

    MySQL Control Center (MySQLCC)
    Download MySQL Control Center (MySQLCC), extract the files, and run setup.exe. MySQL Control Center will install and place an icon on your desktop. Double click this icon to view the control center. On the left side of the screen you should see a list of MySQL servers. If the list is empty, right click the list and select “New.” A dialog will appear. Set the “Name” to “ContentManager” and the “Host” to “Localhost.” Click “Add” and you will be returned to the previous screen.

    You should now see “ContentManager” on the list with a red button indicating that it has stopped. Right click “ContentManager” and select “Connect.” The database will start and you will be connected to it. (In the next section, you will set up a table for the content manager.)

    JDBC Drivers for MySQL (Connector/J)
    Download Connect/J JDBC drivers for MySQL. The downloaded zip file will contain the JDBC drivers. Create a directory at c:mysqljar and unzip the files there.

    Step 3. Create the Database
    This solution builds a very simple database structure using the MySQLCC. You can easily expand upon the instructions for your own applications. If you have MySQLCC open and you are connected to the ContentManager database, open the ContentManager node to view the ‘Databases’, ‘Server Administration’, and ‘User Administration’ nodes.

    1. Open the ‘Databases’ nodes to view the database available on this server. It will probably be populated with ‘mysql’ and ‘test’ databases.
    2. Right click the ‘Databases’ node, and select ‘Create New Database.’
    3. Name the database ‘Content’ and click OK.
    4. Right click the new ‘Content’ database and select ‘Connect.’
    5. Open the node, and select ‘Tables.’
    6. Right click ‘Tables’ and select ‘New Table.’
    7. Add the following fields in the Table manager:
      FieldNameDataTypeSize
      contentIDInt 
      contentDateDateTime 
      contentHeadlineVarChar100
      contentTextlongText 
    8. Select the contentID field, and click the ‘key’ icon to make contentID the key field for the table. Also, make sure that AUTO_INCREMENT is checked, and make sure that contentID is added as an Index on the ‘Indexes’ tab.
    9. Click the floppy disk icon to save, and call the table ‘WebContents.’

    You can now enter data on your table. Double click the table to access the table editor. To edit contentText you need to double click the field to open the text editor, where you can edit and update the text by clicking Apply at the bottom right of the screen.

    Finally, you must give the root user a password. Double click the User administration node, and select root@localhost. Set the password to ‘root’ in the password box and click Apply. This step does not ensure a secure system. For a production system, you should set strong passwords for your database on a properly secured server.

    In some cases, your WebLogic installation may come with a MySQL driver already. Check your documentation about using it. For the purposes of this solution, however, you will step through the procedure for adding a driver for the times when the driver does not come as part of the installation.

    Step 4. Configure WebLogic Application Server for MySQL Access
    When a Web service is running within the WebLogic application server and it needs to use classes that are contained in third-party JAR files (like what we want to do with the MySQL classes hosted in the MySQL JAR file), the application server needs to be configured at start-up to have access to these JAR files.

    The following steps enable you to do this:

    1. Using Windows Explorer, navigate to the following directory (BEA_HOME is the installation directory, probably c:eaweblogic81b):
      {BEA_HOME}samplesdomainsplatform
    2. Right click the file startWebLogic.cmd and select ‘Edit.’
    3. Notepad will open the file. Find the line that reads:
      PRE_CLASSPATH=.;%POINTBASE_CLASSPATH%;%WLW_PRE_CLASSPATH%;
      %WLI_PRE_CLASSPATH%;%WLP_PRE_CLASSPATH%

      and amend it to read:

      PRE_CLASSPATH=.;C:mysqljarmysql-connector-java-3.0.7-stable
      mysql-connector-java-3.0.7-stable-bin.jar;%POINTBASE_CLASSPATH%;
      %WLW_PRE_CLASSPATH%;%WLI_PRE_CLASSPATH%;%WLP_PRE_CLASSPATH%

    Save the file. Launch WebLogic Workshop and start the WebLogic server as before.

    You have added the reference to the MySQL JAR file that you downloaded earlier. If you installed it to a different directory, make sure that the new entry in this file corresponds to the location of the mysql-connector JAR file on your hard drive.

    Now, start the WebLogic console by opening your browser and navigating to http://localhost:7001/console. If the server is running, you will get the WebLogic login prompt. Log in with the username ‘weblogic’ and the password ‘weblogic.’ These are the default settings you accepted when you installed the application. If you chose your own username and password instead of the defaults, enter them here.

    You can test whether the server recognizes your driver by doing the following:

    1. Look for the JDBC setting on the right-hand side and select the ‘Connection Pools’ link underneath it.
    2. On the next screen, select ‘Configure a new JDBC connection pool.’
    3. For database type. select ‘Other.’ If MySQL is available and you want to use the built-in driver, skip the rest of this section, but note the driver classname. You’ll need it later.
    4. Enter the following settings on the next screen:
      • Name: MySQL
      • Driver Classname: com.mysql.jdbc.Driver
      • URL: jdbc:mysql://localhost/content
      • Database User Name: root
      • Password: root
      • Confirm Password: root
    5. Click ‘Test Connection.’ You should see a message saying ‘Connection Successful’ at the top of the screen. If you don’t, please recheck all the previous steps to make sure you have set up the MySQL server correctly.

    Step 5. Create the Web Service
    The Web service will expose two methods. The first method allows users to enter data into the database. The second method retrieves the last five articles from the database, based on their date.

    To create the Web service, start up WebLogic Workshop and the application server as outlined in Step 1. Once it has loaded, do the following:

    1. Close the current application by issuing File?>Close Application.
    2. Create a new application with File?>New?>Application.
    3. On the application dialog, select a ‘Default Application’ and give it the name ‘Webcontents.’
    4. Click ‘Create,’ and click ‘Yes’ if it asks whether you want to create the directory.
    5. Create a new Web services project by selecting File?>New?>Project.
    6. In the ensuing dialog, select ‘Web Services’ on the list and ‘Web Service Project’ as the Project type. Name the project ‘WebContentWS.’
    7. Create the Web Service by selecting File?>New?>Web Service. Give it the name “WS.jws.”
    8. If you look at the WebContentsWS node in your application, you will now see ‘Helloworld.jws’ and ‘Ws.jws.’
    9. Open Ws.jws and the WebLogic Workshop designer will open onto your Web service.

    The WLW designer is a unique piece of software that allows you to visually design your Web service. This solution only scratches the surface of its features, but feel free to play with it and work through the Help file tutorials to see more.

    Step 6. Implement the Web Service
    Within the WLW designer, you can add your methods and design them. To do this, you must first understand the screen layout. Figure 2 shows the designer.

    The Web Service Designer
    Figure 2: The Web Service Designer

    The Web service is denoted by a box, which is divided into two areas. The light gray area on the left denotes the outward-facing interface of the Web service. The darker gray area on the right denotes the inward-facing interface of the Web service. If the Web service is calling other Web services further “in” the architecture, you will see links to them here.

    The methods that you will create in your Web service are fairly straightforward. They are:

    • getContents(): queries the contents of the database and returns the last five articles by date
    • setContents(): uploads new contents to the database

    To create these, follow these steps:

    1. Right click the light gray area on the left, and select ‘New Method.’ Call it getContents.
    2. Repeat the previous step for ‘setContents.’
    3. Right click the getContents link in the gray area, and select ‘Edit XML Maps.’ On the bottom window of the ensuing dialog, change the Java to read:
      public String getContents() 
    4. Right click the setContents link in the gray area, and select ‘Edit XML Maps.’ On the bottom window of the ensuing dialog, change the Java to read:
      public bool setContents(String contentDate, 
      String contentTitle, String contentText)
    5. Note how the XML changes to show the ingoing and outgoing schemas based on the Java language method maps.

      You now have a basic, boilerplate Web service, so the next step is to add the code that implements the methods.

      Step 7. Implement the Web Service Methods
      The final step is implementing the logic that runs the Web service. In this case, you have two methods: one to read from the database, the other to write to it. To implement the read method, amend the getContents() method to look like the one shown in Listing 1. On Designer, you can click ‘Source View’ to go straight to the source code.

      Listing 1 shows getContents() in action. MySQL uses the ‘LIMIT’ clause on a SQL query to limit the number of items. To get the five most recent, run a query that selects everything with the fields ordered by date in descending order and limit it to five records.

      The method runs this query and builds an XML payload from the returned values. Note that the addResultSetToXML function manages this payload.

      This application isn’t bound to any form of DOM document, and it builds the XML as a simple string. See the source code in the WLW download for this method.

      Listing 2 shows the setContents method and how it works. It is very straightforward. It takes the input parameters and uses them to build an INSERT query, which it runs against the database, inserting the values to the database. You may notice that it takes only two parameters: strHeadline and strStory. These correspond to the fields for contentTitle and contentText. So what happens to the other two fields?

      The index field, contentID, was set up as an automatically incrementing field when you set up the database. Hence, when you create a new record, the server updates its value. Simply insert a NULL value, and the server does the rest.

      The date field, contentDate, is a little more interesting. You want it to have the date on which the field was created. You do this by creating a new instance of the java.util.Date class, which defaults to the current date and time. You then create a java.sql.Date instance from this instance, which when converted to a string using the toString method, returns a string in the ‘YYYY-MM-DD’ format that MySQL uses.

      Step 8. Testing the Web Service
      Compile and run the Web service. The WebLogic test harness will execute and should look like Figure 3.

      The Web Service Running Within the BEA Test Harness
      Figure 3: The Web Service Running Within the BEA Test Harness

      To add a story to the Web contents database, simply enter the headline and the story to the text boxes and click setContents. After running this, you can check the MySQL database using MySQLCC to see if the values have been added to the database.

      Clicking getContents will return an XML file that contains the five most recent stories by date. Figure 4 shows how the test harness represents the returned data.

      The Test Harness Return Values for getContents()
      Figure 4: The Test Harness Return Values for getContents()

      On this screen, you can see two important things: the values that are returned from the getContents method, and the structure of the SOAP response if getContents is invoked.

      Step 9. Next Steps
      The logical next steps are writing an application or another Web service that consumes this Web service. Another option is making a user-friendly interface to input the data or a Web site that is driven by the data in the database. To do this, use the WSDL to create a proxy that is local to the consumer. The WSDL that a WebLogic 8.1 Web service generates is fully standard. Microsoft Visual Studio.Net and other Java development environments such as Oracle JDeveloper can easily and transparently consume it. The WSDL and a link to the WSDL are available on the ‘Overview’ tab in the test harness.

      You’ve Only Scratched the Surface
      By following this solution, you have gone from zero state to having a fully functional Web service that runs on a properly configured app server and reads and writes data from a database. Although this solution has shown how simple and powerful the WebLogic Workshop 8.1 truly is, it only scratched the surface. The product has countless other features and complexities that you can investigate, from controls to page flows to workflows and asynchronous services.

    devxblackblue

    About Our Editorial Process

    At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

    See our full editorial policy.

    About Our Journalist