Name: Aman Tripathi
Roll no: 5490.
Class : TYCS
Subject: Web Services (RJSUCS504).
Web Services
Practical No.1
Aim:-Create a SOAP web service to find factorial of a number and consume
using any java client.
Description:- To
create server side
Step1:New project > Java Web > Web Application
Step2:Web Page > New > Web Service
To insert code right click and
To deploy the service first
Then
To run the service
To create Client Side Server New Project > Java > Java Application
To get the WSDL File
localhost:31321/factorial/fact?WSDL
Drag and drop the file in code
Source Code:-
Source code of client side:- package
aabc; import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "fact")
public class fact {
@WebMethod(operationName = "facto")
public int facto(@WebParam(name = "a") int a) {
int fact=1; for(int i=1;i<=a;i++){
fact*=i;
} return
fact;
}
}
Source code of client side:-
package clientws; public
class Clientws {
public static void main(String[] args) {
System.out.println("factorial of the number is"+facto(5));
}
private static int facto(int a) {
aabc.Fact_Service service = new aabc.Fact_Service();
aabc.Fact port = service.getFactPort();
return port.facto(a);
}
}
Output:-
Output of server side:-
Output of client side:-
Practical No.2
Aim:-Create SOAP webservices in java to perform an operation and consume
using .NET client.
Description:-
1. We need two applications(software) for this Practical i.e.,
NetBeans and Visual Studio 2019. First create a web application in
NetBeans and add web services to it.
2. After that click on “insert code” and click “Add web services
operations” and add the parameters, write the code for the program
and save it.
3. Now deploy and then test it.
4. Copy the path of the WSDL file.
5. Now go to VS 2019 and create a web form and add the elements on
it.
6. Now add Service Reference to it and paste the path there and create
it.
7. Now write the code for the Button to check the number is even or
odd.
Output:-
Practical No.3
Aim:-Create SOAP webservices in java to perform an operation and consume
using PHP client.
Description:-
To save PHP file >Disk C:>wamp>www>new folder >save
Input of web service:- package abc;
import javax.jws.WebService; import
javax.jws.WebMethod; import
javax.jws.WebParam;
@WebService(serviceName = "client")
public class client {
@WebMethod(operationName = "addition")
public int addition(@WebParam(name = "a") int a, @WebParam(name = "b") int
b) {
return (a+b);
}
}
Input of php:- <?php
$client = new SoapClient("http://localhost:31159/prc3/client?WSDL");
$param->a = 40; $param->b
= 50;
print_r($client->addition($param));
?>
Output:-
Practical No.4
Aim:-Create RESTful Service From Pattern To Return Current Date And
Time.
Description:-
Step1:-Create a new web application >right click on it>new>RESTful Web
Service from Patterns…
Step2:-After writing the code in “GenericResources.java”>clean and build
Step3:- after clean and build > deploy
Step4:-After deploy to print the output > RESTful Web Service
>GenericResource>HTTP Methods>getText():String>right click>Test Resources
Uri
Source Code for plain/text:-
package abc; import
javax.ws.rs.core.Context; import
javax.ws.rs.core.UriInfo; import
javax.ws.rs.PathParam; import
javax.ws.rs.Produces; import
javax.ws.rs.Consumes; import
javax.ws.rs.GET; import
javax.ws.rs.Path; import
javax.ws.rs.PUT; import
java.text.SimpleDateFormat;
import java.util.Date;
@Path("generic") public class
GenericResource {
@Context private UriInfo
context; public
GenericResource() {
}
@GET
@Produces("text/plain")
public String getText() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date=new Date();
String now = sdf.format(date);
return now;
}
@PUT
@Consumes("text/plain")
public void putText(String content) {
}
}
Source Code for html:-
package abc; import
javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import
javax.ws.rs.Path; import
javax.ws.rs.PUT;
@Path("generic")
public class GenericResource {
@Context private
UriInfo context;
public GenericResource() {
}
@GET
@Produces("text/html")
public String getHtml() {
return("<html><head></head><body><h1>Current Date And Time</h1><p
id=\"p1\"></p><script>var date = new Date();
document.getElementById(\"p1\\\").innerHTML =
date;</script></body></html>");
}
@PUT
@Consumes("text/html")
public void putHtml(String content) {
}
}
Output of plain/text:-
Output of html:-
Practical No.5
Aim:-Create RESTful service from Database and return data in JSON format.
Description:-
Step1:-Create a Java Web Application >Right click >New RESTful Web Service
From DataBase>Follow step same as practical 6 >After connecting Database
Step2:-After all steps > go to > Source Package >
abc.services(databasename.servics) >Open it
Step3:-Remove the “application/xml” part
Step4:-Run the program
Step5:-Copy the link of the program
Step6:-Paste the link in the HTML file
Source Code:-
<!DOCTYPE html>
<html>
<head>
<title>Data from Database</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 2px solid black; /* Border for table cells */
padding: 8px; text-align: left;
}
th {
background-color: #f2f2f2; /* Optional: Background color for table headers
*/
}
</style>
<script>
var req = new XMLHttpRequest();
req.open("GET", "http://localhost:31159/practical5/webresources/abc.emp",
true);
req.onload = function() { if (req.status
>= 200 && req.status < 300) { var data =
JSON.parse(this.response); var t =
document.getElementById("table"); for
(var i = 0; i < data.length; i++) { var r =
t.insertRow(); var c1 = r.insertCell(0);
var c2 = r.insertCell(1);
c1.innerHTML = data[i].empid;
c2.innerHTML = data[i].empname;
}
} else {
console.error("Failed to load data:", req.statusText);
}
};
req.onerror = function() {
console.error("Request error"); };
req.send();
</script>
</head>
<body>
<table id="table">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</table>
</body>
</html>
Output:-
Practical No.6
Aim:-Create RESTful services to perform CRUD operation.
Description:-
Step1:-First start the Java DB server > then create a database
Step2:-Give a name to the database and keep the username and password of the
database the same.
Step3:-Go to your database and expand it go to the “Table” option>right click
create table
Step4:-Fill all the data you need
Step5:-After creating a Web Application>right click>new > Entity Classes From
Database
Step6:-Connect the database to the page
Step7:-Add the table
Step8:-new > JSF pages from entity classes
Step9:- new > RESTful web services from database
Step9:- run the program
Output:-
Practical No.8
Aim:-MVC Application.
Description:-
Step1:-Create a new new project in visual studio
Step2:-Select ASP.NET Application C#
Select MVC Pannel
Untick the the “Configure for HTTPS” option
Step3:-Go to server explorer > Data Connections > Create New SQL Server
Database
Add the PC name to connect the server > Give Database name
Expand the database > tables > add new table >create the table
Step4:-Solution Explorer > Model>add>new item
Go to data section > ADO.NET Entity Data Model
Select EF Designer from database
Tick the selected part in the below img
Select the table option
Step5:-Solution Explorer > Delete the existing controller > add > controller
Select MVC5 controller with read/write actions
After writing the code select the function right click > view
Select the details then Add
Source Code:- using
System;
using System.Collections.Generic;
using System.Linq; using
System.Web; using
System.Web.Mvc; using
MVCDemo.Models;
using MVCDemo.Controllers;
namespace MVCDemo.Controllers
{
public class EmpController : Controller
{
empdbEntities ed = new empdbEntities();
// GET: Emp
public ActionResult Index()
{
ed = new empdbEntities();
return View(ed.Tables.ToList());
}
// GET: Emp/Details/5
public ActionResult Details(int id)
{
ed = new empdbEntities();
return View(ed.Tables.Find(id));
}
// GET: Emp/Create
public ActionResult Create()
{
return View();
}
// POST: Emp/Create
[HttpPost]
public ActionResult Create(Table e1)
{
try
{
// TODO: Add insert logic here
ed = new empdbEntities();
ed.Tables.Add(e1);
ed.SaveChanges(); return
RedirectToAction("Index");
}
catch {
return View();
}
}
// GET: Emp/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Emp/Edit/5
[HttpPost]
public ActionResult Edit(int id, Table e1)
{
try
{
// TODO: Add update logic here
e1 = ed.Tables.Find(id);
ed.Entry(e1).State = System.Data.Entity.EntityState.Modified;
ed.SaveChanges();
return RedirectToAction("Index");
}
catch {
return View();
}
}
// GET: Emp/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Emp/Delete/5
[HttpPost]
public ActionResult Delete(int id, Table e1)
{
try
{
// TODO: Add delete logic here
e1 = ed.Tables.Find(id);
ed.Tables.Remove(e1);
ed.SaveChanges();
return RedirectToAction("Index");
} catch
{ return
View();
}
}
}
}
Output:-
Practical No.9
Aim:-RESTful services using WEB-API. Description:-
Create a new new project in visual studio
Select ASP.NET Application C#
Select WEB-API Panel
Step3:-Go to server explorer > Data Connections > Create New SQL Server
Database
Add the PC name to connect the server > Give Database name
Expand the database > tables > add new table >create the table
Step4:-Solution Explorer > Model>add>new item
Go to data section > ADO.NET Entity Data Model
Select EF Designer from database
Select the table option
Step5:-Solution Explorer > Delete the existing controller > add > controller
Select Web API 2 Controller with read/write actions
Source Code:- using
System;
using System.Collections.Generic;
using System.Linq; using
System.Net; using
System.Net.Http; using
System.Web.Http;
using WEBAPI.Models;
namespace WEBAPI.Controllers
{
public class EmpController : ApiController
{
empdbEntities db = new empdbEntities();
// GET: api/Emp
public IEnumerable<Table> Get()
{
return db.Tables.ToList();
}
// GET: api/Emp/5
public Table Get(int id)
{
return db.Tables.Find(id);
}
// POST: api/Emp
public String Post(Table t)
{
t = new Table();
db.Tables.Add(t);
db.SaveChanges();
return "Resource Created Successfully";
}
// PUT: api/Emp/5
public String Put(int id, Table t)
{
t = db.Tables.Find(id);
db.Entry(t).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return "Updated Successfully";
}
// DELETE: api/Emp/5
public String Delete(int id)
{
Table t = db.Tables.Find(id);
db.Tables.Remove(t); db.SaveChanges();
return "Deleted Successfully";
}
}
}
Output:-
Add api/tablename to get output
Practical No.11
Aim:-Create RESTful services from pattern to demonstrate the VSE of
QueryParam
Description:-
Create Web Application > right click >new > RESTful Web Service From Pattern
After writing the code Test > Restful web service > GenericResource
Source Code:- package abc;
import
javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import
javax.ws.rs.Path; import
javax.ws.rs.PUT; import
javax.ws.rs.QueryParam;
@Path("generic")
public class GenericResource {
@Context private
UriInfo context;
public GenericResource() {
}
@GET
@Produces("text/plain")
public String getText() {
// throw new UnsupportedOperationException();
return "hello";
}
@GET
@Produces("text/plain")
@Path("Ex")
public String getCollege(@QueryParam("name") String name) {
return "My college name is"+name;
}
@PUT
@Consumes("text/plain")
public void putText(String content) {
}
}
Output:-
By changing the url we can get the output