Writing Your Own Servlet

Here is the code for a servlet that can process the HTML form that we created in the section How form data is sent to a URL on page 694. We're going to send a reply that suggests a suitable pet based on the weight and leg count the user submitted.

Compiling your servlet

Make sure that your CLASSPATH has the servlet jar files in it, as shown in the following command, applicable to Windows:

javac -Djava.ext.dirs="\program files\apache software foundation\Tomcat 5.0\common\lib" PetServlet.java

Note: The example command is just one line, but is too wide to print that way in a book. The “-Dproperty=value” option defines a property with that value to the compiler system. The “java.ext.dirs” property is the pathname ...

Get Just Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.