Appendix A: Compiling & Executing Servlets
April 5, 2000
Compiling the servlets:
A servlet file has a .java extension , so the FirstServlet
servlet will be saved in a file FirstServlet.java. Use your
favorite 'javac' compiler to compile the sample servlet (and
all other servlets). Make sure that the 'servlets.jar' file is
in your 'classpath'. So for example, if you installed Java Web
Server and the servlets.jar file is in the directory
C:\JavaWebServer2.0\lib\servlets.jar then you can compile the
servlet with the following command:
"javac -classpath
C:\JavaWebServer2.0\lib\servlets.jar FirstServlet.java"
The best environment to test - execute servlets is of-course your
web server. The Web server should support running servlets.
You can try Apache with Jserv or any other web server with a
servlet engine such as Jrun. If you want to try a new web server,
download an
evaluation copy of Java Web Server, (comes with built
in servlet engine).
After you install JWS and start the server, it will start by
default at port 8080.
One other option is to download the
Java Web Server development Kit
from Sun and use it for testing.
Once you have your web server and the servlet engine ready
you need to set up the default directory from which the servlets
will be 'loaded'. For JWS it is '/servlets' under the JWS
directory. So put your compiled servlets in this directory.
Also you need to call the servlet invoker. With JWS
the /servlet alias starts the invoker.
So to access the 'FirstServlet' servlet under 'servlets'
directory use the following URL:
http://Your Hostname : Port no /servlet/FirstServlet
For e.g.
http://localhost:8080/servlet/FirstServlet
So in this article we saw how to build a simple web application
using servlets and JSP. One can build on this and make a lot of
improvements to the application. First of all we made a lot of
assumptions and did not concentrate on the 'best practices' or
performance improvements. Secondly we did not use any database
such as a table for storing user id, password etc. That helped us
not going into the JDBC details as the database calls and
connections with reference to servlets need a special treatment.
As servlets are multithreaded by default, (Which you can change if
you want), one should consider the possible issues with non-thread
safe calls. We will try to cover some of these topics in our next
article.
Sample Application!
Building Web Applications Using Servlets and JSP
Building Web Applications Using Servlets and JSP Part II
|