STEP 9: Create and execute a
Servlet
|
1. Select File > New > Project. Then New Project window will pop up, select Tomcat Project then click Next button.
2. In the New Tomcat Project window, input the project name, here use "ServletDemo",then click Next button.
3. To make it simple, keep this as default and click Finish button.
4.Select WEB-INF/src in workspace and right click it and select New > File.
5.In the pop up window New File, input file name "SimpleServlet.java" and click Finish button.
6. Input the following code in to the file SimpleServlet.java and click Save button in toolbar.
import java.io.PrintWriter; //import javax.servlet.*;
|
7. Select WEB-INF/ in workspace and right click it and select New > File.
8. A New File window will pop up and input the file name " web.xml" and click Finish button.
9. Input the following content that in the table
<?xml version="1.0" encoding="UTF-8"?> <!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>SimpleServlet</servlet-name> <servlet-class>SimpleServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SimpleServlet</servlet-name> <url-pattern>/SimpleServlet</url-pattern> </servlet-mapping> </web-app> |
into the file web.xml, for this machine it pop up a Notepad window when Finish button clicked in step 7. So copy the content in above table into this Notepad window, then save it.
10. Press the Start Tomcat button in tool bar, after tomcat starts, open a browser and input the URL: http://localhost:8080/ServletDemo/SimpleServlet . The result will show as below: