Sie sind auf Seite 1von 15

RMI RMI CLIENT: importjava.rmi.*; import java.net.*; import java.io.

*; public class RMIclient { public static void main(String args[]) { String id="",use=""; String c; try { BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in)); String several="rmi://127.0.0.1:1099/RMIserver"; RMIserverinter ad=(RMIserverinter)Naming.lookup(several); System.out.println("\n Enter the username"); use=br.readLine(); c=ad.check1(use); System.out.println("The customer's place is " +c); } catch(Exception e) { System.out.println("Error...."+e.getMessage()); } } } RMI SERVER: importjava.rmi.*; import java.net.*; public class RMIserver { public static void main(String arg[])throws RemoteException { try { RMIserverimpl ad=new RMIserverimpl(); Naming.rebind("RMIserver",ad); } catch(Exception e) { System.out.println("Error........"+e.getMessage()); } } }

RMI IMPLEMENTATION: importjava.rmi.*; importjava.rmi.server.*; importjava.sql.*; public class RMIserverimpl extends UnicastRemoteObject implements RMIserverinter { publicRMIserverimpl()throws Exception { } public String check1(String str)throws RemoteException { try { String name="",pass="",city; Connection conn; Statement stmt; ResultSet res; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn=DriverManager.getConnection("Jdbc:Odbc:customer"); stmt=conn.createStatement(); res=stmt.executeQuery("select * from custinfo where cname='"+str+"'"); if(res.next()) { city=res.getString(3); return(city); } } catch(Exception e) { System.out.println("Error........"+e.getMessage()); } return " "; } } RMI INTERFACE: importjava.rmi.*; public interface RMIserverinter extends Remote { /*public double add(double(d1,double d2) throws RemoteException; public double sub(double(d1,double d2) throws RemoteException; public double mul(double(d1,double d2) throws RemoteException; public double div(double(d1,double d2) throws RemoteException;*/ public String check1(String str)throws RemoteException; // public String check2(String n,Stringpass,intid,Stringgen,int age)throws RemoteException; }

OUTPUT: SERVER:

CLIENT:

SOAP USING XML PROGRAM CODING: package pack1; import java.io.*; import java.util.Iterator; import javax.xml.soap.*; public class attatch1 { public static void main(String[] args) { FileReaderfr = null; BufferedReaderbr = null; String line = ""; try { MessageFactorymessageFactory = MessageFactory.newInstance(); SOAPMessage message = messageFactory.createMessage(); SOAPHeader header = message.getSOAPHeader(); SOAPBody body = message.getSOAPBody(); header.detachNode(); AttachmentPart attachment1 = message.createAttachmentPart(); fr = new FileReader(new File("rose1.txt")); br = new BufferedReader(fr); String stringContent = ""; line = br.readLine(); while (line != null) { stringContent = stringContent.concat(line); stringContent = stringContent.concat("\n"); line = br.readLine(); } attachment1.setContent(stringContent, "text/plain"); attachment1.setContentId("attached_text"); message.addAttachmentPart(attachment1); Iterator iterator = message.getAttachments(); while (iterator.hasNext()) { AttachmentPart attached = (AttachmentPart) iterator.next(); String id = attached.getContentId(); String type = attached.getContentType();

System.out.println( "Attachment " + id + " has content type " + type); if (type.equals("text/plain")) { Object content = attached.getContent(); System.out.println("Attachment contains:\n" + content); } } } catch (FileNotFoundException e) { System.out.println("File not found: " + e.toString()); System.exit(1); } catch (IOException e) { System.out.println("I/O exception: " + e.toString()); System.exit(1); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("array exception" +e.toString()); } catch (Exception ex) { ex.printStackTrace(); } } }

OUTPUT:

SERVLET Program Coding: Temp.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <%! String sidu=""; %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> <form name="f4" method="post" action="displayservlet.do"> Enter the name :<input type="text" name="movie"> <input type="submit" value="servlet call"> </form> <% String ss=""; %> <% ss=(String)session.getAttribute("sidu"); %> age:<%= ss %> </body> </html> displayservlet.do packageorgg; importjava.io.IOException; importjava.io.PrintWriter; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjava.sql.*; importjavax.swing.*; importjava.util.*; importjava.io.Serializable; importjavax.servlet.RequestDispatcher; importjavax.servlet.http.HttpSession; public class displayservlet extends HttpServlet implements Serializable { /** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> methods. *

* @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ public Vector resu; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String str2=request.getParameter("movie"); Statement ps1; ResultSetrst; Vector v=new Vector(); String tt; try { /* TODO output your 5m here. You may use following sample code. */ out.println("<html>"); out.println("<head>"); out.println("<title>Servlet displayservlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet displayservlet at " + request.getContextPath() + "</h1>"); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:dsn1","", ""); String sql1="select * from info where pname='"+str2+"'"; ps1=con.createStatement(); rst =ps1.executeQuery(sql1); while(rst.next()) { tt=rst.getString(3); out.println(tt); request.getRequestDispatcher("temp.jsp").forward(request,response);*/ HttpSession session=request.getSession(true); session.setAttribute("sidu",tt); response.sendRedirect("temp.jsp"); } } catch(Exception ex) { } out.println("</body>");

out.println("</html>"); } finally { out.close(); } this.resu=v; } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP * <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { processRequest(request, response); } /** * Handles the HTTP * <code>POST</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { processRequest(request, response);} /** * Returns a short description of the servlet. * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }

Sample input/output:

IMPLEMENTING CORBA SERVICES USING JAVA SquareIntf.idl: moduleSquareApp { interface SquareIntf { long getSquare(in long num); oneway void shutdown(); }; }; SquareClient.java: import SquareApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import java.io.*; public class SquareClient{ public static void main(String args[]){ try{ // create and initialize the ORB ORBorb = ORB.init(args, null); // get the root naming context org.omg.CORBA.ObjectobjRef = orb.resolve_initial_references("NameService"); // Use NamingContextExt instead of NamingContext. This is // part of the Interoperable naming Service. NamingContextExtncRef = NamingContextExtHelper.narrow(objRef); // resolve the Object Reference in Naming String name = "Hello"; SquareIntfhelloImpl = SquareIntfHelper.narrow(ncRef.resolve_str(name)); System.out.println("Obtained a handle on server object: " + helloImpl); System.out.println("Enter the number"); intnum=Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine()); System.out.println(helloImpl.getSquare(num)); helloImpl.shutdown(); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } }

SquareServer.java: import SquareApp.*; 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 SquareImpl extends SquareIntfPOA{ private ORB orb; public void setORB(ORB orb_val){ orb = orb_val; } public intgetSquare(int num1){ return num1*num1; } public void shutdown(){ orb.shutdown(false); } } public class SquareServer{ public static void main(String args[]){ try{ // create and initialize the ORB ORBorb = ORB.init(args, null); // Get reference to rootpoa& activate the POAManager POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); // create servant and register it with the ORB SquareImplhelloImpl = new SquareImpl(); helloImpl.setORB(orb); // create a tie, with servant being the delegate. SquareIntfPOATie tie = new SquareIntfPOATie(helloImpl, rootpoa); // obtain the objectRef for the tie // this step also implicitly activates the // the object SquareIntfhref = tie._this(orb); // get the root naming context org.omg.CORBA.ObjectobjRef = orb.resolve_initial_references("NameService"); // Use NamingContextExt which is part of the Interoperable // Naming Service specification. NamingContextExtncRef = NamingContextExtHelper.narrow(objRef); // bind the Object Reference in Naming String name = "Hello";

NameComponent path[] = ncRef.to_name( name ); ncRef.rebind(path, href); System.out.println("HelloServer ready and waiting ..."); // wait for invocations from clients orb.run(); } catch (Exception e){ System.err.println("ERROR: " + e); e.printStackTrace(System.out); } System.out.println("HelloServer Exiting ..."); } }

OUTPUT:

DCOM Client/Server application

SimpleChatServer.cpp
#include "stdafx.h" #include "SimpleChatServer.h" #include <atlstr.h> #define MAX_LENGTH 300 STDMETHODIMP CSimpleChatServer::CallMe(BSTR clientName, BSTR** helloMessage) { CString temp = _T("Hi "); temp += clientName; temp += _T(", welcome to the simple chat server!"); BSTR str = temp.AllocSysString(); *helloMessage = &str; return S_OK; }

ConsoleCOMClient.cpp
#include "stdafx.h" #include "../SimpleCOM/SimpleCOM_i.h" #include "../SimpleCOM/SimpleCOM_i.c" int _tmain(int argc, _TCHAR* argv[]) { BSTR* message; HRESULT hr; ISimpleChatServer *chat; hr = CoInitialize(0); if(SUCCEEDED(hr)) { hr = CoCreateInstance( CLSID_SimpleChatServer, NULL, CLSCTX_INPROC_SERVER, IID_ISimpleChatServer, (void**) &chat); if(SUCCEEDED(hr)) { hr = chat -> CallMe(_T("Zhiwei"), &message); MessageBox(NULL,*message, L"Message returned from chat server",0); hr = chat -> Release(); } } CoUninitialize(); return 0; }

Steps: 1. 2. 3. 4. 5. Create an ATL project named SimpleCOM. Add COM class to the server. Create an interface and add method CallMe to it. Add a simple chat code to the method and compile the server. Add a new client project to win32 console application as ConsoleCOMClient and set it as start up project.

Output:

Das könnte Ihnen auch gefallen