The Java Servlet architecture lets you use a persistent Java Virtual Machine to run code. Since the JVM is persistent, it has a long-term memory, simplifying things such as memory management and maintaining state, and eliminates problems such as forking and filled process tables. There are a few implementations of servlets; Sun has one, of course, as does Apache.

I just started working with java sevlets at my new job at HighWire Press. I had messed around with java applets a couple years ago, and they were quite the pain in the ass. Java was not quite as cross-platform as Sun might have hoped, at least at that point.

I had also done some work more recently with server-side programming in the form of PHP.

Servlets seem to combine the best of both worlds, which in turn eliminates their respective weaknesses. Since servlets are run on the server, you don't need to worry about the different web browsers handling java differently. The browser doesn't even make you sit there with your thumb up your ass while it loads its own JVM. Servlets are also compiled, as opposed to interpreted like PHP, which gives it an edge in performance. You still get the rapid development benefits of PHP, too, since the server (supposedly) reloads the compiled servlet whenever it is modified.

You can invoke servlets a couple different ways. One is to have a URL point directly at the servlet, so it is loaded just like any other web page, or you can embed the servlet in a regular web page using server-side includes, generally in the form of SERVLET tags. You can also chain the servlets, so the output from one servlet is filtered through the next.

There's so much more, but I just started learning this stuff a week ago.

Java servlets are used to make all kinds of dynamic web pages. It can be used instead of any and all of the following: perl, PHP, ASP, CGI, etc. So I'm quite amased that so few people use it. It is often much faster, and is always easyer to port to other systems, because its java. The only downside i have found so far is if you have a lot of HTML kode in your servlet kode, its quite a lot of extra koding, instead of joust simple HTML kode, but! this is dealt with if you use JSP (very similar to ASP) to handle your static HTML kode. Anyways I'm very pleased with using servlets and I would encourage anybody working with dynamic web pages to do the same.

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...

Log in or registerto write something here or to contact authors.