使用Sysdeo Tomcat plugin集成Eclipse 和 Tomcat(2)
-----------创建Servlet
1.接上个例子MyTomcatProject,在WEB-INF\src建立新文件HiServlet.java,内容详见附1.
2.在WEB-INF建立web.xml,内容详见附2.
3.编译project,启动Tomcat.
4.http://localhost:8080/MyTomcatProject/servlet/HiServlet
5.看见“Hi,Servlet!”,OK.
附1:HiServlet.java.
==================
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HiServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
String title = "Hi,Servlet!";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<h1>" + title + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
附2:web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>HiServlet</servlet-name>
<servlet-class>HiServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HiServlet</servlet-name>
<url-pattern>/servlet/HiServlet</url-pattern>
</servlet-mapping>
<servlet>
</web-app>