+ All Categories
Home > Documents > Jsp Rmi Servlet

Jsp Rmi Servlet

Date post: 25-Oct-2014
Category:
Upload: kpramyait
View: 151 times
Download: 0 times
Share this document with a friend
Popular Tags:
17
Create and Run a simple JSP program using tomcat Step 1: Download latest Apache Tomcat and install machine. Example: The tomcat installed location (C:/Program Files/Apache Software Foundation/Tomcat 6.0/) After installation, the tomcat folder has the following files are bin, conf, lib, logs, temp,webapps and work. Step 2 : Create directory “myjsp” and “simple_programs” under the webapps C:/ Program Files/Apache Software Foundation/Tomcat 6.0/webapps/myjsp/simple_programs Step3 : Create simple jsp program. The program name as “helloworld.jsp” 0001 <%-- 0002 Document : helloworld 0003 Created on : Dec 20, 2010, 10:20:48 PM 0004 Author : eswar 0005 --%> 0006 0007 <%@page contentType="text/html" pageEncoding="UTF-8"%> 0008 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 0009 "http://www.w3.org/TR/html4/loose.dtd "> 0010 0011 <html> 0012 <head> 0013 <meta http-equiv="Content-Type" content="text/ html; charset=UTF-8"> 0014 <title>JSP Page</title>
Transcript
Page 1: Jsp Rmi Servlet

Create and Run a simple JSP program using tomcat

Step 1: Download latest Apache Tomcat and install machine.Example: The tomcat installed location (C:/Program Files/Apache Software  Foundation/Tomcat 6.0/)After installation, the tomcat folder has the following files are  bin, conf, lib, logs, temp,webapps and work.

Step 2 : Create directory “myjsp” and “simple_programs” under the webappsC:/ Program Files/Apache Software Foundation/Tomcat 6.0/webapps/myjsp/simple_programsStep3 : Create simple jsp program. The program name as “helloworld.jsp”

0001 <%--

0002     Document   : helloworld

0003     Created on : Dec 20, 2010, 10:20:48 PM

0004     Author     : eswar

0005 --%>

0006  

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

0008<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

0009    "http://www.w3.org/TR/html4/loose.dtd">

0010  

0011 <html>

0012     <head>

0013         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

0014         <title>JSP Page</title>

0015     </head>

0016     <body>

0017  

Page 2: Jsp Rmi Servlet

0018         <p> Hi, <h3><%="Hello!" %></h3></p>

0019         <p>You successfully run a simple jsp program.</p>

0020         <p>Learn More concept about jsp.</p>

0021  

0022     </body>

0023 </html>

Step 4: Save this file the following location :C:/ Program Files/…/…/webapps/myjsp/simple_programs/helloworld.jspStep 5: Before run this code, know some details about tomcat.http://eswartechsolution.wordpress.com/2010/12/22/apache-tomcat-port-information/Stpe 6: Run jsp programa)Start the tomcat serviceStart -> run -> services.msc -> Select Apache Tomcat -> startb)Open new tab in browser or open new window :http://localhost:8080/myjsp/simple_programs/helloworld.jsp

Page 3: Jsp Rmi Servlet

   }

   //================================================ 

   public  String  greetme(String   s)  throws RemoteException

   {

   return("How are you?......"+s); 

   }

}

//over.

-------------------------------------------------------------------

Now, compile greeterimpl.java

>javac greeterimpl.java

 We get greeterimpl.class

------------------------------------

  The next step is to create the class files for the Stub & Skeleton

using the rmic.exe provided in jdk.

>rmic greeterimpl

You will find that this command automatically creates the class files

for Stub & Skeleton.

--------------------------------------------------------

   We can now create the servlet file as follows, which will be the client for the RMI

server.

--------------------------

Page 4: Jsp Rmi Servlet

greeterclientservlet.java

import greeterinter.*;

import greeterimpl.*;

import greeterimpl_Stub.*;

import greeterimpl_Skel.*;

import java.net.*;

import java.rmi.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class greeterclientservlet   extends HttpServlet

