Professional Documents
Culture Documents
Write A Program To Implement The Operation That Can Receive A Request and Will
Write A Program To Implement The Operation That Can Receive A Request and Will
AIM: Write a program to implement the operation that can receive a request and will
return a response in two ways
i) One-way operation
ii) Request-response operation
2. Task To Be Done:
In this experiment we have to create two different projects in one project we will create
a web service and use database connection for storing and fetching data. It will contains
code for background implementation of two method namely getData() and insertData().
In second project the actual implementation of user-interface is created along with URL
of WSDL file of method which are created in first project.
3. Implementation code
First Project Snippet
package com.jitu;
import javax.annotation.Resource;
import javax.sql.DataSource;/**
@WebService(serviceName = "myws")
public class myws {
@Resource(name = "myconn")
private DataSource myconn;
/**
* Web service operation
* @param d
* @return
*/
@WebMethod(operationName = "getName")
public String getName(@WebParam(name = "d") int d) {
//TODO write your implementation code here:
try {
Connection con = myconn.getConnection();
PreparedStatement pr = con.prepareStatement("Select * from friend where id=?");
pr.setInt(1,d);
ResultSet rs = pr.executeQuery();
if(rs.next()){
return rs.getString(2);
}
else
{
return "no record found";
}
} catch (SQLException ex) {
return "error";
}
/**
* Web service operation
* @param d
* @param name
*/
@WebMethod(operationName = "insertName")
@Oneway
public void insertName(@WebParam(name = "d") int d, @WebParam(name = "name")
String name) {
try {
Connection con = myconn.getConnection();
PreparedStatement pr = con.prepareStatement("insert into friend(id,firstname)
values(?,?)");
pr.setInt(1,d);
pr.setString(2,name);
pr.executeUpdate();
out.println("success");
} catch (SQLException ex) {
Logger.getLogger(myws.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
ii. insertData.jsp
DATABASE TABLE:
OUTPUT:
RESULT: