Saturday 17 September 2011

CREATION OF A WEB SERVICE WITH DATABASE CONNECTIVITY


CREATION OF A WEB SERVICE WITH DATABASE CONNECTIVITY

Aim:
To create a web service for adding few numbers using NetBeans and write client side code to invoke the web service.

Algorithm:
Using the Netbeans API create a project of the type web application.
Establish connectivity with the database along with the necessary authentication parameters.
Create a web service in the project.
Click on the Design tab and design the prototype of the web service.
Click on source tab and modify the application logic of the web service.
Save the project.
Right click on the project and click on deploy and undeploy.
Then test the web service.
Create another web application project and create a jsp file.
Right click on project and click on create web service client.
Browse and choose the web service created i.e wsdl url
Drag and drop the web service reference to the source code window.
Then pass the appropriate parameters to the web service client and invoke the web service.

STEPS FOR CREATING TRAVEL WEB SERVICE
Requirements:
->Mysql
->netbeans with glass fishv2 server
I.Before creating project in net beans do the following:
1.install Mysql setup file and the gui tools .
2.Then open the mysql browser in the start->programs->mysql->mysql browser
Then restore the back up(newproject2011…) as follows
1.choose  tools->mysql administrator
2.Then choose restore in that window..Then click open backup in the bottom…open the backup file(newproject201….) which is in the attachment….then click start restore..
3.now the  schema is created in the mysql…..
Now strat the coding in netbeans…

SERVER SIDE:
1.create the server project as before and with the following changes.
Project name: KPNBusTravels
2.web service name:KPNBusTravels
Package name:kpnpack
3.in design window
Operation name:getBus
Parameters:
Source and destination with the type as String.
4.Add the following code in source
package kpnpack;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
 *
* @author user
*/
@WebService()
public class KPNBusTravelsWS {
/**
* Web service operation
*/
@WebMethod(operationName = "getBus")
public String getBus(@WebParam(name = "source")
String source, @WebParam(name = "destination")
String destination) {
//TODO write your implementation code here:
ArrayList<String> service_name = new ArrayList<String>();
ArrayList<String> departure_time = new ArrayList<String>();
ArrayList<String> arrival_time = new ArrayList<String>();
ArrayList<String> bus_fare = new ArrayList<String>();
ArrayList<String> bus_type = new ArrayList<String>();
ArrayList<String> bus_category=new ArrayList<String>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/qos","root","root");
Statement st1=con1.createStatement();
st1.executeUpdate("insert into qostable(webname, exetime,price,compute,availtime) values('"+wn+"','"+et+"','"+pr+"','"+ct+"','"+at+"')");
Statement st=con.createStatement();
ResultSet rs = st.executeQuery(sql);
int i=0;
while (rs.next()) {
departure_time.add(rs.getString(3));
arrival_time.add(rs.getString(4));
bus_fare.add(rs.getString(5));
bus_type.add(rs.getString(9));
bus_category.add(rs.getString(11));
service_name.add(rs.getString(12));
//  service_name.add(rs.getString("Servicename"));
}
} catch ( Exception e) {
e.printStackTrace();
}
String concatBus = service_name.toString()+"&"+departure_time.toString()+"&"+arrival_time.toString() +"&"+bus_fare.toString() +"&"+
bus_type.toString() +"&"+bus_category.toString() +",";
return concatBus;
}
}
5.Then deploy the service and test the web service as before.
Give input as Chennai and Coimbatore .then click getbus.

CLIENT SIDE:

1.create new project
Name:client1
2.As before in addition program create web service reference.
3.The source code for index.jsp and action,jsp are as follow.
Index.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Hello World!</h2>
<form name="" action="actionn.jsp" method="post">
Enter Source:<input  name="source" type="text" /><br/>
Enter Destination:<input  name="destination" type="text" /><br/>
<input  name="ok" type="submit" value="" />
</form>
</body>
</html>
Action.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Hello World!</h2>
<%
String a1=request.getParameter("source");
String b1=request.getParameter("destination");
%>
<%-- start web service invocation --%><hr/>
<%-- end web service invocation --%><hr/>
<%-- start web service invocation --%><hr/>
<%
String[] res,commasplit=null,lbrsplit=null,rbrsplit=null;
String[][] flight_trans=new String[100][100];
int row=0,col=0;
int trow=0,tcol=0;
int midrow=0,midcol=0;
// Jagson
String[] jagres,jagcommasplit=null,jaglbrsplit=null,jagrbrsplit=null;
String[][] jagflight_trans=new String[100][100];
int jagrow=0,jagcol=0;
//Air India
String[] airres,aircommasplit=null;
String[][] airflight_trans=new String[100][100];
int airrow=0,aircol=0;
try {
kpnpack.KPNBusTravelsWSService service = new kpnpack.KPNBusTravelsWSService();
kpnpack.KPNBusTravelsWS port = service.getKPNBusTravelsWSPort();
// TODO initialize WS operation arguments here
java.lang.String source = "a1";
java.lang.String destination = "b1";
// TODO process result here
java.lang.String result = port.getBus(source, destination);
out.println("Result = "+result);
res=result.split("&");
System.out.println("Jet Split:"+res);
for(int i=0;i<res.length;i++)
{
int j=0;
row=res.length;
String lbr=res[i].replace("[", "");
String rbr=lbr.replace("]", "");
commasplit=rbr.split(",");
for( j=0;j<commasplit.length;j++)
{
col=commasplit.length;
System.out.println("flight_trans[i][j]"+i+"/"+j);
if(commasplit[j]!=null)
flight_trans[j][i]=commasplit[j];
}
// if( flight_trans[i][j]==null)
// col=0;
}
// out.println("Res = "+res[0]);
System.out.println("TR");
System.out.println("jetRow:"+row);
System.out.println("jetColumn:"+col);
for(int r=0;r<row;r++)
{
for(int c=0;c<col;c++)
{
if(flight_trans[r][c]!=null)
System.out.print("tr:"+flight_trans[r][c]);
}
System.out.println();
}
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
<%-- end web service invocation --%><hr/>
</body>
</html>
4.Then finally undeploy and deploy the client and run it

0 comments:

Post a Comment