{

greeterinter   server; 

  public void doPost(HttpServletRequest   request,

   HttpServletResponse  response)

   throws ServletException, IOException

  {

  response.setContentType("text/html");

  PrintWriter   out = response.getWriter();

  String   s = request.getParameter("text1");

  try

  {

Page 5: Jsp Rmi Servlet

 server =(greeterinter) Naming.lookup("//" + "localhost"  + "/greeterinter");

// Protocol is left unspecied.Normally it is JavaRemoteMethod Protocal

// If there are firewall restrictions , it automatically

// switches over to http .

  }

  catch (Exception e1)   {out.println(""+e1);}

   String   s1 = server.greetme(s);

   System.out.println(s1);

  out.println("<html>");

  out.println("<body>");

  out.println(s1);

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

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

  }

}

-----------------------------------------------------------------------

 To compile the servlet, change the classpath as follows:

  >set classpath=%classpath%;c:\jsdk2.0\src

--

  Now we will be able to compile the servlet.

------------------------------------------------------------

Create the html file to invoke the above servlet.

greeterclientservlet.htm

Page 6: Jsp Rmi Servlet

  <html>

<body>

<form  method=post action='http://localhost:8080/servlet/greeterclientservlet'>

<input   type=text  name='text1'>

<input   type=submit>

</form>

</body>

</html>

================================

 Now copy greeterclientservlet.htm to c:\tomcat\webapps\root

Copy all the class files in c:\greeter to   c:\tomcat\webapps\root\web-inf\classes

---------------

Now, we are ready to execute our RMI-servlet program.

====

c:\greeter>start rmiregistry

This command creates a blank window. Minimize it.

In the same DOS window,

>java greetserverimpl

--

  This starts the remote server . The remote object is created and

registered in the RMI registry in the remote server.

We get a message informing us about it.

Page 7: Jsp Rmi Servlet

  The implementation program creates an instance of the class and registers the

instance in the RMI REGISTRY in the remote server, by the identity of 'greeterinter'.

// Naming.rebind("//" + "localhost" + "/greeterinter",   app);

Remember to start the 'Tomcat' server as before.

Now , we start the InternetExplorer and type the URL as

'http://localhost:8080/greeterclientservlet.htm'

------------

  We get a form. Fill up your name in text1 and submit.

---

  The data is sent to the servlet. The servlet program looks up at the

Registry to locate the object by name 'greeterinter'.

After getting an instance.by 'Naming.lookup'  , it invokes the 'greetme' function on the

object and passes the string from the form as paramter to the method called. The

method call with parameter is passed to the stub.The stub sends it to the

Skeleton.The skeleton sends it to the remoteobject in remote server. The remote

object executes the function there and sends the result to skeleton. The skeleton

sends it to stub. The stub sends it to the servlet. The servlet sends the result to the

browser.

**********************************************************************************

( For reasons of space , we are constarined to limit ourselves to this much.

If you need a more elaborate treatment, the books by Jaworsky  and the one by

Bill McCarty are ideal).

******************************************************************************

 Let us now see how,  an RMI-IIOP program is developed.

This part is very unique and important. You may not find this in any textbook,

because , RMI-IIOP was introduced only in JDK1.3 and most of the Java books in

Page 8: Jsp Rmi Servlet

circulation deal only with JDK1.2.Sun's tutorial is not much helpful. So, read

carefully.

-----------------------------------------------------------------------------

Here ara a few lines from Sun's documentation on RMI--IIOP.

 'RMI lacks interoperability with other languages, and because it uses a non-standard

communication protocol, cannot communicate with CORBA objects.

  IIOP is CORBA'S communication protocol.It defines the way in which the bits are

sent over a wire between CORBA clients and servers.

   Previously Java programmers had to choose between RMI and JavaIDL.for

Distributed programming solution. Now, by adhering to a few restrictions, RMI

objects can use the IIOP protocol.( Internet InterOperable Protocol) and

communicate with CORBA objects.

   Thus, RMI-IIOP combines RMI-style ease of use with  CORBA  cross-language

interoperability.

To resume,

