★ wanayoo — archive 1999 http://www.wdvl.com/Authoring/Tools/Tutorial/serverside_java.htmlNouvelle recherche | Portail wanayoo
Click Here! logo
internet.com
Join the free
WDVL newsletter!

home

Server-Side Java

November 1, 1999

Another alternative to using CGI or SSI/ASP technology is to create services on the web server machine that are capable of handling web input, processing that input, and returning the processed input back through the web server. One of the preferred languages for doing so is Java (though C++ or VB would also work for servers).

There are two primary ways to do this: 1)Java Servers and 2) Java Servlets.

Implementing a server in Java is fairly trivial since the language was designed with the network in mind. Essentially you create sockets for each client and then process the data coming in through the socket.

On a very simplistic level, you might have something like:

try {
	ServerSocket socket = new ServerSocket(1969);
	Socket s = server.accept;
	PrintStream ps = new printStream(s.getOutputStream());
	ps.println("Hello Cyberspace");
	ps.flush;
	}
catch (IOException e) {
	System.out.println(e);
	}

Once your server is written, you can start it up and it should wait patiently for clients to connect to it. Connection code might be as simple as the following:


try {
	Socket s = new Socket("www.mydomain.com", 1969);
	DataInputStream dis = new DataInputStream(s.getInputStream());
	String serverResponse = dis.readLine();
	System.out.println(serverResponse);
	}
catch (IOException e) {
	System.out.println(e);
	}

However, most people actually implement server-side Java with Servlets. Servlets allow you to embed a Java server service through the web server. You can think of them as server-side applets. The web server will load and execute them the same way a browser would an applet.

The lifecycle of a servlet execution would be something like

  1. Web browser makes an HTTP request, specifying the servlet to be used to handle the request
  2. The web server passes off the request to the servlet. If the servlet has not already been loaded into memory, the web server will do so (that way future requests can be handled immediately).
  3. The servlet responds by sending data back to the client via the web server.

As you can see, this works much like CGI and ASP. In fact, like ASP, or perl embedded CGI, servlets work very fast because they are loaded into the web server.

Servlets (like Servers) have the extra benefit in that since they run on the server side, security and communications restrictions typical of Java applet programming do not apply. Servlets are freed of the Java sandbox.

Servlets can also maintain state, are platform independent, and extremely extensible as they are written in object oriented Java.

The only catch is that you need to use a web server that knows about servlets. The most popular include Java Web Server (JWS), Apache (using JServ), and O'Reilly WebSite. However, even if you don't have a servlet enabled web server, many web servers have third party add-ons which handle servlets on behalf of the web server. These include ServletExec and JRun.

COM and Active-X
Introduction to the Web Application Development Environment (Tools)
Distributed Resources DCOM, CORBA, RMI

Up to => Home / Authoring / Tools / Tutorial


internet.com
e-commerce


Click Here!

Copyright 1999-2000 internet.com Corp. Legal Notices. Privacy Policy.

www.internet.com