IDEA使用Maven创建JavaWeb入门项目
主要内容
使用IDEA通过Maven方式创建JavaWeb项目,然后以简单的index.html和index.jsp页面请求为例,携带参数访问基于注解的HttpServlet,解决乱码问题并返回结果
具体步骤
1. 选择Maven并创建webapp
2. 指定项目名称、项目路径
3. 首次使用需要指定maven安装包的配置文件,以及本地仓库
4. src/main
目录中创建java
文件夹并编写HelloServlet
源文件,在webapp
目录中创建index.html
或index.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.jsp
和index.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
推荐阅读
-
初次使用IDEA创建maven项目的教程
-
新版本IntelliJ IDEA 构建maven,并用Maven创建一个web项目(图文教程)
-
使用vue-cli创建项目的图文教程(新手入门篇)
-
荐 使用IDEA搭建一个简单的JavaWeb图书管理项目(详细步骤指导、提供源码)
-
eclipse弃坑记第一篇之在idea上配置Tomcat环境并创建Javaweb项目的详细步骤原创
-
IDEA中使用Maven模板创建Maven WebApp项目并使用Tomact来运行项目
-
IDEA 创建一个Mybatis Maven项目的方法步骤(图文)
-
使用iDea创建一个Springboot项目
-
在eclipse中使用Maven分模块搭建SSM框架,创建jar、war、pom工程模块教学,项目的热部署,需要的可以留下邮箱,给大家发整理过的Maven笔记
-
idea创建javaweb原生项目