Tutorial 6: Servlets

Servlets are server side java classes that render into a web page. (Similar to Perl/CGI)

How to create your first servlet

From Eclipse…
1. Right click on project.
2. Click on new
3. Click on other
4. Type in servlet (it is in the wizard section)
5. Click next.
6. Add package, class name. In this case the package is com.informalsemantics, the class is FirstServlet.
7. Click next
8. Click Finish.

You have now created the scafolding for a simple servlet, however you will notice some compile time errors.

All of the imports for javax.servlet.* will be underlined red and have compile time errors.
For this, you will need to add the servlet-api.jar to the build path.
To do this…
1. right click on your project, build path, configure build path
2. Click on the libraries tab.
3. Click on Add external JARs
4. Navigate to where you installed Tomcat. (in my case, it was c:\tomcat)
5. Go the libs folder. (c:\tomcat\libs)
6. And select servlet-api.jar.
7. Click select.
8. Click OK.
Eclipse will now recompile your project and the compile time errors will disappear.

You now have FirstServlet.java under src in the package com.informalsemantics. Lets do the usual, and add some code to display “Hello World!”.

Open FirstServlet.java.
There are 3 default methods in your servlet class.
– FirstServlet() – default constructor
– doGet – is called when the servlet has a get http request made to it
– doPost – is called when the servlet has a post http request made to it.

@WebServlet(description = “My First Servlet”, urlPatterns = { “/FirstServlet” })
This tell the JVm that the servlet is called “My First Servlet” and it can be found at the following URL. “/FirstServlet” (web context)

The params to doGet and doPost are…
– HttpServletRequest – contains anything that comes FROM the browser.
– HttpServletResponse – Contains the stuff the application needs to send back to the browser. Most common is to get the writer from HttpServletResponse for a PrintWriter that then writes data back to the browser.

response.setContentType(“text/html”) tells the browser to expect HTML.

You can then use the out.println statement to build a webpage using standard HTML tags.
e.g. out.println(“

Hello World!

More complex servlets – Hello BLUE world
In the real world, your code will never be this simple, so let’s create a custom helper class to do some work in our new servlet.

In this, I’ve used a helper class to output the title and change the colour to blue. This gives an example of how to interface with objects within your servlet. I’ve created a servlet called TestServlet1 and a custom class called TestServlet1Utilities.

The TestServelet1Utilities class contains a single method that simple returns a string with the HTML doctype and head tags. See below.

	 * <pre>: Requires the title of the page
	 * <post>: Outputs the required html as a string that defined the title. 
	public static String getHeadWithTitle(String title){
	    return("<!DOCTYPE html>\n" +
	            "<html>\n" +
	            "<head><title>" + title + "</title></head>\n"); 

Annotations VS web.xml
You can now create a servlet by just using an annotation.
e.g. @WebServlet(“/test”)
and this allow, assuming your class extends HttpServlet, is enough to let the web app know to serve your servlet from the specified path.
Prior to this, each servlet has to be specifically specified in the applications web.xml file.
Web.xml lives inside the web-inf directory of your application.
e.g. of a web.xml file defining a servlet:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"





While this is how you will see a lot of servlet code written, annotations is an easier and simpler approach.