We will need 4 files (ie)

   a)  greeter.java  ( the Interface file)

   b)  greeterimpl.java   ( the implementation file)

   c)  greeterclientservlet.java  ( the servlet which invokes the remote server) 

   d)  greeterclientservlet.htm  ( the html file to invoke the servlet)

These files have been given below.( Carefully compare these files with their RMI

counterpart to appreciate the similarity and minute dfferences).

Page 9: Jsp Rmi Servlet

Servlet:

======

To run a servlet one should follow the steps illustrated below: 

Download and Install the tomcat server: Install the tomcat server in a

directory in which you want to install and set the classpath.for the

variable JAVA_HOME in the environment variable.

Set the class for the jar file: Set the classpath of the servlet-api.jar file in the

variableCLASSPATH inside the environment variable by using the following

steps.

For Windows XP, 

Go to Start->Control Panel->System->Advanced->Environment Variables-

>New button and Set the values as

Variable Name:  CLASSPATH 

Variable Value:  C:\Program Files\Java\Tomcat 6.0\lib\servlet-api.jar

For Windows 2000 and NT

Go to Start->Settings->Control Panel->System->Environment Variables->New

button and Set the values as

Variable Name:  CLASSPATH 

Variable Value:  C:\Program Files\Java\Tomcat 6.0\lib\servlet-api.jar

Create a java source file and a web.xml file in a directory structure. 

To develop an application using  servlet or jsp make the directory structure

like one given below and maintain the pages of your application according to

this directory structure: 

Roseindia/

   WEB-INF/

    web.xml

    struts-config.xml

    classes/

   Myservlet.class

   lib/

Page 10: Jsp Rmi Servlet

   Myapp.jar

  Welcome.html

   Welcome.jsp

Compile the java source file, put the compiled file (.class file) in the classes

folder of your application and deploy the directory of your application in the

webapps folder inside the tomcat directory.

Start the tomcat server, open a browser window and type the

URLhttp://localhost:8080/directory (folder name of your application)

name/servler name and press enter.

Page 11: Jsp Rmi Servlet

Put servlet-api.jar inside the lib folder of apache tomcat. 1)create a servlet.

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class HelloWorld extends HttpServlet{   public void doGet(HttpServletRequest request, HttpServletResponse response)                                   throws ServletException,IOException{    response.setContentType("text/html");    PrintWriter pw = response.getWriter();    pw.println("<html>");    pw.println("<head><title>Hello World</title></title>");    pw.println("<body>");    pw.println("<h1>Hello World</h1>");    pw.println("</body></html>");  }}

2)Go to the webapps folder of your apache tomcat and create a web application folder but it should having an appropriate name like examples.

3)Create web.xml and classes folder inside the WEB_INF folder of web application folder.

4)Copy the servlet to the classes folder.

5)Edit the web.xml to include servlet?s name and url pattern.

  <servlet>  <servlet-name>HelloWorld</servlet-name>  <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping>

6)Compile your servlet.

7)Run Tomcat server by clicking the startup.bat file. This is located inside the bin folder of apache tomcat.

8)Open the browser and type the following url:

http://localhost:8080/webapplicationfolder_name/HelloWorld

1)Normal command prompt programin

javac -classpath ".;D:\xampp\tomcat\lib\servlet-api.jar"

D is my driver path can you install to other driver

Page 12: Jsp Rmi Servlet

2) another is easy to compiling system, it using programmer notepad (this system can using for java compiling)

Download for this link (open and free)

http://www.pnotepad.org/

following this step

1) Open PP

2) Tool -> option -> select tool tab

3) Select language java to upper tab

4) Selecting add button

5) Filling form

6) Name :- Servelt

Command:- E:\Program Files\java\jdk1.6.0_25\bin\javac.exe // JDK javac path

Folder :- C:\java // your using or file folder

Parameter :- %f -classpath ".;D:\xampp\tomcat\lib\servlet-api.jar" // this copy and paste and changing a driver Ex:- D

Shortcut:- F12 // selecting you lick button

Save:- current file // or you lick

After Ok

1 view -> output or press F8

Ok finish

Coding after press F12

Page 13: Jsp Rmi Servlet

