499 Simple Servlet Demo
1. Create a directory called
"caradd"
2. Create a directory in carradd called
"WEB-INF"
3. Create a directory in caradd called
"classes". This is where you place your servlets code (i.e. the java
files and classes). While strictly speaking you don't have to keep your java
files here, it is nice to have them all in one place
4. Create a servlet in 'classes' called
CarAdd.java. Here is some sample code:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class CarAdd extends HttpServlet
{
/**
* Handle
the HTTP GET method by building a simple web page.
*/
public void
doGet (HttpServletRequest request,
HttpServletResponse response)
throws
ServletException, IOException
{
PrintWriter out;
String title = "Car Add Placement
Servlet";
// set
content type and other response header fields first
response.setContentType("text/html");
// then
write the data of the response
out =
response.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD><BODY>");
out.println("<H1>"
+ title + "</H1>");
out.println("<P>This
is output from the car add servlet.");
out.println("</BODY></HTML>");
out.close();
}
}
1. Create a file called
"web.xml" in the 'WEB-INF' directory. This binds a servlet name to
the servlet class. Here is the "web.xml" file for this applet:
<?xml version="1.0"
encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>caradd</servlet-name>
<servlet-class>CarAdd</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>caradd</servlet-name>
<url-pattern>/caradd</url-pattern>
</servlet-mapping>
</web-app>
1. Start a Tomcat server
2. Open http://<machine>:<port>/caradd/caradd
in a web browser where machine is the
name of the machine you are using and port
is your first port number. If you don't have a port number use 8080.
Other Servlet Resources
CMPUT 391
Servlets Information
http://www.cs.ualberta.ca/~shengjiu/ta/cmput391.html
This is from
a database course that is being offered currently that is also using servlets.
It contains setup information for the UAlberta environment.
Servlets
Tutorial
http://java.sun.com/docs/books/tutorial/servlets/TOC.html
This is Sun's
Java tutorial for servlets
Servlets API
http://java.sun.com/products/servlet/2.2/javadoc/index.html
The Java
Servlet API documentation
Tomcat
Documentation
http://jakarta.apache.org/tomcat/jakarta-tomcat/src/doc/uguide/tomcat_ug.html