填写Module的项目信息,如下:
image-20201213145308747配置 Maven 设置:
image-20201213145357863配置Module的存储路径:
image-20201213145653588可以从目录结构来看,生成的目录结构缺少,需要手动配置一下工程目录。
创建 java 源码路径:
image-20201213150051596创建 resources 配置文件夹:
image-20201213150227545创建 test 单元测试文件夹:
image-20201213150357332<?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>com.lijw</groupId> <artifactId>javaweb_demo_01</artifactId> <version>1.0-SNAPSHOT</version> <!-- 设置打包方式 --> <packaging>war</packaging> <!-- 项目名称以及服务url --> <name>javaweb_demo_01 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <!-- 项目属性,设置jdk 以及 编码 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <!-- 项目依赖 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>javaweb_demo_01</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
配置 tomcat 部署 javaweb 工程,如下:
使用骨架创建的 javaweb 工程,自动创建了 index.jsp 页面,我们可以测试访问:
image-20201213151722364image-20201213151738704上面是使用骨架来创建工程的,如果不使用骨架,怎样创建工程呢?
<!-- 设置打包方式 --> <packaging>war</packaging>
浏览器访问如下:
image-20201213154929582首先搜索一下 Servlet 的 Maven 坐标,如下:
访问 https://mvnrepository.com/ 搜索 servlet 即可。
image-20201213155713092将依赖拷贝到 pom.xml 中,如下:
image-20201213155759065@WebServlet("/demo2") public class HelloServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("hello java web demo 2"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }