J2EE Tutorial - Session Tracking Example
package ourbeans; // THIS IS A JAVA BEAN.import java.util.*;
public class cart
{
Vector vector1;
public cart()
{
vector1= new Vector();
}
public void add(String s)
{
vector1.addElement(s);
}
public Vector showitems()
{
return vector1;
}
}
carter.htm
<html>
<body>
<form method=post action=carter.jsp >
WHICH PLACES DO YOU WANT TO VISIT? <BR>
<select name=combo1>
<option value="Bombay">Bombay
<option value="Bangalore">Bangalore
<option value="Madras">Madras
<option value="Delhi">Delhi
<option value="Calcutta">Calcutta 0
<option value="Nagpur">Nagpur
</select>
<input type=submit> 1
</form>
</body>
</html> 2
The following JSP file is invoked by carter.htm ( pay particular attention to 'scope' of the bean!)
carter.jsp 3
<html>
<body>
<jsp:useBean id="cart1" scope="session" class="ourbeans.cart" /> 4
<%
String s=request.getParameter("combo1");
cart1.add(s); 5
Vector vector1 =cart1.showitems();
int n=vector1.size();
for(int j=0; j<n; j++) 6
{
String a = (String) vector1.elementAt(j);
out.println(a); 7
out.println("<br>");
}
%> 8
</body>
</html>
Copy carter.htm & carter.jsp to c:\tomcat\webapps\root directory. In Internet Explorer , we type the url as 'http://localhost:8080/carter.htm. We get the form which asks you what places you would like to visit. You can select some city and submit. You can go back and select another city and submit again. We find that city names thus selected are added to the list and displayed, thereby achieving session-tracking. 9
While studying EJB, the most important topic in J2EE, there will be reference to 'stateful session bean'. (ie) a session bean which has session-tracking feature. ( A stateless session bean does not retain its state).The greeter bean that we saw earlier is an example of 'stateless' bean. ( but not in EJB context).
Having thus familairized ourselves with the essential web-server techniqies, we are now ready to move on to the enterprise tier, (ie) the remote object server. This RemoteObjectServer may be either RMI server or IDL server or RMI-IIOP server . Finally, it can be an EJB server.
The core of J2EE is EJB . At present , EJB is sought to be exposed as an XML-WebService. The second group is the essence of J2EE. Though, four technologies are mentioned there (ie) RMI, IDL, RMI-IIOP and EJB, the first 3 technologies are a build up towards EJB ,the culmination. 0
RMI was introduced in JDK1.1 (1997)
IDL was introduced in JDK1.2 (1998)
RMI-IIOP was introduced in JDK1.3 (1999) 1
EJB followed closely.
Modern Enterprise Computing , in the final reckoning, is a manifestation of Distributed Objects Technology. Typically, the Enterprise Computing world is characterized by great many Operating systems and equally varied Programming languages. The operating systems may be IBM Mainframe, IBM Midframe like OS 400, Mini Computer systems (HP) , UNIX systems ( AIX of IBM, HP-UX of Hewlett-Packard, Solaris of SUN etc) , Linux & finally Desktop Windows / Mac & Mobile PALM. Such a situation may well occur even within a single organization, if it is fairly large and mature.( In other words, even an Intranet may have a multiplicity of platforms and languages). The earliest Enterprise language of the Mainframe era was COBOL. In the UNIX systems , C & later C++ reigned supreme. By 1990, Visual Basic entered the scene and gradually matured into VB.net, and it now challenges C++! In 1995, Java was released and today it is almost the default language for Enterprise world. Even in MicroSoft DotNet platform, J# is a an approved choice and so Java ( pure or tainted) can claim to be the universal language for Enterprise computing today.( J# is another incarnation of Visual J++6. but restricted to webapplications using ASP.net.)
( However, for works like device drivers, Java is not directly suitable, as it does not have pointers. Hence C/C++ are preferred for such work. But, the C/C++ routines can be wrapped in Java using JNI (Java Native Interface) and then released for the Web World. Hence, there is a great need to develop a standard, where the objects written in different languages & residing within different machines running different opearting systems can coopearte to produce the end result. Such a system will be an instance of Distributed computing.And the objects are Distributed Objects.Before the advent of Object -Oriented Programming, RPC (Remote Procedure Call) was used between different machines. This was later modified into ORPC ( Object-Oriented Remote Procedure call). 2
Microsoft DCOM was based on such a concept. DCOM was based on COM and COM is language -agnostic.(ie) we can create COM in any language of our choice in Microsoft platform.( At present DCOM , has been superceded by DotNet framework) . The drawback of DCOM was that it was very much platform-dependent. (ie) It worked only where both the server and client were Windows machines. Even DotNet platform is , as yet,Windows-specific.(However, DotNet WebService is able to communicate with other platforms like Unix/Linux etc by using XML .This is for the first time that it has become possible , with such ease. XML is textual and non-proprietory. XML parsers are available for all platforms. Hence the superiority of ASP.net webservice over DCOM.) And Microsoft has unceremoniuosly jettisoned DCOM.
Java is the only compiled language , so far ,which runs in a great variety of platforms. Unlike C & C++ programs , we do not have to compile the code and tweak it for each platform.This feature , besides automatic garbage collection, is the greatest merit of Java and why J2EE is the preferred choice of big companies ,which typically use OS like Mainframe, Midframe, MiniComputer, UNIX & Linux rather than Windows.