1.在Myecplise下new一个新的web project
2.在新建包里建立一个MySevlet类:
public class MyServlte extends HttpServlet { protected void service(HttpServletRequest req,HttpServletResponse resq) throws IOException { resq.getWriter().write("this is my first servlet"); System.out.println("this is my first servlet"); } } |
3.在WEB-INF下编写web.xml文件:
<servlet> <servlet-name>my</servlet-name> <servlet-class>servlet.MyServlte</servlet-class> </servlet> <!--配置访问方式--> <servlet-mapping> <servlet-name>my</servlet-name> <url-pattern>/my</url-pattern> </servlet-mapping> |
4.在tomcat中的webapps目录下新建一个目录为project,将源码中的WebRoot目录下的文件全部拷贝进project目录中
5.运行tomcat,在浏览器中输入localhost:8080/project/my ,查看运行结果