1.创建一个空白的maven项目servlet,作为父工程,以后就在这个父工程中创建module,删除src,导入依赖
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies>
2.右击父项目servlet创建一个module,servlet_helloServlet
创建一个类
public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("run HelloServlet.doGet()..."); PrintWriter writer = resp.getWriter(); writer.print("hello servlet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
3.修改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true"> <servlet> <servlet-name>helloServlet</servlet-name> <servlet-class>com.cong.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
4.配置tomcat,运行,进入首页
5.访问我们写的类的doGet()方法,打印了hello servlet
原文地址:https://www.cnblogs.com/ccoonngg/p/11879684.html
时间: 2024-11-09 20:57:02