💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
### **3.4.1需求** 创建一个web工程,实现入门程序的功能。 1)添加index.jsp,输出hello world 2)添加一个servlet转发到jsp页面。 第一步:创建maven工程 ![](https://box.kancloud.cn/c96146d2199a7a3d0c74341f60320514_806x714.png) ![](https://box.kancloud.cn/833be518931350d25cdc5c6a3f7810b3_807x149.png) 添加webapp文件夹 ![](https://box.kancloud.cn/03cc8e6d04d3027f99348dee0626ccee_719x279.png) ![](https://box.kancloud.cn/c4051cf454d18f6a7a44f237ba92b687_283x332.png) ![](https://box.kancloud.cn/58c9ad9559383033b31e00bfb7b1f450_458x566.png) ![](https://box.kancloud.cn/8221eb2adb0c9b3e63eae2e1dd960342_878x630.png) ![](https://box.kancloud.cn/619465c98b2516c91569bfa2dda354d4_978x518.png) 第二步:pom文件添加依赖 ~~~ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.li</groupId> <artifactId>web</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.14.v20181114</version> <configuration> <httpConnector> <port>8080</port> </httpConnector> <webApp> <contextPath>/my</contextPath> </webApp> </configuration> </plugin> </plugins> </build> </project> ~~~ 3.web.xml里内容 ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class>cn.li.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ~~~ 4.MyServlet里面内容 ~~~ public class MyServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setAttribute("msg","hello"); req.getRequestDispatcher("/index.jsp").forward(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } } ~~~ 5.编写jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <title>Insert title here</title> </head> <body> ${msg} </body> </html> ~~~ 6.运行 ①Run-- ![](https://box.kancloud.cn/763800386d8c6982393f3c2833b09ba5_1063x633.png) ![](https://box.kancloud.cn/138e4d75638e69c710aa0eb109a61459_586x142.png) ②tomcat插件运行 pom.xml文件中加入 ~~~ <build> <!--插件位置--> <plugins> <plugin> <!--tomcat7配置--> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <!-- or if you want to use tomcat 6.x <artifactId>tomcat6-maven-plugin</artifactId> --> <version>2.2</version> <configuration> <port>8080</port> <path>/</path> </configuration> </plugin> </plugins> </build> ~~~ 然后按下图运行 ![](https://box.kancloud.cn/8a6fd2e223abe5267654e3b59ccd9659_360x406.png) ③jetty服务器运行 ~~~ <build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.14.v20181114</version> <configuration> <httpConnector> <port>8080</port> </httpConnector> <webApp> <contextPath>/my</contextPath> </webApp> </configuration> </plugin> </plugins> </build> ~~~ 和tomcat7同理运行 ### 补充:如果页面出现${msg} 解决:1.~~~ //jsp页面加 <%@ isELIgnored="false"%> ~~~ 2.web.xml开头换成3.1版本 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> ~~~ 补充:maven官网:http://maven.apache.org/ tomcat插件:https://tomcat.apache.org/maven-plugin-2.2/run-mojo-features.html