Java Servlets

A servlet is basically a persistant Java class that sits on a server, listening for requests and returning responses. The most common type of servlet is the HttpServlet. The whole API lives in javax.servlet.* package, and is one of the standard core J2EE APIs.

The most important methods in a servlet are the init() and the service() methods. The init() method is called by your servlet container when it first instantiates your servlet, so it can be used to initialise member variables, create connection pools, whatever you need it to. The service() method is called by your container when a request comes in for the servlet. It should process the request and act on it, whether by making queries on a database, or spewing forth HTML, and then return a response object, which should include a reference to whatever the result of processing the request is. In the case of a HttpServlet (javax.servlet.http.HttpServlet), the service() method is replaced by two methods, doGet() and doPost(), which respectively deal with http get and http post requests.

To contradict Fluffy's writeup above, there is a third way to call a servlet, and that is by submitting a html form to it. Granted, this is kinda similar to just typing in the URL, but IMHO there's a subtle difference, in that a form is often submitted with post instead of get. Here's how to submit a form to a servlet:

<form name="myForm" action="URL of Servlet" method="POST or GET">
...form body...
</form>

So then the servlet will do it's thing with whatever data you've submitted in the form.

That's servlets, then. If you're working with servlets and run into a problem, feel free to /msg me. Other resources include Sun's website (java.sun.com), The Jakarta Apache website (jakarta.apache.org, which is home of the Tomcat servlet container/webserver project), or Google.com's Usenet repository (go to google.com, click on "groups" and just type in your question), which is a great for answering kinda trivial little questions, especially the type of question that's only trivial if you know the answer...