使用idea创建JavaWeb项目

最近在复习 Java Web 的知识,以前学的时候使用的是 Ecplise, 现在使用 Idea,所以在此记录一下如果使用 Idea 创建 Java Web 项目,已经常见问题的解决方案

创建 Java Web 项目

开始创建项目

  

https://image.coderlab.cn/preview/1438622828243992577

  

https://image.coderlab.cn/preview/1438622835235897345

创建好的文件结构如下:

  

https://image.coderlab.cn/preview/1438622996368474113

配置 Java Web 项目

创建 classes 和 lib 文件夹,创建位置如下图所示

  

https://image.coderlab.cn/preview/1438623026026397698

配置 Project Structure ,操作流程如下

File –> Project Structure…, > 2. 进入 Project Structure 窗口 > 3. 点击 Modules –> 选中项目“Java-Web-demo” –> 切换到 Paths 选项卡 –> 勾选 “Use module compile output path”,将 “Output path” 和 “Test output path” 都改为之前创建的 classes 目录

  

https://image.coderlab.cn/preview/1438623039410421761

配置放入的 lib 的 Jar

点击 Modules –> 选中项目“Java-Web-demo” –> 切换到 Dependencies 选项卡 –> 点击下边的“+”,选择 “JARs or directories…”,选择创建的 lib 目录

  

https://image.coderlab.cn/preview/1438623055592046593

配置打包方式 Artifacts

点击 Artifacts 选项卡,IDEA 会为该项目自动创建一个名为“JavaWeb:war exploded”的打包方式,表示 打包成 war 包,并且是文件展开性的,输出路径为当前项目下的 out 文件夹,保持默认即可。勾选“Show content of elements”,表示显示详细的内容列表。

  

https://image.coderlab.cn/preview/1438623067264794625

配置 Tomcat

Run -> Edit Configurations,进入“Run Configurations”窗口,点击“+”-> Tomcat Server -> Local,创建一个新的 Tomcat 容器 > 2. 在“Name”处输入新的服务名,点击“Application server”后面的“Configure…”,弹出 Tomcat Server 窗口,选择本地安装的 Tomcat 目录 -> OK > 3. 在“Run Configurations”窗口的“Server”选项板中,去掉勾选“After launch”,设置“HTTP port”、“JMX port”、”VM options“,点击 Apply -> OK,至此 Tomcat 配置完成。

  

https://image.coderlab.cn/preview/1438622299363229698

  

https://image.coderlab.cn/preview/1438623121404870658

JavaWeb 测试

  • 打开 index.jsp,写下下面语句
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<%-- Created by IntelliJ IDEA. User: gclm Date: 2019-02-20 Time: 15:04 To change
this template use File | Settings | File Templates. --%> <%@ page
contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>Java Web 测试项目</title>
  </head>
  <body>
    <%--$END$--%>
    <h1>Hello Wrold!!!!</h1>
  </body>
</html>
  • 启动 Tomcat

Run -> Edit Configurations,进入“Run Configurations”窗口,选择之前配置好的 Tomcat,点击“Deployment”选项卡,点击“+” -> “Artifact”-> 选择创建的 web 项目的 Artifact…修改“Application context”-> Apply -> OK

说明:此处的 Application context 是指定本工程的根目录

4.2 在 index.jsp 文件中的 body 之间添加要显示的内容,然后点击“运行”的绿色三角 打开浏览器,输入:localhost:8080

  

https://image.coderlab.cn/preview/1438623140778360833

  

https://image.coderlab.cn/preview/1438623151738077186

Servlet 编写与配置

  • 编写 Servlet

    创建 HelloServlet 文件继承 HttpServlet

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    package club.gclmit.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Date;
    
    /**
     * Copyright (C), 2016-2018, 孤城落寞的博客
     *
     * @program: club.gclmit.servlet
     * @ gclm
     * @date: 2019-02-20 15:22
     * @
     */
    public class HelloServlet extends HttpServlet {
    
        String message = null;
        @Override
        public void init() throws ServletException {
            System.out.println("XXXXX 开始初始化 Servlet");
            message = "Hello Servlet,这是我的第一个Servlet 程序";
        }
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("Servlet 开始执行 Get 请求!!!!");
            /**
             * 设置请求头,告诉浏览器响应的内容是用 UTF-8 编码的 Html
             *
             */
            response.setHeader("Content-type","text/html;charset=UTF-8");
            /**
             *  告诉 Servlet 用UTF-8编码。而不是默认的 IS08859
             */
            response.setCharacterEncoding("UTF-8");
            /**
             *  告诉 Servlet 返回的内容是 Html
             */
            response.setContentType("text/html");
            response.getWriter().println("<h1>"+message+"</h1>");
            response.getWriter().println(new Date().getTime());
            System.out.println("Servlet 响应数据完成!!!!");
        }
    
        @Override
        public void destroy() {
            super.destroy();
            System.out.println("开始销毁 Servlet");
        }
    }
    
  • 配置 Servlet,下面两种方法任选其一。

    • 方法一:在 WEB-INF 目录下 web.xml 文件的标签中添加如下内容:

    https://image.coderlab.cn/preview/1438623164459401218

    img

    • 方法二:在 HelloServlet 文件的类前面加上:@WebServlet(“/hello”)

    https://image.coderlab.cn/preview/1438623172512464898

    img

  • 运行 servlet

    点击运行按钮

    https://image.coderlab.cn/preview/1438623180288704514

  • 运行效果

    https://image.coderlab.cn/preview/1438623192494129154

常见问题

1. 控制台乱码

  解决方案:

右上角 Edit Configurations 的 VM options 处添加 -Dfile.encoding=UTF-8

2. HttpServletResponse 输出的中文乱码问题

  解决方案: 看我另外一篇博客

3. org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]

  解决方案: 看我另外一篇博客