The actual procedures used to develop the application are described below.
1) Creating the Web Service Project
5) Creating the Web Service Client
6) Checking the Application Operation
7) Distributing the Application to the Operating Environment
1) Creating the Web Service Project
Select [File] > [New] > [Project] from the menu bar. [New Project] is displayed.
Select [Web] > [Dynamic Web Project] from the [New Project] wizard, then click [Next].
Check and enter the following setup items:
Setup Items | Setup Content |
---|---|
Project name | WebServiceSample |
Target Runtime | Interstage Application Server V11.1 IJServer Cluster (Java EE) |
Dynamic Web Module version | 2.5 |
Configuration | Default Configuration for Interstage Application Server V11.1 IJServer Cluster (Java EE) |
Add project to an EAR | Do not select. |
Set the information, then click [Next].The [Web Module] page is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
Context Root | WebServiceSample |
Content Directory | WebContent |
Java Source Directory | src |
Generate deployment descriptor | Check. |
2) Creating the Web Service
2-1) Creating the Data Class
Hold the country name and total population information, and create a data class that gets information from there. To create the data class, select the created project, then right-click to display the context menu. Select [New] > [Class] from the context menu. The [New Java Class] wizard is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
Source folder | WebServiceSample/src |
Package | sample |
Name | CountryData |
Constructors from superclass | Check |
The following source file is generated:
Source file | Description |
---|---|
CountryData.java | Data class |
Implement the processing for holding the country names and total populations. Add the places shown in red below to the source.
Data Class Implementation (CountryData.java)
package sample; public class CountryData { private String countryName; private int totalPopulation; public CountryData() { // TODO Auto-generated constructor stub } public CountryData(String name, int total) { setCountryName(name); setTotalPopulation(total); } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public int getTotalPopulation() { return totalPopulation; } public void setTotalPopulation(int totalPopulation) { this.totalPopulation = totalPopulation; } } |
Point
After the fields are added, and while the class is in the selected state, [Source] > [Generate Getters and Setters] can be selected from the menu to add getter/setter.
2-2) Creating the Web Service Pattern
The Web service is performed from creating a Java class. Select the created project, then right-click to display the context menu. Select [New] > [Class] from the context menu. Creation of a special Java class is not required. Specify the source folder, package, and name to create the class.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
Source folder | WebServiceSample/src |
Package | sample |
Name | PopulationRanking |
2-3) Declaring the WebService Annotation
In the created Java class, define the WebService annotation as shown below. The parts in red are the WebService annotation definition.
WebService Annotation Definition
package sample; import javax.jws.WebService; @WebService public class PopulationRanking { } |
2-4) Implementing the Web Service
Implement the method of the function you want to publish as a Web service. Add the places shown in red below to the source.
Implementation of the Method published as the Web Service
package sample; import javax.jws.WebService; @WebService public class PopulationRanking { private CountryData[] countries; public PopulationRanking(){ countries = new CountryData[]{ new CountryData("China",1330000000), new CountryData("India",1140000000), new CountryData("The United States",300000000), new CountryData("Indonesia",230000000), new CountryData("Brazil",190000000), new CountryData("Pakistan",160000000), new CountryData("Bangladesh",150000000), new CountryData("Russia",140000000), new CountryData("Nigeria",140000000), new CountryData("Japan",130000000) }; } public CountryData getCountryData(int rank){ --rank; if(9 < rank || rank < 0 ){ return null; } return countries[rank]; } } |
3) Creating the EAR Project
Select [File] > [New] > [Project] from the menu bar. The [New Project] is displayed.
Select [Java EE] > [Enterprise Application Project] from the New Project wizard, then click [Next].
Check and enter the following setup items:
Setup Items | Setup Content |
---|---|
Project name | WebServiceEARSample |
Target Runtime | Interstage Application Server V11.1 IJServer Cluster (Java EE) |
EAR version | 5.0 |
Configuration | Default Configuration for Interstage Application Server V11.1 IJServer Cluster (Java EE) |
Set the information, then click [Next].The [Enterprise Application] page is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
Java EE Module Dependencies | WebServiceSample |
Content Directory | EarContent |
Generate deployment descriptor | Not Checked |
4) Preparing to Get the WSDL
4-1) Associating the EAR Project and Server
Select the server in the Servers view, then select [Add and Remove Projects] from the context menu.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
Configured projects | WebServiceEARSample |
4-2) Deploying to the Server
Select the server in the Servers view, then select [Start] from the context menu.
The EAR file is automatically deployed when the server starts.
Check the following information in the Servers view.
Items to Check | Contents to Check |
---|---|
Server state | Started |
Server status | Synchronized |
Status of the EAR file (WebServiceEARSample) | Synchronized |
Status of the WAR file under the EAR file (WebServiceSample) | Synchronized |
4-3) Checking the URL for the WSDL
Use the Interstage Java EE Admin Console to check the URL of the WSDL.
In the Servers view, select a server, then from the context menu, select [Admin Console] to start the Interstage Java EE Admin Console.
For details on how to use the Interstage Java EE Admin Console, separately check the details on how to use the Interstage Application Server Java EE execution environment.
From the tree on the left side of the Interstage Java EE Admin Console, click [Web Service] > [Web Service Class Name (PopulationRanking)]. Next, click the [View] of [WSDL] button displayed on the right side of the window. The WSDL created using the Interstage Application Server is displayed.
Check the URL of the displayed WSDL. Use this URL when creating the client.
URL of the WSDL
http://localhost/WebServiceSample/PopulationRankingService?wsdl |
5) Creating the Web Service Client
Conventionally, procedures to create a Web application as a client are needed, but with this product a sample is imported and the important points for a Web service client are checked. If you want to know the detailed procedure for creating a Web application, refer to "2.2 Introduction" in the Web application chapter.
Use the following procedure to import a sample.
Select [File] > [Import] from the menu. Select [General] > [Existing Projects into Workspace] from the displayed Import wizard. Select [Select archive file], then click the [Browse] button and select the following files:
<Workbench installation folder>\sample\WebServiceSample.zip
Select [WebServiceClientSample] from [Project], then click [Finish].
After the WebServiceClientSample project has been imported, select the WebServiceEARSample project. Right-click to display the context menu and select [Properties]. Select [Java EE Module Dependencies] from the Properties dialog box. Select WebServiceClientSample.war and add the client to the EAR.
5-1) Creating the Client Project
Create a dynamic Web project as a Web service client. Since this can be completed by importing a sample, details are omitted. Refer to the following table for the settings.
Setup Items | Setup Content |
---|---|
Project name | WebServiceClientSample |
Target Runtime | Interstage Application Server V11.1 IJServer Cluster (Java EE) |
Dynamic Web Module version | 2.5 |
Configuration | Default Configuration for Interstage Application Server V11.1 IJServer Cluster (Java EE) |
Add project to an EAR | Check |
EAR Project Name | WebServiceEARSample |
5-2) Creating the Service Endpoint Interface from the WSDL
The Service Endpoint Interface and other files required to invoke the Web service must be created from the WSDL. These are created by the imported sample project as shown below.
Select [Web Services] > [Web Service(JAX-WS)] from the [New] wizard. The [New Web Service(JAX-WS)] wizard creates the files. Set the source folder for importing files and the URL of the WSDL file fetched from the Interstage Application Server in Step 4).
If deploying a Web service as an EAR, the package of the service endpoint interface must be modified.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
Source folder | WebServiceClientSample/src |
WSDL file name | Set the URL fetched from the Interstage Application Server. |
It changes from the package for which the package of the source generated automatically is specified in WSDL | Check |
Package | stub |
Check that the following files that are in the file name list have been created under the source folder.
File Name | Notes |
---|---|
PopulationRanking.java | Service Endpoint Interface |
PopulationRankingService.java | Service interface |
CountryData.java | Data definition class |
GetCountryData.java | Data binding class |
GetCountryDataResponse.java | Data binding class |
ObjectFactory.java | Object generation class |
package-info.java | Package information class |
Point
In order to refer the WSDL definitions and so on, the WSDL file specified in the Web service (JAX-WS) wizard and XML Schema file referred by WSDL file are automatically obtained from the reference destination, and copied to "wsdl" folder under the project.
5-3) Creating the Servlet Class
Create the servlet class. Creation is completed simply by setting the following setup items in the imported sample project.
Setup Items | Setup Content |
---|---|
Java package | sample |
Class name | ServletController |
Superclass | javax.servlet.http.HttpServlet |
Which method stubs would you like to create? | Constructor from the superclass |
5-4) Creating the I/O Page
Create the I/O page JSP. Files like the following are already created in the imported sample project.
Input Page (default.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>World ranking for total population</title> </head> <body> <h1>Input page</h1> <p> Enter a ranking from 1 to 10.</p> <p></p> <form action="ServletController" method="post"> Rank:<br> <input name="rank" type="text" size="10">Rank<br> <p></p> <input type="submit" value="OK"> <input type="reset" value="Clear"> </form> </body> </html> |
Output Page (result.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>World ranking for total population</title> </head> <body> <h1>Output page</h1> Total population ranking ${param.rank} rank is "${applicationScope.ranking.countryName}".<br> Total population is ${applicationScope.ranking.totalPopulation} people. <br> <hr> <form action="ServletController" method="post"> <input type="submit" value="Back to input page"> <input type="hidden" name="mode" value="top"> </form> </body> </html> |
Error Page (error.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>World ranking for total population</title> </head> <body> <h1>Error page</h1> The entered ranking is not in the specified range, or a server error occurred.<br> <br> <hr> <form action="ServletController" method="post"> <input type="submit" value="Back to input page"> <input type="hidden" name="mode" value="top"> </form> </body> </html> |
5-5) Specifying the Dependency Injection
The implementation for invoking the Web service is performed in the servlet class. This is already implemented in the servlet source of the imported sample project.
Create a business interface field, set the following WebServiceRef annotation, and set the Dependency Injection specifications.
WebServiceRef Annotation Definition
@WebServiceRef(name="service/PopulationRanking") private PopulationRankingService service; |
Point
Instead of the lookup method used up to J2EE1.4, Dependency Injection is used in Java EE and can be used to fetch and use an object. (During execution, the object is automatically set in a field by means of a Java EE container.)
5-6) Implementing the Web service Invocation Processing
To invoke the Web service, fetch the object to the Service Endpoint Interface as shown below, then invoke the method via the interface. The Web service invocation processing is defined as shown below in the created servlet class. This is already implemented in the servlet source of the imported sample project.
Web Service Invocation Method Definition
PopulationRanking business = service.getPopulationRankingPort(); CountryData country = business.getCountryData(rank); |
The servlet class in which Web service invocation is implemented is as shown below. (The imported project sample source is in this format).
The parts in red are the added and changed places.
Servlet Class (ServletController.java)
package sample; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; |
6) Checking the Application Operation
6-1) Associating the Project with a Server
No action is required because this has already been performed during preparations to create the client.
6-2) Setting Breakpoints
Set a breakpoint at the first line of the getCountryData() method of the Web service class. To set or delete a breakpoint, double-click the ruler part on the left edge of the editor.
6-3) Launching the Server
Select a server in the Servers view, then select [Debug] from the context menu.
Point
If you want to simply launch the application without using the debugger, select [Start] from the context menu.
If the deployment destination server in the Servers view is not registered, then it must be added. For details on how to do that, refer to "6.2.6 Checking Application Behavior".
The EAR file is deployed automatically before the server starts.
Check the following information in the Servers view:
Items to Check | Contents to Check |
---|---|
Server state | Started |
Server status | Synchronized |
EAR file (WebServiceEARSample) status | Synchronized |
Status of the WAR file (WebServiceSample) under the EAR file | Synchronized |
Status of the WAR file (WebServiceClientSample) under the EAR file | Synchronized |
6-4) Executing the Application
In the Servers view, select the deploying Web project (WebServiceClientSample), then from the context menu, select [Web browser]. The Web browser is started, and the input window is opened.
Setup Items | Setup Content |
---|---|
URL of input screen | http://localhost/WebServiceClientSample/ |
Enter a population ranking in the [Rank:] input field, then click [OK].
Point
The user can select the project, then select [Run As] > [Run on Server] or [Debug As] > [Debug on Server] from the context menu to add the server, add the project, launch the server, and launch the client all together.
In addition, the Web browser used as the client can be changed by selecting [Window] > [Web Browser] from the menu.
6-5) Debugging the Application
The applications runs and is interrupted at the breakpoints. Check the Variables view display. Select [Run] > [Step Over] from the menu and check the program state in the Variables view.
For debug details, refer to "6.2.6.1 Debugging".
Point
In addition to checking values, values can be changed in the Variables view.
Processing that has been interrupted by the debugger can be restarted by selecting [Run] > [Resume] from the menu. This allows the processing on the server side to complete so that the output page is displayed on the Web browser. Check that the country name and total population for the entered ranking is displayed.
7) Distributing the Application to the Operating Environment
An EAR file must be created in order to deploy the application to the operating environment. The created EAR file uses the server deployment function to deploy the application to the server.
7-1) Exporting the Application
The EAR file is created from the Export wizard. To launch the Export wizard, select [File] > [Export]. From [Export] wizard, select [Java EE] > [EAR file].
The [EAR Export] wizard is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
EAR project | Specify the enterprise application project. |
Destination | Specify the creation destination for the EAR file. |
7-2) Distributing to the Operating Environment
The application can be deployed from a remote environment to the operating environment by logging in to the Interstage Java EE Admin Console at the operating environment and by specifying a local EAR file.