欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IDEA使用Maven创建JavaWeb入门项目

程序员文章站 2022-07-12 22:20:37
...

主要内容

使用IDEA通过Maven方式创建JavaWeb项目,然后以简单的index.html和index.jsp页面请求为例,携带参数访问基于注解的HttpServlet,解决乱码问题并返回结果

具体步骤

1. 选择Maven并创建webapp
IDEA使用Maven创建JavaWeb入门项目

2. 指定项目名称、项目路径

IDEA使用Maven创建JavaWeb入门项目

3. 首次使用需要指定maven安装包的配置文件,以及本地仓库
IDEA使用Maven创建JavaWeb入门项目

4. src/main目录中创建java文件夹并编写HelloServlet源文件,在webapp目录中创建index.htmlindex.jsp用于测试,lib目录是可选的,在pom.xml中补充配置
IDEA使用Maven创建JavaWeb入门项目

5. 配置pom.xml

<?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>org.example</groupId>
    <artifactId>HelloMaven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>HelloMaven Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <!--tomcat版本-->
        <vtomcat>7.0.47</vtomcat>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jsp-api</artifactId>
            <version>${vtomcat}</version>
            <scope>provided</scope>
        </dependency>
        <!--HttpServlet支持-->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>${vtomcat}</version>
            <scope>provided</scope>
        </dependency>
        <!--用于支持-HttpServlet注解-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>HelloMaven</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!--插件列表 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <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>
                                <url>http://localhost:8080/manager/text</url>
                                <username>tomcat</username>
                                <password>tomcat</password>
                              </configuration>
					-->
                </plugin>
                <!--以下为自动生成的-->
                <!--        <plugin>-->
                <!--          <artifactId>maven-compiler-plugin</artifactId>-->
                <!--          <version>3.8.0</version>-->
                <!--        </plugin>-->
                <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-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>

6. 查看web.xml

<?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" metadata-complete="false">
  <!--
  1. 项目使用annotation声明的servlet或者filter,那么metadata-complete需要声明为false ,不写默认false
  2. 显式设置为true时,只会扫描当前文件中的配置,不再扫描文件所在jar中声明的配置 
-->
  <display-name>Archetype Created Web Application</display-name>
</web-app>

7. 添加index.jspindex.html

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2020/7/16
  Time: 19:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
</body>
<h2>Hello World!</h2>
<%  String ss="index.jsp的请求";
    out.println(ss);
%>
<a href="javascript:location.href=encodeURI('/HelloMaven/hello?msg=chinese中文字符串乱码测试')">发送请求到servlet</a>
</body>
</html>

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>index.html页面请求</h2>
</body>
<a href="/HelloMaven/hello?msg=chinese中文字符串乱码测试">方式1:发送参数请求到servlet</a>
<br>
<a href="javascript:location.href=encodeURI('/HelloMaven/hello?msg=chinese中文字符串乱码测试')">方式2:发送参数请求到servlet</a>
</body>
</html>

8. 编写HelloServlet.java

package com.test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

//@WebServlet("/hello")
//请求映射必须是以"/"开头,下面与上等价
//@WebServlet(name="HelloServlet",value="/hello")
@WebServlet(urlPatterns = "/hello",name="xxx")
public class HelloServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String msg = request.getParameter("msg");
        System.out.println("获取参数方式1:"+msg);
        msg = URLDecoder.decode(request.getParameter("msg"),"utf-8");
        System.out.println("解码之后:"+msg);
//        jsp页面虽然设置了utf-8编码,但传输的过程中使用的编码是:ISO-8859-1
        String msg2 = new String(request.getParameter("msg").getBytes("ISO-8859-1"),"utf-8");
        System.out.println("获取参数方式2:"+msg2);
//        获取参数方式2:chinese中文字符串乱码测试

        response.setCharacterEncoding("utf-8");

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        response.getWriter().println("<h2 style=\"color:orange\">请求成功,显示参数:"+msg2+"</h2>");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

9. 输入mvn tomcat7:run后回车

IDEA使用Maven创建JavaWeb入门项目

10. 出现以下结果说明可以在浏览器打开,点击生成的蓝色链接即可跳转到浏览器运行

IDEA使用Maven创建JavaWeb入门项目

11. 点击链接或直接在输入访问的url可发起请求到HelloServlet处理

IDEA使用Maven创建JavaWeb入门项目

12. 点击后返回的结果

IDEA使用Maven创建JavaWeb入门项目

13. HelloServlet输出信息

IDEA使用Maven创建JavaWeb入门项目

14. 问题及解决摘要

【1】问题:Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on project Hello_2: Could not start Tomcat

解决:复用了上一个项目的pom.xml文件导致,需要HelloMaven`及其相关配置

【2】问题:Error:(4,32) java: 程序包javax.servlet.annotation不存在

解决:没有导入HttpServlet的注解依赖,可导入以下依赖,经测试导入<version>2.5</version>也是可以的

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

【3】问题:Cannot access aliyun (http://maven.aliyun.com/nexus/content/groups/public/) in offline mode and the artifact org.apache.maven.surefire:surefire-booter:pom:2.12.4 has not been downloaded from it before.

解决:File——>Settings,设置Work offline

IDEA使用Maven创建JavaWeb入门项目

相关标签: Java maven