You are on page 1of 5

1.

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 static java.lang.System.out;


import javax.jws.Oneway;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

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);
}
}
}

Second Project Code:


i. getData.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<%-- start web service invocation --%><hr/>


<%
try {
com.dd.Myws_Service service = new com.dd.Myws_Service();
com.dd.Myws port = service.getMywsPort();
// TODO initialize WS operation arguments here
int d = Integer.parseInt(request.getParameter("text3"));
// TODO process result here
java.lang.String result = port.getName(d);
out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
<%-- end web service invocation --%><hr/>
</body>
</html>

ii. insertData.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<%-- start web service invocation --%><hr/>


<%
int id=Integer.parseInt(request.getParameter("text1"));
String fname=request.getParameter("text2");
try {
com.dd.Myws_Service service = new com.dd.Myws_Service();
com.dd.Myws port = service.getMywsPort();
// TODO initialize WS operation arguments here
int d = id;
String name = fname;
port.insertName(d, name);
out.println("success");
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
<%-- end web service invocation --%><hr/>
</body>
</html>
iii. index.html

DATABASE TABLE:

OUTPUT:
RESULT:

You might also like