EJB

I have tried to get the step by step procedure to run a lab experiment. Even after exaustive search I got only a few-that too I could,nt implement the steps as such. I have tried to run the EJB combining the steps from 1 or 2 web resources and the steps from j2ee1.4 tutorial. It really works. I've aslo given the source code for you to test. I have installed j2sd1.4.2 in C drive and j2ee1.4 in c:\Sun2\AppServer Just follow the steps as given below. velumaniramesh at yahoo dot com Place the following java programs in c:\arith1\arith1 arith1.java arith1Home.java arith1Bean.java arith1Client.java Source code is given at the end. Steps for Execution C:\arith1\arith1>set path=c:\j2sdk1.4.2\bin; C:\arith1\arith1>set classpath=c:\Sun2\AppServer\lib\j2ee.jar;c:\Sun2\AppServer\bin;.; C:\arith1\arith1>set j2ee.home=c:\Sun2\AppServer; C:\arith1\arith1>set java.home= c:\j2sdk1.4.2; C:\arith1\arith1>javac *.java AllPrograms-.SunMicrosystems->ApplicationServerPE->Start default server Creating the J2EE Application In this section, you'll create a J2EE application named arith1App, storing it in the file arith1App.ear. 1. In deploytool, select File New Application. 2. Click Browse. 3. In the file chooser, navigate to this directory: C:\arith1\arith1 In the File Name field, enter arith1App.ear. 4. Click New Application. 5. Click OK. 6. Verify that the arith1App.ear file resides in the directory specified in step 3. Creating the Enterprise Bean Packaging the Enterprise Bean To package an enterprise bean, you run the Edit Enterprise Bean wizard of the deploytool utility. During this process, the wizard performs the following tasks: • Creates the bean's deployment descriptor • Packages the deployment descriptor and the bean's classes in an EJB JAR file • Inserts the EJB JAR file into the arith1App.ear file To start the Edit Enterprise Bean wizard, select File New Enterprise Bean. The wizard displays the following dialog boxes. 1. Introduction dialog box a. Read the explanatory text for an overview of the wizard's features. b. Click Next. 2. EJB JAR dialog box a. Select the button labeled Create New JAR Module in Application. b. In the combo box below this button, select arith1App. c. In the JAR Display Name field, enter arith1JAR. d. Click Edit Contents. e. In the tree under Available Files, locate the build/arith1 subdirectory. (If the target directory is many levels down in the tree, you can simplify the tree view by entering all or part of the directory's path name in the Starting Directory field.) f. In the Available Files tree select these classes: arith1.class, arith1Bean.class, and arith1Home.class. (You can also drag and drop these class files to the Contents text area.) g. Click Add. h. Click OK. i. Click Next. 3. General dialog box a. Under Bean Type, select the Stateless Session. b. In the Enterprise Bean Class combo box, select arith1.arith1Bean. c. In the Enterprise Bean Name field, enter arith1Bean. d. In the Remote Home Interface combo box, select arith1.arith1Home. e. In the Remote Interface combo box, select arith1.arith1. f. Click Next. 4. In the Expose as Web Service Endpoint dialog box, select No and click Next. 5. Click Finish. Creating the Application Client An application client is a program written in the Java programming language. At runtime, the client program executes in a different virtual machine than the Application Server Packaging the Application Client To start the New Application Client wizard, select File New Application Client. The wizard displays the following dialog boxes. 1. Introduction dialog box a. Read the explanatory text for an overview

Page 14: Jsp Rmi Servlet

