36 WEB SERVICES -II
Dr S. Abirami
Learning Objectives
In the previous module, we have studied about the basics of webservice, components of web service, architecture of web services and applications of web services. In this module, we are going to learn how to create a new web service and how to deploy it using NETBEANS IDE.Also, consumption of web services in different modes is going to be learnt.
- WEB SERVICE CREATION
Here, we are going to see how to create a web service in Java using NetBeans IDE using GlassFish 4.0 specifications.
1.1 Creating a Web Service in Java using NetBeans IDE
The step-by-step instructions to create and deploy a web service in Java using NetBeans
IDE and GlassFish 4.0 are as follows:
Prerequistites for creating the web service is the requirement of NetBeans 8.0 IDE which can be downloaded from https://netbeans.org/downloads/ – Link.
Step 1:To create a Java based web service, open NetBeans IDE and click on New Project and choose Java Web -> Web Application as shown in Figure 1. Enter the Project Name as CalculationWS, use the default settings and then click on “Finish”. Now the server required for the web service need to be selected and the server chosen here Glass Fish server with Java EE 7 Web version as shown in Figure 2.Now a new Project has been created using these settings.
Figure 1. Creation of a Web Application
Step 2: Create a Web Service
Once the project has been created, now we are supposed to create a web service. For that, go to the Project Tree Structure displayed on the left side of the window. Right click on the project and select “New” and then choose “Web Service” as shown in Figure 3.
Figure 3. Creation of Web service
Specify web service name as “CalWS” and package name as “CalculationWS” as indicated in Figure 4. Click on “Finish” to create a new web service.
Figure 4. Naming of new Web Service
Once the web service creation is over, the next step is to create a method over it which can act on the web service created. Once you open the CalWS in the editor, you will get a skeleton code. Inside the skeleton code, you can enter the code as per your requirement. According to this project, in CalWS.java file, the original hello() function has been replaced with the following code:
@WebMethod(operationName = “Add”)
public String Add(@WebParam(name = “value1”) String value1,@WebParam(name = “value2”) String value2 ) {
float value=Float.valueOf(value1)+Float.valueOf(value2); return (Float.toString(value));
}
Here, @Web method corresponds to the method of web service, @WebParam corrersponds to the parameters passed to the method with two string arguments. In this code, value of two floating point numbers has been added and a string equivalent of that value has been returned. Now the web service method has been created.
Step 3: Deploy and Test Web Service by choosing Projects->CalculationWS->Deploy. Right click on the project and select “Deploy” as shown in Figure 5.
This is to deploy all the web services in this project. Once the web service has been deployed, you will get a message that, “web service deployed successfully”. To test the web service, right click on the service and select “Test Web Service”. Again, Right click on the project and select “clean and build”, a war file will be automatically generated under the “dist” directory as shown in Figure 6.
Once tested, tester page will be displayed as shown in Figure 7, type two numbers in the tester page and deploy it using glassfish server. The sum of the two numbers will be displayed as shown in the figure 8.
2. CONSUMING THE WEB SERVICE
After the creation of web service, some client applications need to get developed inorder to consume the web services. The web service can be consumed by creating a client application using anyone of the following ways:
- Java Class in Java SE Application
- Servlet in Web Application
- JSP Page in Web Application
2.1 Java Class in Java SE Application
- A Java class in a Java SE application can be created to consume a web service by following the given steps:
- Choose File > New Project (Ctrl-Shift-N on Linux and Windows).
- Select Java Application from the Java category. Name the project as “CalculatorWS_Client_Application”. Create Main Class selected and accept all other default settings.
- Click Finish to create the client application.
- Once the client application has been created, Right-click
- the CalculatorWS_Client_Application node and choose New > Web Service Client. The New Web Service Client wizard opens.
- Select Project as the WSDL source. Click Browse. Browse to the CalWS web service from the CalculationWS in the CalculatorWSApplication project as shown in the figure 9.
- When you have selected the web service, click OK. The following dialog box shown in Figure 10 gets opened, where you don’t select a package name and leave that field empty. Leave the other settings at default and click Finish.
- Ø The projects window displays the new web service client, with a link node under web service references for the addition method that you have created using the web service . This is shown in Figure 11.
- Double-click the main class so that it opens in the Source Editor. Drag the add node below the main() method as shown in Figure 12.
Alternatively, instead of dragging the add node, you can right-click in the editor and then choose Insert Code > Call Web Service Operation.
public static void main (String args[])
{
private static string addition(float i, float j)
{
org.me.Calculation.CalWS_service = new org.me.Calculation.CalWS_service(); Org.me.Calculation.CalWSport = service.getCalWSport(); return port.add(i,j);
}
}
In the main() method body, replace the TODO comment with code that initializes values for i and j, calls add(), and prints the result.
public static void main(String[] args)
{
int i = 3;
int j = 4;
result = add(i, j);
System.out.println(“Result = ” + result);
}
The Output window now shows the sum:
Compile: run: Result = 7
BUILD SUCCESSFUL (total time: 1 second)
2.2 JSP Page in Web Application
The web service can be consumed by creating a JSP page which is done by following the given steps:
- Choose File -> New Project (Ctrl+Shift+N on Linux and Windows).
- Select Web Application from the Java Web category. Name the project “CalculatorWSJSPClient”. Clicknext and then click Finish.
- Expand the web page node under the project node and delete index.html.
- Right click on the web pages node and choose new->JSP in the popup menu.
- If JSP is not available in the popup menu. Choose new->Other and select JSP in the web category of the new file wizard.
- Type index for the name of the JSP file in the new file wizard and click finish.
- Right click the CAlculatorWSJSPClient node and choose new -> web service client.
- Select project as the WSDL source. Click browse and browse the CalWS web service in the CalculatorWSApplication project.
- After selecting the web service, Click OK.
- In the web service reference node, expand the node represents the web service.
- The add operation that has to be invoked by the client is now exposed.
- Drag the add operation to the client’s index.jsp page and drop it below the H1 tags.
- The code for invoking the service’s operation is now generated in the index.jsp as shown below:
try { org.me.calculation.CalWS_Service service = new
org.me.calculation.CalWS_Service();
org.me.calculation.CalWS port = service.getCalWSPort();
int i=0;
int j=0;
int result= port.add(i, j);
out.println(“Result=” +result);
}
catch{Exception ex}
{//TODO handle custom exceptions here}
The values for i and j are changed from zero to other integer, such as 3 and 4.Replace the comment section of the TODO in the exception handling catch block with
“out.println (“exception” +ex);”
When the above code is executed, the output will be displayed as follows in figure 13.
Summary
In this module, we have learnt about the creation and deployment of web services. Following that, we have learnt about the various ways in which a web service can be consumed by different types of applications such as Java and JSP.
Web Links
- https://www.tutorialspoint.com/webservices/what_are_web_services.htm
- http://www.java4s.com/web-services/
- http://javapapers.com/web-service/restful-services-http-basic-authentication/
- https://www.cl.cam.ac.uk/~ib249/teaching/Lecture1.handout.pdf
- https://www.w3schools.com/xml/xml_services.asp
- https://www.javatpoint.com/web-services-tutorial
- https://www.cs.colorado.edu/~kena/classes/7818/f08/lectures/lecture_4_netbeans_presenta.pdf
- http://vhost3.cs.rit.edu/project_web_page/webservices.html
- http://cs.ulb.ac.be/public/teaching/infoh511
- https://www.fas.harvard.edu/~cscie259/distribution/lectures/1/lecture1.pdf
- http://www.java2blog.com/2013/03/web-service-tutorial.html
- http://www2.imm.dtu.dk/courses/02267/slides/weekp08.pdf
- David Chappell and Tyler Jewell, “Java Web Services”, First Edition, O’Reilly, 2002
- Paul J. Deitel, Harvey Deitel, “Java How to Program”, Ninth Edition, Pearson,2012.