Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com

By installing J2EE you'll have j2ee.jar, that contains all classes to develop servlets+JSP . By pointing your tool at this file ( setting a classpath, adding a library ) you can roll !

=======

Servlets cooperate with an HTTP-server, like Apache's webserver, supported by the Tomcat Servlets/JSP container, for example.

By entering this URL in a browser it sends an HTTP GET request to the HTTP-server, which is passed on to an instance of the ServUno class , made by Tomcat. Tomcat can also function as an HTTP-server itself, its default port is 8080, so you can test without a separate HTTP-server.

This class is an extension of javax.servlet.http.HttpServlet and its doGet method is called .

Here, we use the HttpServletResponse reference, resp, that we receive as parameter, to set our response's type ( HTML most of the time, but could be any other MIME type )

Then we ask the response to get us a java.io.PrintWriter reference ,....

to which we can easily output HTML .

After all HTML has been created, it is sent to the HTTP-server, that forwards it to the browsing client .

The client's browser receives what's shown in the picture .

=================

VERY IMPORTANT

ONE SINGLE SERVLET INSTANCE CAN HANDLE HUNDREDS OR THOUSANDS OF CLIENT REQUESTS. DO NOT STORE DATA IN THE SERVLET THAT APPLIES TO ANY ONE SPECIFIC CLIENT. See the Session lesson for more on this.

========

You can add an init and a destroy method to the servlet class, but these apply only to that one single instance. The init method could be used to lookup a datasource or get parameters, f.e.

DON'T FORGET to call the superclass' init !

A servlet instance may stay active for as long as the server runs.

=======

We have deployed our example in the folder/directory servtest.war. Tomcat wants it in its web-apps directory. Some application servers require the files to be zipped in a single file. One way to accomplish this is by using the jar tool. Eclipse(-based tools) have an Export function.

Its name appears in the URL.

It contains these files in the required paths.

The most important lines in the configuration file, web.xml, ....

linked by this name.

Used in the URL.

=======

NOTES

=======

Tomcat 4+ has a manager app that allows you to deploy on Tomcat running, so you won't have to stop+start it.

=======

.war = Web ARchive

=======

WEB-INF uppercase !

=======

If you're using JBoss+Tomcat(embedded), the war directory or file has to be copied to a deploy directory, probably the one under server/default.

=======

If you want to know everything allowed or required in a web.xml file, just look up this URL.

By doing this, you can for instance learn that when you have more than one servlet, you have to specify all servlets first and then their mappings.
? optional, once.
* zero or more.

.