项目管理工具-Maven-创建一个mavenweb项目

IDEA开发maven项目

  1. 点击NewProject,填写项目名字Name为javaWeb-maven,填写项目的存储地址,选择Archetype为org.apache.maven.archetypes:maven-archetype-webapp,然后再点击Create,等待maven编译完成。
    在这里插入图片描述
  2. 点击File之后选择Settings。
    在这里插入图片描述
  3. 选择Build、Execution、Deployment,然后选择Maven,输入对应的maven的本地路径、maven配置文件和maven仓库的地址。
    在这里插入图片描述
  4. 项目创建成功如下图
    在这里插入图片描述
    在这里插入图片描述
  5. 右击main目录,点击New,点击Directory,创建一个java目录
    在这里插入图片描述
  6. 在java目录下面创建一个com/hu/servlet目录,然后创建一个servlet程序HelloServlet
    在这里插入图片描述

这里,我们可以看到无法解析到HttpServlet类。

  1. 在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>
  1. 这时我们再点击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>

  1. 在servlet包中的HelloServlet程序添加如下代码
package com.hu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

public class HelloServlet extends HttpServlet {

    @Override
    protected 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);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
  1. 在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' 的 Cookie
        if (username) {
            document.getElementById('user-info').innerText = '欢迎,' + username + '!';
        } else {
            document.getElementById('user-info').innerText = '未找到用户信息';
        }
    });
</script>
</body>
</html>
  1. 配置对应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>

  1. 在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>
  1. 点击maven,点击Plugins,然后点击tomcat7,最后点击tomcat7:run。
    在这里插入图片描述
  2. 项目启动成功后,访问对应的请求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 依赖不推荐使用。
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿胡爱编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值