Servlet logo
Servlet Tutorial

Overview  
HTTP/CGI  
Servlets  
JSP  
Resources  

Zameer's Education


 

Introduction | Servlets & HTTP | Life-Cycle | Sessions | Summary | Examples

Examples: Source & Class Files

Running Servlets

The recent boom in server-side Java has made it even more simple to get your servlets up and running. If you haven't got access a web server that implements the Servlet 2.1 or above API, download one. You can get Tomcat (2.2), JSWDK (2.1) or ServletExec (2.1) from our servlet resource page.

Running under Servlet 2.1 API

Every web server that supports servlets will have a servlets_root directory. It is under servlets_root that you place your compiled servlet classes. For example, our HelloWorld servlet example that is in the com.ack.servlets package would reside in:

servlets_root/com/ack/servlets/HelloWorld.class

The web server reserves the URI /servlet for use by servlets. A call to http://www.yourserver.com/servlet sees the web server looking under the servlets_root directory for the servlet in question. For example, the following URL is used to explicitly invoke the above HelloWorld servlet:

http://www.yourserver.com/servlet/com.ack.servlets.HelloWorld

There are a couple of things to note here. Firstly, there is no trailing s in /servlet. Secondly, as expected, you package name should mirror the directory structure under servlets_root, in this can com.ack.servlets maps onto com/ack/servlets. And finally, you do not need to prefix HelloWorld with .class.

Another way to access a servlet at the web server by its registered name. For example, when we registered the com.ack.servlets.HelloWorld servlet at the web server, we could have given it the name hey. Because servlets can be accessed by their registered name, we can invoke the HelloWorld servlet as follows:

http://www.yourserver.com/servlet/hey

As well as registered names, you can also access a servlet through a servlet alias. This is a very simple but powerful feature that the web server can employ for a number of difference reasons. With servlet aliasing we can alias the HelloWorld servlet to any URI under that web server. For example, the servlet alias /docs/greetings.html for HelloWorld would see the servlet being invoked for the following URL:

http://www.yourserver.com/docs/greetings.html

Servlet aliasing can also be done for groups of documents, URIs and so on. The alias /hello could be used invoke the HelloWorld servlet for all URIs beginning with /hello, for exampe:

http://www.yourserver.com/hello/stuff.html

The alias *.greetings could be used to invoke the HelloWorld servlet for all documents with the .greetings suffix, for example:

http://www.yourserver.com/paranoid/x-files/members.greetings

In fact, servlet aliasing is used by the web server to invoke serlvets for special documents such as *.jsp, *.shtml, *.jhtml and a rather special URI that goes by the name of /servlet.