Professional Documents
Culture Documents
Aim: Write a program to implement to create a simple web service that converts the temperature
from Fahrenheit to Celsius and vice a versa.
Open Netbeans 8/8.2 --- file --- new project --- java web (left) --- web application(right)
package mypack;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "tempservice")
@WebMethod(operationName = "hello")
@WebMethod(operationName = "F_to_C")
return ((val-32)*5/9);
@WebMethod(operationName = "C_to_F")
Step 4 run
URL http://localhost:8080/tempwebservice/tempWebService?WSDL
//TempClient.java
package tempclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Double t;
int ch;
ch=Integer.parseInt(br.readLine());
System.out.println("Enter temperature");
t=Double.parseDouble(br.readLine());
if(ch==1)
System.out.println("temperature in fahrenheit="+cToF(t));
else
System.out.println("Temperature in celcius="+fToC(t));
return port.cToF(val);
return port.fToC(val);
O/P:
1. Celcius to Fahreheit
2. Fahrenheit to celcius
Enter your Choice
1
Enter temperature
25
temperature in fahrenheit=77.0
Practical-02
Aim: Write a program to implement the operation can receive request and will return a response
in two wayspr. a) One - Way operation b) Request –Response Factorial program
@WebMethod(operationName ="factorial")
double f=1;
int i;
for(i=1;i<=num;i++){
f= f*i;
return f;
Then right click and on the project and select “New webservice Client” option and paste the WSDL url
link
Explore Web services References->and drag drop method which you want to call
import java.util.Scanner;
System.out.println(factorial(num));
Result
Practical-03
Aim: Write a program to create a web service to perform ceaser cipher encryption.
First create a new project→ Web project
Right click on project→ New→ Web Service
package com.abc;
import java.util.Random;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "CCipherService")
public class CCipherService {
@WebMethod(operationName="encrypt")
String ct="";
for(int i=0;i<msg.length();i++){
ct+=(char)(msg.charAt(i)+rd.nextInt(26));
return ct;
}
Then clean and build the project→ deploy→ Right Click on CCipherService in Web Services
and Click on Test Web Service
Now, Create a java application. It’s purpose would be to create the java client for the above web
service.
Now, Right Click on the Client application→ New→ Web Service Client and paste the WSDL URL of
the web service which was previously deployed and tested
Now, drag and drop the encrypt method from Web Service References into
the CcipherClient.java and use the below code
CCipherClient.java
package ccipherclient;
import java.util.Scanner;
System.out.println("Enter message");
String msg=sc.next();
System.out.println("Encrypted message: "+encrypt(msg));
return port.encrypt(name);
}
Clean and build CcipherClient→ Run the project
O/P:
Enter message
abc
Parameter num
Type double
Go to source page and Now type the code below in Factorialwebservice.java file
@WebMethod(operationName ="factorial")
double f=1;
int i;
for(i=1;i<=num;i++){
f= f*i;
}
return f;
http://localhost:14188/FactorialWebservice/Factorialwebservice?WSDL
4:- clean and build , and deploy the webservice created in the java and Test the WebService
http://localhost:14188/FactorialWebservice/Factorialwebservice?WSDL
5:- Now RightClick on Progect name and add (Service Reference in Client app) and paste the WSDL url
6:- Now doubleclick the button and then type the following cod in the aspx.cs file
ServiceReference1.FactorialwebserviceClient client =
new ServiceReference1.FactorialwebserviceClient();
Open Netbeans 8/8.2 --- file --- new project --- java web (left) --- web application(right)
package mypack;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "calcservice")
@WebMethod(operationName = "hello")
@WebMethod(operationName = "Addition")
return a+b;
@WebMethod(operationName = "Subtract")
public Integer Subtract(@WebParam(name = "a") int a, @WebParam(name = "b") int b) {
return a-b;
Step 4 run
Go to file – new—project
Give name as calcclient
Right click on calcclient –new --- web service client and paste the URL copied above
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="index.jsp">
Enter n1 <input type="text" name="n1">
Enter n2<input type="text" name="n2">
<input type="submit" value="submit">
</form>
</body>
</html>
Repeat the same steps for subtract place cursor below hello world and select
Run index,html
Practical-06
Aim: Write a JAX-WS web service for finding Armstrong no. Define a Servlet / JSP that consumes
the web service.
File->New->Project
Right click on project and select Web servic
Right click on code and selet Add Web Service operation to add method in web services
Right click on project name and deploy the webservices
servlet.java. //index.html
<html>
<head>
<title>Armstrong number</title>
<meta charset="UTF-8">
<body>
</form>
</body>
</html>
//Armstrong.java
package com.abc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;
@WebServiceRef(wsdlLocation =
"WEB-INF/wsdl/localhost_8080/ServletService/ServletService.w
sdl")
response.setContentType("text/html;charset=UTF-
8"); try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Armstrong</title>");
out.println("</head>");
out.println("<body>");
int n=Integer.parseInt(request.getParameter("t1"));
out.println(armstrong(n));
out.println("</body>");
out.println("</html>");
@Override
processRequest(request, response);
@Override
@Override
// Note that the injected javax.xml.ws.Service reference as well as port objects are not thread
safe.
/ If the calling of port operations may lead to race condition some synchronization is
required. com.abc.ServletService port = service.getServletServicePort();
return port.armstrong(n);
Enter number:
153
153 is armstrong
Practical-07
Aim: Define a web service method that returns the contents of a database in a XML string.
Go to the services tab and create a database by using either javaDB or Mysql.if creating by using
mysql then first register mysql server by right clicking on Database.
• Right click on project ->new select RestFul Web services from database.
Note: The name of JNDI and user should be same eg. Here its abc.
Deploy restful web services by right clicking on project name.
Practical-08
Aim: Define a RESTful web service that accepts the details to be stored in a database and performs
CRUD operation.
Go to the services tab and create a database by using either javaDB or Mysql.if creating by using
mysql then first register mysql server by right clicking on Database.
Steps
Write Code
create database db1;
use db1;
File –new—project
Right click on project name ie jsonproject—add—new item
Drag and Drop Emp in the center
Right click on project name ie jsonproject—add—new item—select webservice
Emp1.asmx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
namespace jsonproject
{
/// <summary>
/// Summary description for Emp1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment
the following line.
// [System.Web.Script.Services.ScriptService]
public class Emp1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetEmplyees(int Id)
{
var json = "";
var name = from result in mydc.Emps
where result.Id==Id
select result;
JavaScriptSerializer jss = new JavaScriptSerializer();
json = jss.Serialize(name);
return json;
}
}
}
WCF service
MathService.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MathService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class
name "MathService" in code, svc and config file together.
public class MathService : IMathService
{
public Int32 add(Int32 n1, Int32 n2)
{
return n1 + n2;
}
IMathService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MathService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
interface name "IMathService" in both code and config file together.
[ServiceContract]
public interface IMathService
{
[OperationContract]
Int32 add(Int32 n1, Int32 n2);
[OperationContract]
Int32 subtract(Int32 n1, Int32 n2);
[OperationContract]
Int32 multiply(Int32 n1, Int32 n2);
[OperationContract]
Int32 divide(Int32 n1, Int32 n2);
}
}
6)Test the service (click run)
3 label
WCF Client
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MathServiceTestApp1.MathService1;
namespace MathServiceTestApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
if (comboBox1.Text == "add")
{
textBox3.Text = loclient.add(num1, num2).ToString();
}
{
textBox3.Text = loclient.divide(num1, num2).ToString();
}
}
}
}
if (comboBox1.Text == "add")
{
textBox3.Text = loclient.add(num1, num2).ToString();
}
{
textBox3.Text = loclient.divide(num1,
num2).ToString();
}
18)output is as follows
Practical 11
Use WCF to create a basic ASP.NET Asynchronous JavaScript and XML(AJAX) service
Go to File->New->WebSite
Select ASP.NET Empty WebSite
set path
write GreetingService (which is the name of ajax enabled web services )
Step 7:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="sm1" runat="server">
<Services>
<asp:ServiceReference Path="~/GreetingService.svc" />
</Services>
</asp:ScriptManager>
<input type="button" value="greet" onclick="showgreeting()" />
</div>
</form>
</body>
</html>
function showgreeting()
{
GreetingService.Greeting(onSuccess);
}
function onSuccess(response)
{
alert(response);
}
</script>
Step 9: run by right click on default.aspx in solution explorer ---select view in browser
Practical 12
Aim: Demonstrate using the binding attribute of an endpoint element in WCF
(Program for WCF TCP-Binding)
namespace SampleCalcSvc
{
[ServiceContract]
public interface ICalcService
{
[OperationContract]
int GetSum(int a, int b);
}
}
namespace SampleCalcSvc
{
public class CalcService : ICalcService
{
public int GetSum(int a, int b)
{
return a + b;
}
}
}
9)right click on SampleCalcSvc (from solution explorer)------build
Finish
Give name as SampleCalcSvcNetTcpEndpoint
Address –mex
Binding –mexTcpBinding
IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFConsole
NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name
"IService1" in both code and config file together.
[ServiceContract]
[OperationContract]
[OperationContract]
}
Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFConsole
/ NOTE: You can use the "Rename" command on the "Refactor" menu to change the
class name "Service1" in code, svc and config file together.
/ NOTE: In order to launch WCF Test Client for testing this service, please
select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
{
return (s * s);
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleWCFApp
class Program
}
8)Run the project.
O/P
Practical-14
Aim: Develop a WCF and consume by using MVC application.
Delete the IService1.cs and Service1.svc files and then Right click on project -> Add WCF
Service
Select IService1.cs and type in the [Operation Contract]
IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfGcd
NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name
"IService1" in both code and config file together.
[ServiceContract]
[OperationContract]
}
Service1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfGcd
NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name
"Service1" in code, svc and config file together.
NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or
Service1.svc.cs at the Solution Explorer and start debugging.
int r;
while (n2 != 0)
r = n1 % n2;
n1 = n2;
n2 = r;
return n1;
Select and Run the Service1.cs and test the Web Service. Also, copy the WSDL url for Service
Reference.
Now, add the Service Reference by Right Clicking the Project-> Add Service Reference
Paste the WSDL url which we copied earlier.
Now, go in Controllers folder of project and type in the HomeController.aspx
HomeController.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace GCDMVC3.Controllers
{
return View();
{
ViewBag.Message = "Your app description page.";
return View();
}
return View();
}
}
}
After this, open the Views folder and type in the Index.aspx. Add the line in any of the
ContentID tags
Index.aspx