環境
IDEA JDK1.8 Maven servlet-api:2.5 tomcat:9.0.0.M9html
構建java
IDEA上新建一個Maven項目
web
添加pom依賴api
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency>
編寫繼承HttpServlet
的實現tomcat
@Override public void init() throws ServletException { System.out.println("初始化"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.print("<h1>hello,World</h1>"); } @Override public void destroy() { System.out.println("銷燬"); }
配置web.xmlapp
<web-app> <servlet> <servlet-name>hello_world</servlet-name> <servlet-class>com.teemo.blog.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello_world</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
項目結構圖
ide
運行url
IDEA配置Tomcat
code
運行
xml
最終結果