of the wizard's features. b. Click Next. 2. JAR File Contents dialog box a. Select the button labeled Create New AppClient Module in Application. b. In the combo box below this button, select arith1App. c. In the AppClient Display Name field, enter arith1Client. d. Click Edit Contents. e. In the tree under Available Files, locate this directory: C:\arith1\arith1 f. Select the arith1Client.class file. g. Click Add. h. Click OK. i. Click Next. 3. General dialog box a. In the Main Class combo box, select arith1Client. b. Click Next. c. Click Finish. Specifying the Application Client's Enterprise Bean Reference When it invokes the lookup method, the arith1Client refers to the home of an enterprise bean: Object objref = myEnv.lookup("ejb/Simplearith1"); You specify this reference in deploytool as follows. 1. In the tree, select arith1Client. 2. Select the EJB Ref's tab. 3. Click Add. 4. In the Coded Name field, enter ejb/Simplearith1 5. In the EJB Type field, select Session. 6. In the Interfaces field, select Remote. 7. In the Home Interface field enter, arith1.arith1Home. 8. In the Local/Remote Interface field, enter arith1. arith1 9. In the JNDI Name field, select arith1Bean. 10. Click OK. Mapping the Enterprise Bean References To map the enterprise bean references in the clients to the JNDI name of the bean, follow these steps. 1. In the tree, select arith1App. 2. Click the Sun-specific Settings button. 3. Select the JNDI Names in the View field. 4. In the Application table, note that the JNDI name for the enterprise bean is arith1Bean. 5. In the References table, enter arith1Bean in the JNDI Name column for each row. Figure 24-1 shows what the JNDI Names tab should look like after you've performed the preceding steps. Figure 24-1 ConverterApp JNDI Names Deploying the J2EE Application Now that the J2EE application contains the components, it is ready for deployment. 1. Select the arith1App application. 2. Select Tools Deploy. 3. Under Connection Settings, enter the user name and password for the Application Server. 4. Tell deploytool to create a JAR file that contains the client stubs. (For more information on client JAR files, see the description under Creating the Application Client.) a. Select the Return Client JAR checkbox. b. In the field below the checkbox, enter C:\arith1\arith1 5. Click OK. 6. In the Distribute Module dialog box, click Close when the deployment completes. 7. Verify the deployment. a. In the tree, expand the Servers node and select the host that is running the Application Server. b. In the Deployed Objects table, make sure that the arith1App is listed and its status is Running. 8. Verify that a stub client JAR named arith1AppClient.jar resides in C:\arith1\arith1 Running the Application Client To run the application client, perform the following steps. 1. In a terminal window, go to this directory: C:\arith1\arith1 2. C:\arith1\arith1>set APPCPATH=c:\arith1\arith1\arith1AppClient.jar 3. C:\arith1\arith1>set path=c:\Sun2\AppServer\bin; 4. C:\arith1\arith1>appclient -client arith1App.ear -name arith1Client -textauth In the terminal window, the client displays the result Product of 2& 3:6 //arith1Client.java import arith1.arith1; import arith1.arith1Home; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class arith1Client { public static void main(String[] args) { try { Context initial = new InitialContext(); Context myEnv = (Context) initial.lookup("java:comp/env"); Object objref = myEnv.lookup("ejb/Simplearith1"); arith1Home home = (arith1Home) PortableRemoteObject.narrow(objref, arith1Home.class); arith1 a = home.create(); int product = a.multiply(3,2); System.out.println("Product of 2& 3:"+product);

Page 15: Jsp Rmi Servlet

System.exit(0); } catch (Exception ex) { System.err.println("Caught an unexpected exception!"); ex.printStackTrace(); } } } //arith1.java package arith1; /** * Remote interface for CalculatorBean. */ import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.math.*; public interface arith1 extends javax.ejb.EJBObject { /** * The method that returns the multiplied value */ public int multiply( int val1,int val2 ) throws java.rmi.RemoteException; } //arith1Home.java package arith1; /** * Home interface for CalculatorBean. */ public interface arith1Home extends javax.ejb.EJBHome { public arith1 create() throws javax.ejb.CreateException,java.rmi.RemoteException; } //arith1Bean.java package arith1; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class arith1Bean implements SessionBean{ public void ejbCreate() throws CreateException { } public void setSessionContext( SessionContext aContext ) throws EJBException { } public void ejbActivate() throws EJBException { } public void ejbPassivate() throws EJBException { } public void ejbRemove() throws EJBException { } /** * The method that returns the multiplied value * */ public int multiply(int val1, int val2){ System.out.println("I am from multiply : " + val1 + " * " + val2); return val1*val2; } }


Recommended