IDEA使用Maven创建JavaWeb入门项目

主要内容

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

具体步骤

1. 选择Maven并创建webapp
IDEA使用Maven创建JavaWeb入门项目_第1张图片

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

IDEA使用Maven创建JavaWeb入门项目_第2张图片

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

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

5. 配置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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>HelloMavenartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>

    <name>HelloMaven Maven Webappname>
    
    <url>http://www.example.comurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
        
        <vtomcat>7.0.47vtomcat>
    properties>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.apache.tomcatgroupId>
            <artifactId>tomcat-jsp-apiartifactId>
            <version>${vtomcat}version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>org.apache.tomcatgroupId>
            <artifactId>tomcat-servlet-apiartifactId>
            <version>${vtomcat}version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>

    dependencies>

    <build>
        <finalName>HelloMavenfinalName>
        <pluginManagement>
            <plugins>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.8.0version>
                    <configuration>
                        <source>1.8source>
                        <target>1.8target>
                        <encoding>UTF-8encoding>
                    configuration>
                plugin>
                <plugin>
                    <groupId>org.apache.tomcat.mavengroupId>
                    <artifactId>tomcat7-maven-pluginartifactId>
                    <version>2.2version>
                    
                plugin>
                
                
                
                
                
                <plugin>
                    <artifactId>maven-clean-pluginartifactId>
                    <version>3.1.0version>
                plugin>
                
                <plugin>
                    <artifactId>maven-resources-pluginartifactId>
                    <version>3.0.2version>
                plugin>
                <plugin>
                    <artifactId>maven-surefire-pluginartifactId>
                    <version>2.22.1version>
                plugin>
                <plugin>
                    <artifactId>maven-war-pluginartifactId>
                    <version>3.2.2version>
                plugin>
                <plugin>
                    <artifactId>maven-install-pluginartifactId>
                    <version>2.5.2version>
                plugin>
                <plugin>
                    <artifactId>maven-deploy-pluginartifactId>
                    <version>2.8.2version>
                plugin>
            plugins>
        pluginManagement>
    build>
project>

6. 查看web.xml


<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">
  
  <display-name>Archetype Created Web Applicationdisplay-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>Titletitle>
head>
body>
<h2>Hello World!h2>
<%  String ss="index.jsp的请求";
    out.println(ss);
%>
<a href="javascript:location.href=encodeURI('/HelloMaven/hello?msg=chinese中文字符串乱码测试')">发送请求到servleta>
body>
html>

index.html


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

请求成功,显示参数:"+msg2+"

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

9. 输入mvn tomcat7:run后回车

IDEA使用Maven创建JavaWeb入门项目_第5张图片

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

IDEA使用Maven创建JavaWeb入门项目_第6张图片

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

IDEA使用Maven创建JavaWeb入门项目_第7张图片

12. 点击后返回的结果

IDEA使用Maven创建JavaWeb入门项目_第8张图片

13. HelloServlet输出信息

IDEA使用Maven创建JavaWeb入门项目_第9张图片

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的注解依赖,可导入以下依赖,经测试导入2.5也是可以的

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        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入门项目_第10张图片

你可能感兴趣的:(Java,maven)