Advertisment

Java-based Web Services 

author-image
PCQ Bureau
New Update

In the first article of this series, Web Services on PCQLinux 2004 (page 76, PCQuest, April 2004), we had set up a package called Axis and deployed a simple Java code as a Web service. In this article, we will see how to deploy a compiled Java code as a Web service. This becomes useful if you have access to compiled Java classes and not the source code. For this, we will compile and use the same source code (AWebService.java) that we wrote last month. You can find a copy of this source code in this month's PCQ Essential CD. Compile the code as:






javac AWebService.java







This will produce a file called AWebService.class. Copy this file to /opt/tomcat/webapps/axis/WEB-INF/classes directory. Next, to deploy compiled code as a Web service we need to go through a new step-writing a WSDD (Web Service Deployment Descriptor) file. 

The WSDD file



The syntax of the file is simple and is as follows:




























With the name attribute of the service tag we christen our Web service as MyWebService (say). You must note this name as we will use it while coding the client for this Web service. In the next line, we specify the class name of the main class of our compiled code. With the * in the line:





We declare that we want to expose all the public methods of our compiled class as a Web service. You can also be selective and specify a comma separated list of the name of the public methods. We must remind you that only public classes and public methods within a public class can be deployed as a Web service. 

Save the above lines in a file called deploy.wsdd and copy this file to the directory

/opt/tomcat/webapps/ axis/WEB-INF/classes. 

Deploy the compiled code



Change to the above directory and issue the following command.






java -cp $AXISCLASSPATH org.apache.axis.client.AdminClient -l http://127.0.0.1:8080/axis/services deploy.wsdd







We deploy compiled code and Web applications (as you will see later) using a Java app called AdminClient (org.apache.axis.client.AdminClient) that comes with the Axis packages. We had discussed about the $AXISCLASSPATH environment variable in the last month's article. With the -l option you specify the URL under which the Web service is deployed. Restart Tomcat as:





/etc/rc.d/init.d/tomcat stop


/etc/rc.d/init.d/tomacat start





A Java Web app converted into a Web service

Client for the compiled code



The client for our compiled code will be the same as that in the last month's article, but with a change in the URL or the endpoint. As we specified with the -l option above, the Web service is now deployed under the URL http://127.0.0.1:8080/axis/services. We also need to suffix the name of the Web Service (MyWebService) that we had specified in the deploy.wsdd file. Hence, as from the last month's client code, we only need to change the line that specifies to URL as:






String endpoint =

http://127.0.0.1:8080/axis/services/MyWebService”;







You can find a copy of the client code in the Linux section on this month's PCQ Essential CD. We compile and run the client in the same way as mentioned in the last article,
ie,:






javac -classpath $AXISCLASSPATH WebServiceClient.java


java -cp $AXISCLASSPATH:. WebServiceClient








You should be greeted by the same display as last month but this time the client code is communicating with our compiled Java code.




Web apps as Web service



How about if we have an existing Web app and we want to deploy its processing/business logic as a Web service? In the Linux section on this month's PCQ Xtreme CD we have given a webapp that checks a MySQL database to see whether a domain name is blocked or not. 

Copy the directory blocked_domains to /opt/tomcat/webapps directory. The processing logic of the Web app is contained in a Java class CheckDomain found in the directory blocked_domains/WEB-INF/classes/cd. The source code of CheckDomain can also be found in the same directory for your reference. Note that the isBlocked( ) method returns true if the domain is blocked and returns false otherwise. The CheckDomain class is specified to be in a package called cd. The Web app also bundles the MySQL JDBC drivers in the directory blocked_domains/WEB-INF/lib which is required to connect to the MYSQL database. Next, we set up the MySQL database for this Web app. Before we proceed, we assume that you have a running MySQL installation on the PCQLinux 2004 machine.

On the PCQ Essential CD, in the Linux section, you will find a file called setupdb.sql. Change to this directory and issue the following command to use the script:






mysql -u root -p < setupdb.sql







The script will create a table named domains, which stores the names of the domains that are blocked. It inserts foo.com in the table which means foo.com is a blocked domain. It also creates a user called pcq with a password as secret who has access to the blocked_domains database. 


Direct Hit!
Applies to: Java developers
USP: To deploy various forms of Java apps as Web services
Code on CD:

system/cdrom/software/web_service

To get a taste of the Web application key in the URL http://127.0.0.1:8080/blocked_domains in the browser. Type foo.com in the text box and click the button. You should get a “The domain foo.com is blocked” message. 

The two essential steps to convert the Web application to a Web service are:



Copy all the .jar file found in the directory webapps/axis/WEB-INF/lib to webapps/blocked_domains/WEB-INF/lib


Second, copy the servlet definitions in the web.xml file found in webapps/axis/WEB-INF to the web.xml file in the directory webapps/blocked_doamins/WEB-INF. Note that you have to copy the contents between the tags. Once done, the web.xml for blocked_domains should look as follows:









xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance



xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd”


version=”2.4”>





AxisServlet


Apache-Axis Servlet





org.apache.axis.transport.http.AxisServlet


















AdminServlet


Axis Admin Servlet





org.apache.axis.transport.http.AdminServlet








100













SOAPMonitorService


SOAPMonitorService





org.apache.axis.monitor.SOAPMonitorService




















SOAPMonitorPort

5001






100








AxisServlet


/servlet/AxisServlet












AxisServlet


*.jws












AxisServlet


/services/*












SOAPMonitorService


/SOAPMonitor















Next, create a WSDD file with the following content.



xmlns:java=”http://xml.apache.org/axis/wsdd/providers/java”>




















Note that the className's value has the package name prefixed. Save the contents in a file deploy.wsdd and copy the file to

webapps/blocked_domains/WEB-INF/classes/cd. 

Next, issue the following command:





java -cp $AXISCLASSPATH org.apache.axis.client.AdminClient -l http://127.0.0.1:8080/blocked_domains/services deploy.wsdd






Note the URL specified with the -l option. This time we are deploying the Web service at the URL of the blocked_domain Web application (instead of Axis's). Now the isBlocked( ) method is exposed as a web service. Coming to the client to invoke the webservice, we need to make the following changes in the client code.





Modify the endpoint as:





String endpoint = “http://127.0.0.1:8080/blocked_domains/services/CheckDomain”;







Specfiy the name of the method:





call.setOperationName(new Qname(“isBlocked”));







Call the service: 





Boolean result = (Boolean)call.invoke(new

Object<>{“foo.com”});







Note that, since the isBlocked( ) methods returns a boolean value (true or false) we have cast it to a Boolean object (as opposed to String object for earlier client code). Also note we have supplied foo.com as the parameter. 


Compile and run the client as before. You should get an output as 'Result: true'. 













Shekhar Govindarajan



IT4Enterprise







Advertisment