Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
EJB
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

J2EE Tutorial - Session Tracking

                           

Let us now consider a case where the object  is not available in the webserver but in another remote machine. This is a typical case of  MULTI-TIER Client-Server computing. The client here is  a  servlet program.Let us call it 'socketservlet.java'. This  program accepts the data from the  form as before but  passes the data to the remote server. The bean in the remote server uses this data as parameter for its business logic, creates the result and then sends the result to the servlet. The servlet then forwards the result to the client's browser. We will use the standard Socket method.

  It is evident that we are now introducing one more layer.( we have 3 layers now).

   1) The user's browser

   2) The servlet in web-server

   3) The Remote Server housing the bean encapsulating the business logic.

   In this case, no processing is done at the web-server. The servlet simply accepts  the data from the user, forwards it to the remote server where the data is processed by the bean at the remote server and the program sends the result to the servlet , which in turn sends it to the user's browser.

import java.io.*;

import java.net.*;

public class socketgreetserver

{

   public static void main(String args[])

   {

   try

   {

   ServerSocket  server= new ServerSocket(1234);

   System.out.println("server ready..listeneing for client in port 1234!");

   while(true)

   {

   Socket  sock = server.accept();

   System.out.println("client connected");

   DataInputStream  ins=new DataInputStream(sock.getInputStream());

   String s = ins.readLine();

   System.out.println("data received");

   greeter   greeter1 = new   greeter();      //  create instance of local bean

   String  s1 = greeter1.greetme(s);

   PrintStream  ps= new  PrintStream(sock.getOutputStream());

   ps.println(s1);

   }

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

   }//main

}

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

Edit the above file in a dos window.

 >set path=c:\windows\command;c:\jdk1.3\bin

>javac  socketgreetserver.java     

>java socketgreetserver

   Now the remoteserver is running and is waiting for connection

request from the client.

(ensure that the bean class is available in the same folder).

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

 We now see the corresponding servlet.

// socketgreetservlet.java

import java.io.*;

import java.net.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class socketgreetservlet extends HttpServlet

{

  public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException

  {

       response.setContentType("text/html");

       PrintWriter out=response.getWriter();

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

   try

   {

    String s1=request.getParameter("text1");//  get data from browser

   Socket  client= new Socket("localhost",1234);  // for testing

   System.out.println("server connected!");

   PrintStream  ps= new  PrintStream(client.getOutputStream());

   ps.println(s1); // send data to remote server

   DataInputStream  ins=new DataInputStream(client.getInputStream());

   String s = ins.readLine();    // get the data sent by the remote server

   System.out.println("result obtained from remote server");

   out.println("<html>");

   out.println("<body>");

                  out.println(s);    // send it to browser

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

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

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

   }//main

}

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

(Compilation & deployment in Tomcat , as before)

    Can we call this 'Distributed Object 'Technology?  Though , this indeed is a case of Distributed Computing, it does not qualify for being classified as 'Distributed Objects' technology, because

the method on the bean was invoked in the Remote Server and the bean also resided in the Remote Server.

--------

     In the example considered , we assume that at both ends we are having JVM and the remote object also is a java object. What if the remote object is not a java object?

what if the remote object is wriiten in C++ and the author of that code does not care to provide a java wrapper?  Such cases are covered by CORBA  which Sun emulated in graded steps through RMI, IDL,

RMI-IIOP &  finally EJB.

------

   Another important feature required by most web-applications is Session-Tracking.

HTTP is a stateless protocol. (ie) In a multiform application, the data sent through the first

form will not be remembered and cannot be used in a subsequent form. , unless we make some

arrangement.

      The usual illustration of this problem is the 'Shopping Cart'.

The following  methods  have been proposed. 

                       a) Hidden FormFields

                       b) URL-rewriting

                       c) Cookies

                       d) Session object

                       e) Application Object

 But , due to various reasons, it is the 'Session '  object method , that is found to be the best solution. Basically, Session-tracking means that any data pertaining to a browser session is retained for about 20 minutes by default, unless  the user closes the session earlier. The data is retained in the webserver's memory and not in the server's hard disk.( This point is important because, EJB entity bean is stored in Enterprise-server's hard-disk., as we shall see shortly). JSP makes it very easy to do session-tracking if we use a bean. We simply declare the scope of the jspbean as 'Session' and we get session-tracking. The following example shows a typical ' shoppingcart' bean.  We will call it cart.java  (' Shopping Cart'  is a generic term used for describing session-tracking apps.)

                           

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.