企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
1.在src下创建java文件HelloWorld.java ![](https://box.kancloud.cn/7fb44615e0b709956136ac4a4e8b2ea2_1197x586.png) ~~~ package zyw.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HelloWorld extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("接收到get请求"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("接收到post请求"); } } ~~~ 快捷键 ~~~ sout =>System.out.println(); ~~~ 2.在web》WEB=INF下的web.xml配置servlet信息 ![](https://box.kancloud.cn/75a1437832e2b968b769bb41194f77bc_1186x528.png) ~~~ <?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"> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>zyw.servlet.HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hi</url-pattern> </servlet-mapping> </web-app> ~~~ 3.为了让访问路径加个层,在tomcat配置的时候加个层 ![](https://box.kancloud.cn/46f9cb35db190341b40a77b74d9ed42f_1091x352.png) 4.启动项目一起正常 ![](https://box.kancloud.cn/170c270c5056045227e718f63767bcc0_777x148.png) 5.在路径上加上hi后访问 ![](https://box.kancloud.cn/8bdcaf868087da4b2a78e92bbc867cd7_529x53.png) 可在IDEA中看到信息 ![](https://box.kancloud.cn/c6340a64d25dfe334cb200518de65710_1303x460.png)