The objective of this workshop is to create a simple Servlet that can respond to client requests and generate some output.
This workshop should take no longer than 60 minutes.
Click the icon below to see a demo of the Servlet.
Okay so it is not very exciting but the request is passing a query string that defines a
firstname and lastname parameters. You can see these in the address bar
of your browser. The Servlet picks up these values and constructs the phrase "Hello Barney Bear".
You can edit the URL in your address bar and change the values of 'Barney' and 'Bear' to
something else and the Servlet will pick up these values.
There are four parts to this workshop and indeed in writing servlets. These are,
Application Servers can vary in the way they handle Servlets. This workshop assumes you are using Tomcat but if you are using a different Application Server then consult its documentation for details on deploying Servlets.
Simple.java in the 'src' subfolder of the WEB-INF folder.import javax.servlet.*; import javax.servlet.http.*; import java.io.*;
public class Simple extends HttpServlet
{
}
doGet() method so add the following to the
body of the Servlet,
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
}
Note, this method, as always, takes two parameters: HttpServletRequest and
HttpServletResponse and raises two exceptions: ServletException,
and IOException.
doGet() method is to define the content
type for the output which is usually HTTP but could be other formats for example XML or even binary
if the servlet is used to generate images. In our example we are outputting HTML and so we also
instantiate the out object to which we will be writing our HTML.
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String sFirstName = request.getParameter("firstname");
String sLastName = request.getParameter("lastname");
out.println("<html>");
out.println("<head>");
out.println("<title>A Simple Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>A Simple Servlet</h1>");
out.println("<p>Hello " + sFirstName + " " + sLastName + "</p>");
out.println("</body>");
out.println("</html>");
out.close();
By now your Servlet should look like this:
Before we can compile the Servlet we need to setup our environment specifically our classpath. A good practice is to create a batch file that will do this for you so in future you can just run this batch file to set your environment.
rem JDK location Set jdk=C:\jdk1.3\bin rem JSP Container location Set AppServer_Home=[tomcat home]\common\lib Set servlet=%AppServer_Home%\servlet.jar Set CLASSPATH=%jdk%;%servlet%; echo %classpath%
set_env.bat at the command prompt.set_env.bat next time you open the command prompt.javac -d ../classes Simple.java
This will compile the Servlet and if it worked it will create the file
Simple.class in the 'classes' folder, the '-d ../classes' switch tells the
compiler where to put the compiled class file.
If you encountered the error "javax.servlet does not exist" then your classpath is incorrect. Check the paths defined in your batch file and try again.
Errors with the actual Servlet file are reported in the usual manner. Below is an error regarding a missing semicolon:
Simple.java:33: ';' expected
out.println("