idea创建基于maven的javaweb项目入门

主要内容

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

具体步骤

1. 选择Maven并创建webapp
在这里插入图片描述

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

在这里插入图片描述

3. 首次使用需要指定maven安装包的配置文件,以及本地仓库
在这里插入图片描述

4. src/main目录中创建java文件夹并编写HelloServlet源文件,在webapp目录中创建index.htmlindex.jsp用于测试,lib目录是可选的,在pom.xml中补充配置
在这里插入图片描述

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后回车

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

12. 点击后返回的结果

在这里插入图片描述

13. HelloServlet输出信息

在这里插入图片描述

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

在这里插入图片描述

  • 11
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值