A Servlet extends the javax.servlet.http.HttpServlet class.
In a Servlet, you can override doXxxx methods.
<Sample (JDK5.0+Tomcat5.5)>
Directory
scwcd
+ WEB-INF
+ src
+ sample
+ classes
+ sample
web.xml
Test.java
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class Test extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("<html><body>");
out.println("Hello");
out.println("</body></html>");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>scwcd</description>
<display-name>scwcd</display-name>
<distributable />
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>sample.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
</web-app>
See http://localhost:8080/scwcd/Test on your browser.
<Result>
Hello
Tags in the web.xml
servlet : Information for a Servlet
servlet-name : Servlet name
servlet-class : [package name].[Servlet name]
Use a jsp-file tag for specifying a jsp file
servlet-mapping : Mapping between a Servlet and URL
url-pattern : The URL pattern for a Servlet
URL is like this:
http://[domain]/[project root]/[Savlet name]
The distributable tag
That means the application can be deployed in a distributed environment.