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
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.
|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.
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.
|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.