文章目录
- IDEA开发maven项目
- 依赖范围
IDEA开发maven项目
- 点击NewProject,填写项目名字Name为javaWeb-maven,填写项目的存储地址,选择Archetype为org.apache.maven.archetypes:maven-archetype-webapp,然后再点击Create,等待maven编译完成。
- 点击File之后选择Settings。
- 选择Build、Execution、Deployment,然后选择Maven,输入对应的maven的本地路径、maven配置文件和maven仓库的地址。
- 项目创建成功如下图
- 右击main目录,点击New,点击Directory,创建一个java目录
- 在java目录下面创建一个com/hu/servlet目录,然后创建一个servlet程序HelloServlet
这里,我们可以看到无法解析到HttpServlet类。
- 在pom.xml中添加servlet坐标,点击小标来刷新maven加载对应的jar包或者点击maven的标之后点击刷新按钮。
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>javaWeb-maven</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>javaWeb-maven Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><build><finalName>javaWeb-maven</finalName></build>
</project>
- 这时我们再点击HelloServlet类,可以看到当前类并没有报错了。
9. 设置 jdk 编译版本,在pom.xml中添加以下代码
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>javaWeb-maven</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>javaWeb-maven Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><build><finalName>javaWeb-maven</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build>
</project>
- 在servlet包中的HelloServlet程序添加如下代码
package com.hu.servlet;import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;public class HelloServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Cookie cookie = new Cookie("username","root");resp.addCookie(cookie);req.getRequestDispatcher("hello.html").forward(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
- 在webapp下面创建一个html文件
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<span id="user-info"></span>
<script>function getCookie(name) {const value = `; ${document.cookie}`;const parts = value.split(`; ${name}=`);if (parts.length === 2) return parts.pop().split(';').shift();}document.addEventListener('DOMContentLoaded', function() {const username = getCookie('username'); // 假设你要获取名为 'username' 的 Cookieif (username) {document.getElementById('user-info').innerText = '欢迎,' + username + '!';} else {document.getElementById('user-info').innerText = '未找到用户信息';}});
</script>
</body>
</html>
- 配置对应servlet的访问路径
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><servlet><servlet-name>HelloServlet</servlet-name><servlet-class>com.hu.servlet.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>HelloServlet</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping>
</web-app>
- 在pom.xml文件中添加tomcat7插件。然后刷新maven。
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>javaWeb-maven</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>javaWeb-maven Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><build><finalName>javaWeb-maven</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>8080</port><path>/</path></configuration></plugin></plugins></build>
</project>
- 点击maven,点击Plugins,然后点击tomcat7,最后点击tomcat7:run。
- 项目启动成功后,访问对应的请求http://localhost:8080/hello,结果如下图
依赖范围
- compile:编译范围,指 A 在编译时依赖 B,此范围为默认依赖范围。编译范围的依赖会用在编译、测试、运行,由于运行时需要所以编译范围的依赖会被打包。
- provided:provided 依赖只有在当JDK 或者一个容器已提供该依赖之后才使用, provided 依赖在编译和测试时需要,在运行时不需要,比如:servlet api 被tomcat 容器提供。
- runtime:runtime 依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如:jdbc 的驱动包。由于运行时需要所以runtime 范围的依赖会被打包。
- test:test 范围依赖 在编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用, 比如:junit。由于运行时不需要所以test 范围依赖不会被打包。
- system:system 范围依赖与 provided 类似,但是你必须显式的提供一个对于本地系统中JAR
文件的路径,需要指定 systemPath 磁盘路径,system 依赖不推荐使用。