测试servlet例子
File->New->New Project(java Tomcat Project)
输入 test next 再输入 test 最后finish
右键test工程,new class 包名test,类名test
超类的名称写上javax.servlet.http.HttpServlet
打开test.java ,右键test.java文件内容 source->Override/Implement Methods
选上 doGet doPost
去掉基类调用super.doGet(req, resp);
输入
view plain
PrintWriter out = new PrintWriter (resp.getOutputStream());
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body>hello world!");
out.println("</body></html>");
out.close();
右击PrintWriter,source->Organize Import
然后右击test工程,选择Properties->java build path->order and export 全选然确定
创建该工程的部署文件web.xml 放到test/Web-INF目录下
view plain
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>test.test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
启动tomcat
访问 http://localhost:8080/test/test,这里注意tomcat端口问题