You are on page 1of 50

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

1. Implementing Hello Service using RMI(to be executed at command prompt).


Hello.java
import java.rmi.*;
public interface Hello extends Remote
{
public String sayHello() throws RemoteException;
}
HelloImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class HelloImpl extends UnicastRemoteObject implements Hello
{
public HelloImpl() throws RemoteException
{}
public String sayHello() throws RemoteException
{
return "Hello";
}
}
HelloServer.java
import java.rmi.*;
public class HelloServer
{
public static void main(String args[]) throws Exception
{
HelloImpl hello=new HelloImpl();
Naming.bind("hello",hello);
System.out.println("RMI OBJECT BOUND TO REGISTRY");
}
}
HelloClient.java
import java.rmi.*;
public class HelloClient
{
public static void main(String args[]) throws Exception
{
Remote r=Naming.lookup("hello");
Hello hello=(Hello)r;
System.out.println(hello.sayHello());
}
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT :

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

2. Implementing Hello Service to send name from client using RMI.


Hello.java
import java.rmi.*;
public interface Hello extends Remote
{
public String sayHello(String a) throws RemoteException;
}
HelloImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class HelloImpl extends UnicastRemoteObject implements Hello
{
public HelloImpl() throws RemoteException
{}
public String sayHello(String a) throws RemoteException
{
return "Hello"+a;
}
}
HelloServer.java
import java.rmi.*;
public class HelloServer
{
public static void main(String args[]) throws Exception
{
HelloImpl hello=new HelloImpl();
Naming.bind("hello",hello);
System.out.println("RMI OBJECT BOUND TO REGISTRY");
}
}
HelloClient.java
import java.rmi.*;
public class HelloClient
{
public static void main(String args[]) throws Exception
{
Remote r=Naming.lookup("hello");
Hello hello=(Hello)r;
System.out.println("Enter Name:");
Scanner s = new Scanner(System.in);
String a = s.nextLine();
System.out.println(hello.sayHello(a));
}}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

3. Implementing Calculator Service using RMI.


Calc.java
import java.rmi.*;
public interface Calc extends Remote
{
public int addition(int a, int b) throws RemoteException;
public int subtraction(int a, int b) throws RemoteException;
public int multiplication(int a, int b) throws RemoteException;
public int division(int a, int b) throws RemoteException;
}
CalcImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class CalcImpl extends UnicastRemoteObject implements Calc
{
public CalcImpl() throws RemoteException
{}
public int addition(int a, int b) throws RemoteException
{
return a+b;
}
public int subtraction(int a, int b) throws RemoteException
{
return a-b;
}
public int multiplication(int a, int b) throws RemoteException
{
return a*b;
}
public int division(int a, int b) throws RemoteException
{
return a/b;
}
}
CalcServer.java
import java.rmi.*;
public class CalcServer
{
public static void main(String args[]) throws Exception
{
CalcImpl cal=new CalcImpl();
Naming.bind("hello",cal);
System.out.println("RMI OBJECT BOUND TO REGISTRY");
}}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

CalcClient.java
import java.rmi.*;
public class CalcClient
{
public static void main(String args[]) throws Exception
{
Remote r=Naming.lookup("hello");
Calc cal=(Calc)r;
System.out.println("Enter value of a:");
Scanner s = new Scanner(System.in);
int a = Integer.parseInt(s.nextLine());
System.out.println("Enter value of b:");
int b = Integer.parseInt(s.nextLine());
System.out.println(cal.addition(a,b));
System.out.println(cal.subtraction(a,b));
System.out.println(cal.multiplication(a,b));
System.out.println(cal.division(a,b));
}}
OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

4. Servlet program for hello world


MyServlet.java
package newpackage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
// TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet MyServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello</h1>");
out.println("<p>Servlet MyServlet at " + request.getContextPath () + "</p>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
}
}
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>
<h1>Hello World!</h1>
<a href="MyServlet">click here</a>
</body></html>

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

5. Servlet program for doGet() and doPost() methods using TOMCAT server .
index. html
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="UserInfo" method="get">
Enter UserName: <input type="text" name="tx1">
Enter RollNo : <input type="text" name="tx2">
<input type="submit" value="Submit">
</form>
</body>
</html>
UserInfo.java (Servlet Class)
package newpackage;
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;

@WebServlet(name = "UserInfo", urlPatterns = {"/UserInfo"})


public class UserInfo extends HttpServlet {

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("tx1");
String rollno = request.getParameter("tx2");
try {
out.println("The UserName is :"+name+" Roll NO is :" +rollno);
} finally {
out.close();
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("tx1");
String rollno = request.getParameter("tx2");
try {
out.println("The UserName is :"+name+" Roll NO is :" +rollno);
} finally {
out.close();
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

6. JDBC statement using MS. Access


import java.sql.*;
public class DbConnect
{
public static void main(String a[]) throws Exception
{
Connection con;
Statement st;
ResultSet rs;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("Jdbc:Odbc:mars");
st=con.createStatement();
rs=st.executeQuery("select * from studentDB");
while(rs.next())
{
System.out.println("\nRoll no\t ="+rs.getInt(1)+"Name\t ="+rs.getString(2));
}
}
}
Output:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

7. Program to execute JDBC prepared statement using MS-Access


Import java.sql.*;
public class PS
{
public static void main(String args[]) throws Exception
{
ResultSet rs;
Int acc=1;
Int var1;
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection
Con=DriverManager.getConnection(jdbc:derby://localhost:1527/DB11, as, as);
PreparedStatement pt=
con.prepareStatement(select balance from APP.accInfo where accno=?");
pt.setInt(1,acc);
rs=pt.executeQuery();
while(rs.next())
{
var1 =rs.getInt("balance");
System.out.println(var1+" is the balance");
}
}
}
Output:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

8. Program to create a java bean using BDK for graphical shapes.


package newpackage;
import javax.swing.JButton;
import java.awt.*;
import java.io.Serializable;
public class grbuttonx extends JButton implements Serializable
{
private Image img;
public grbuttonx()
{
img=null;
}
public void setImage(Image i)
{
img=i;
}
public void paint(Graphics g)
{
if (img != null)
g.drawImage(img, 0, 0,null);
else
{
g.setColor(Color.red);
Dimension size = getSize();
g.fillOval(0, 0, size.width, size.height);
//g.fillRect(0,0, size.width, size.height);
//g.fillRoundRect(0,0, size.width, size.height,5,5);
}
}
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

9.Program to implement a Stateless EJB


package newpackage;
import javax.ejb.Stateless;
@Stateless
public class StatelessBean implements StatelessRemote {
public String sayHello(String name) {
return "welcome"+name;
}
}
StatelessBeanRemote.java
package newpackage;
import javax.ejb.Remote;
@Remote
public interface StatelessRemote {
String sayHello(String name);
}
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>
<h1>Hello World!</h1>
<form action="Hello" method="GET">
<input type="text" name="name" value="" /><input type="submit" value="submit"
/></form>
</body>
</html>

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

Servlet.java
package newpackage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
@EJB
private StatelessRemote statelessBean;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("name");
String name1=statelessBean.sayHello(name);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>welcome... " +name1+ "</h1>");
out.println("</body>");

out.println("</html>");

} finally {
out.close();
}
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

10.Program to implement a state full EJB


NewSessionBean.java
package newpackage;
import javax.ejb.Stateful;
@Stateful
public class NewSessionBean implements NewSessionRemote {
int c = 0;
public int counter() {
return c++;
}
public void remove() {
}
}
NewSessionRemote.java
package newpackage;
import javax.ejb.Remote;
@Remote
public interface NewSessionRemote {
int counter()
}

NewServlet.java
package newpackage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NewServlet extends HttpServlet {
@EJB
private NewSessionRemote newSessionBean;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

int c=newSessionBean.counter();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
// TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>counter is"+c+" </h1>");
out.println("</body>");
out.println("</html>");
//
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on
the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello
</h1>
<a href="NewServlet">Click</a>
</body></html>

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

Output:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

11. Program to implement CD/DVD Browsing Application using C#.net


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;
namespace BrowseCd
{
public partial class frmbrowse : Form
{
public frmbrowse()
{
InitializeComponent();
}
private void frmBrowse_Load(object sender, EventArgs e)
{
}
private void btnBrowse_Click(object sender, EventArgs e)
{
using (OpenFileDialog open = new OpenFileDialog())
{
open.Title = "CD / DVD Browsing Application";
open.ShowDialog();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmBrowse_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult d = MessageBox.Show("Are you sure you want to close this
application?","Browsing
Application",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultBut
ton.Button2);
if (d == DialogResult.No)
e.Cancel = true;
}
}
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

12. Program to Implement Information Retrieving Box Application using C#.Net


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;
namespace InformationFromMessageBox
{
public partial class frmRetrieve : Form
{
public frmRetrieve()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
DialogResult d = MessageBox.Show("Click any of the button and see
the information retrieved and displayed below!","Information To
Retrieve",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information);
if (d == DialogResult.Yes)
lblInformation.Text = "You have clicked Yes button!";
else if (d == DialogResult.No)
lblInformation.Text = "You have clicked No button!";
else if (d == DialogResult.Cancel)
lblInformation.Text = "You have clicked Cancel button!";
lblInformation.Visible = true;
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmRetrieve_FormClosing(object sender, FormClosingEventArgs
e)
{
DialogResult d = MessageBox.Show("Are you sure you want to close
this
application?","Information
To
Retrieve",MessageBoxButtons.YesNo,
MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
if (d == DialogResult.No)
e.Cancel = true;
}}}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

13. Program to Implement a Currency Converter Application using C#.Net


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;
namespace WindowsFormsApplication1
{ public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult d = MessageBox.Show("are you sure you wnat to exit", "Currency Converter",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (d == DialogResult.No)
e.Cancel = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex < 0)
{
MessageBox.Show("please select a country", "currency converter",
MessageBoxButtons.OK, MessageBoxIcon.Information);
comboBox1.Focus();
return;
}
else if (textBox1.Text.Length == 0)
{
MessageBox.Show("please enter amount", "currency converter",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Focus();
return;
}
double currencyValue = 0;
switch (comboBox1.SelectedIndex)
{
case 0: currencyValue = 75;
break;
case 1: currencyValue = 170;
break;
case 2: currencyValue = 13;
break;
case 3: currencyValue = 43;
break;
case 4: currencyValue = 48;
break;
}
double amount = double.Parse(textBox1.Text);
textBox2.Text = (currencyValue * amount).ToString();
}
}
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

14. Program to implement ActiveX control for Name and Age


AClass.cs
using System;
using System.Runtime.InteropServices;
namespace ANamespace
{
public interface ASignatures
{
string FName();
string SName();
int Age { get;}
}
[ClassInterface(ClassInterfaceType.AutoDual)]
public class AClass :ASignatures
{
public string FName()
{
return "Medicine";
}
public string SName()
{
return "Engineering";
}
public int Age
{
get { return 24; }
}
}
}
Activex1.html
<html>
<head>
<script language="javascript">
<!-- Load the ActiveX object -->
var x = new ActiveXObject("ANamespace.AClass");
<!-- Access the Method -->
alert(x.FName());
alert(x.SName());
<!-- Access the Property -->
alert(x.Age);
</script>
</head>
<body>
</body>

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

BIT-432

M.J.C.E.T.,-HYD34

MIDDLEWARE TECHNOLOGIES LAB

1604-10-737-033

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

15. Program to create Domain Name Server like DNS using RMI that connects to
database to retrieve IP addresses given website name.
dnsinter.java
import java.rmi.*;
public interface dnsinter extends Remote
{
public String showip(String ws) throws Exception;
}
dnsimp.java
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
public class dnsimp extends UnicastRemoteObject implements dnsinter
{
Connection con;
PreparedStatement pt;
ResultSet rs;
String ip;
public dnsimp() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("Jdbc:Odbc:Bank");
}
public String showip(String ws) throws Exception
{
pt=con.prepareStatement("select ip from dns where ws=?");
pt.setString(1,ws);
rs=pt.executeQuery();
while(rs.next())
{
ip =rs.getString("ip");
}
return ip;
}
}
dnsServer.java
import java.rmi.*;
public class dnsServer
{
public static void main(String arg[]) throws Exception
{

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

dnsimp bi=new dnsimp();


Naming.rebind("bankobj",bi);
}
}
dnsClient.java
import java.io.*;
import java.rmi.*;
public class dnsClient
{
public static void main(String arg[]) throws Exception
{
Remote r=Naming.lookup("rmi://127.0.0.1/bankobj");
dnsinter m=(dnsinter)r;
String ip=m.showip("www.google.com");
System.out.println("ip address is" +ip);
}
}

Output:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

16. Program to implement Entity Bean for Student Information System.


NewEntity.java
package newpackage;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class NewEntity implements Serializable {
//private static final long serialVersionUID = 1L;
//@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long id;
private String name;
public NewEntity() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof NewEntity)) {
return false;
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

NewEntity other = (NewEntity) object;


if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "newpackage.NewEntity[id=" + id + "]";
}
}
NewEntityFacade.java
package newpackage;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class NewEntityFacade implements NewEntityFacadeRemote {
@PersistenceContext(unitName="EnterpriseApplication2-ejbPU")
private EntityManager em;
public void create(Long id,String name) {
NewEntity newEntity =new NewEntity();
newEntity.setId(id);
newEntity.setName(name);
em.persist(newEntity);
}
public void edit(NewEntity newEntity) {
em.merge(newEntity);
}
public void remove(NewEntity newEntity) {
em.remove(em.merge(newEntity));
}
public NewEntity find(Object id) {
return em.find(NewEntity.class, id);
}
public List<NewEntity> findAll() {
return em.createQuery("select object(o) from NewEntity as o").getResultList();
}
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

NewEntityFacadeRemote.java
package newpackage;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface NewEntityFacadeRemote {
void create(Long id,String name);
void edit(NewEntity newEntity);
void remove(NewEntity newEntity);
NewEntity find(Object id);
List<NewEntity> findAll();
}
NewServlet.java
package newpackage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NewServlet extends HttpServlet {
@EJB
private NewEntityFacadeRemote newEntityFacade;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Long id=null;
String name=null;
id=Long.parseLong(request.getParameter("id"));
name=request.getParameter("name");
newEntityFacade.create(id,name);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
// TODO output your page here
out.println("<html>");

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on
the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello !</h1>
<form action="NewServlet" method="GET">
<input type="text" name="id" value="" />
<input type="text" name="name" value="" />
<input type="submit" value="submit" />
</form>
</body></html>

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

Output:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

17. Program to implement Entity Bean for Library Management System.


NewEntity.java
package newpackage;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class NewEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String stu_name;
private String stu_cls;
private Long no_of_buks_issued;
public NewEntity() {
}
public Long getNo_of_buks_issued() {
return no_of_buks_issued;
}
public void setNo_of_buks_issued(Long no_of_buks_issued) {
this.no_of_buks_issued = no_of_buks_issued;
}
public String getStu_cls() {
return stu_cls;
}
public void setStu_cls(String stu_cls) {
this.stu_cls = stu_cls;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public Long getId() {
return id;
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

public void setId(Long id) {


this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof NewEntity)) {
return false;
}
NewEntity other = (NewEntity) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "newpackage.NewEntity[ id=" + id + " ]";
}
}
NewEntityFacade.java
package newpackage;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class NewEntityFacade extends AbstractFacade<NewEntity> {
@PersistenceContext(unitName = "EnterpriseApplication4-ejbPU")
private EntityManager em;
public void create(Long id,String stu_name,String stu_cls,Long no_of_buks_issued) {
NewEntity n=new NewEntity();
n.setId(id);
n.setStu_name(stu_name);
n.setStu_cls(stu_cls);
n.setNo_of_buks_issued(no_of_buks_issued);
em.persist(n);
}
public void edit(NewEntity newEntity) {

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

em.merge(newEntity);
}
public void remove(NewEntity newEntity) {
em.remove(em.merge(newEntity));
}
public NewEntity find(Object id) {
return em.find(NewEntity.class, id);
}
public List<NewEntity> findAll() {
return em.createQuery("select object(o) from NewEntity as o").getResultList();
}
}
NewEntityFacadeRemote.java
package newpackage;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface NewEntityFacadeRemote {
void create(Long id,String name);
void edit(NewEntity newEntity);
void remove(NewEntity newEntity);
NewEntity find(Object id);
List<NewEntity> findAll();
}

NewServlet.java
package newpackage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
@EJB
private NewEntityFacade newEntityFacade;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Long id=Long.parseLong(request.getParameter("id"));
String stu_name=request.getParameter("stu_name");
String stu_cls=request.getParameter("stu_cls");
Long no_of_buks_issued=Long.parseLong(request.getParameter("no_of_buks_issued"));
newEntityFacade.create(id,stu_name,stu_cls,no_of_buks_issued);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>name is " + stu_name + "</h1>");
out.println("<h1>id is " + id + "</h1>");
out.println("<h1>class is is " + stu_cls + "</h1>");
out.println("<h1>no of buks issued are" + no_of_buks_issued + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}

index.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>
<h1>Hello World!</h1>
<form method="get" action="NewServlet"/>
<p> ur name is.. </p>

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

<input type="text" name="stu_name" value="enter name" />


<p> ur id is.. </p>
<input type="text" name="id" value="enter id" />
<p> ur cls is.. </p>
<input type="text" name="stu_cls" value="enter cls" />
<p> buks isued are.. </p>
<input type="text" name="no_of_buks_issued" value="enter no of buks" />
<input type="submit" value="submit"/>
</body>
</html>
OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

18. Hello Service using Corba


Hello.idl
module HelloApp
{
interface Hello
{
string sayHello();
oneway void shutdown();
};
};
HelloServer.java
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.util.Properties;
class HelloImpl extends HelloPOA {
private ORB orb;
public void setORB(ORB orb_val) {
orb = orb_val;
}
public String sayHello() {
return "\nHello world !!\n";
}
public void shutdown() {
orb.shutdown(false);
}
}
public class HelloServer {
public static void main(String args[]) {
try{
ORB orb = ORB.init(args, null);
POA rootpoa =POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
HelloImpl helloImpl = new HelloImpl();
helloImpl.setORB(orb);
org.omg.CORBA.Object ref =rootpoa.servant_to_reference(helloImpl);
Hello href = HelloHelper.narrow(ref);
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "Hello";
NameComponent path[] = ncRef.to_name( name );
ncRef.rebind(path, href);
System.out.println("HelloServer ready and waiting ...");
orb.run();
}
catch (Exception e) {

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

System.err.println("ERROR: " + e);


e.printStackTrace(System.out);
}
System.out.println("HelloServer Exiting ...");
}
}
HelloClient.java
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
public class HelloClient
{
static Hello helloImpl;
public static void main(String args[])
{
try{
ORB orb = ORB.init(args, null);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "Hello";
helloImpl = HelloHelper.narrow(ncRef.resolve_str(name));
System.out.println("Obtained a handle on server object: " + helloImpl);
System.out.println(helloImpl.sayHello());
helloImpl.shutdown();
} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
} }}
Output:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

19. Corba program for stock market details retrieval


Stock.idl
module StockApp
{
interface Stock
{
long show(in string a);
};
};
StockServer.java
import StockApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.sql.*;
import java.util.Properties;
class StockImpl extends StockPOA {
private ORB orb;
public void setORB(ORB orb_val) {
orb = orb_val;
}
public int show(String a)
{
Connection con;
PreparedStatement pt;
ResultSet rs;
String name = a;
int var1=0;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("Jdbc:Odbc:stock");
pt = con.prepareStatement("select qty from stockDB where prod=?");
pt.setString(1, name);
rs = pt.executeQuery();
while (rs.next()) {
var1 = rs.getInt("qty");
}
}
catch(ClassNotFoundException e){}
catch(SQLException p){}
return var1;
}
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

public class StockServer {


public static void main(String args[]) {
try{
ORB orb = ORB.init(args, null);
POA rootpoa =POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
StockImpl stockImpl = new StockImpl();
stockImpl.setORB(orb);
org.omg.CORBA.Object ref =rootpoa.servant_to_reference(stockImpl);
Stock href = StockHelper.narrow(ref);
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "Stock";
NameComponent path[] = ncRef.to_name( name );
ncRef.rebind(path, href);
System.out.println("StockServer ready and waiting ...");
orb.run();
}
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("StockServer Exiting ...");
}
}
StockClient.java
import StockApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
public class StockClient
{
static Stock stockImpl;
public static void main(String args[])
{
try{
ORB orb = ORB.init(args, null);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "Stock";
stockImpl = StockHelper.narrow(ncRef.resolve_str(name));
System.out.println("Obtained a handle on server object: " + stockImpl);
String prod="abc";
System.out.println("value is: "+stockImpl.show(prod));

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
}
OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

20. Corba program for bank balance retrieval


Bank.idl
module BankApp
{
interface Bank
{
long bal(in long a);
};
};
BankServer.java
import BankApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.sql.*;
import java.util.Properties;
class BankImpl extends BankPOA {
private ORB orb;
public void setORB(ORB orb_val) {
orb = orb_val;
}
public int bal(int a)
{
Connection con;
PreparedStatement pt;
ResultSet rs;
int acc = a;
int var1=0;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("Jdbc:Odbc:bank");
pt = con.prepareStatement("select bal from bankDB where acc=?");
pt.setInt(1, acc);
rs = pt.executeQuery();
while (rs.next()) {
var1 = rs.getInt("bal");
}
}
catch(ClassNotFoundException e){}
catch(SQLException p){}
return var1;
}
}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

public class BankServer {


public static void main(String args[]) {
try{
ORB orb = ORB.init(args, null);
POA rootpoa =POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
BankImpl bankImpl = new BankImpl();
bankImpl.setORB(orb);
org.omg.CORBA.Object ref =rootpoa.servant_to_reference(bankImpl);
Bank href = BankHelper.narrow(ref);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "Bank";
NameComponent path[] = ncRef.to_name( name );
ncRef.rebind(path, href);
System.out.println("BankServer ready and waiting ...");
orb.run();
}
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("BankServer Exiting ...");
}
}
BankClient.java
import BankApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
public class BankClient
{
static Bank bankImpl;
public static void main(String args[])
{
try{
ORB orb = ORB.init(args, null);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "Bank";
bankImpl = BankHelper.narrow(ncRef.resolve_str(name));
System.out.println("Obtained a handle on server object: " + bankImpl);
int acc=123;
System.out.println("balance is: "+bankImpl.bal(acc));
} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
} }}

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

DEPARTMENT OF INFORMATION TECHNOLOGY

M.J.C.E.T.,-HYD34

1604-10-737-033

OUTPUT:

BIT-432

MIDDLEWARE TECHNOLOGIES LAB

Page No :

You might also like