zoukankan      html  css  js  c++  java
  • 2017.12.12 架构探险-第一章-从一个简单的web应用开始

    参考来自:《架构探险》黄勇 著

    1 使用IDEA搭建MAVEN项目

    1.1 搭建java项目

    (1)创建java项目

    为了整个书籍的项目,我创建了一个工程,在这个工程里创建了每个章节的module。创建过程见随笔《待定》。

    创建完成后,项目结构如下:

    ps:对maven项目而言,classpath是java和resources两个根目录。

    (2)调整pom配置
    • 统一源代码的编码方式
    • 统一源代码和编译输出所用的JDK版本
    • 打包时跳过单元测试(可选)
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>org.smart4j</groupId>
     8     <artifactId>chapter1</artifactId>
     9     <version>1.0-SNAPSHOT</version>
    10 
    11     <properties>
    12         <!--编码方式-->
    13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    14     </properties>
    15 
    16     <build>
    17         <plugins>
    18             <!--编译-->
    19             <plugin>
    20                 <groupId>org.apache.maven.plugins</groupId>
    21                 <artifactId>maven-compiler-plugin</artifactId>
    22                 <version>3.3</version>
    23                 <configuration>
    24                     <source>1.7</source>
    25                     <target>1.7</target>
    26                 </configuration>
    27             </plugin>
    28             <!--测试-->
    29             <plugin>
    30                 <groupId>org.apache.maven.plugins</groupId>
    31                 <artifactId>maven-surefire-plugin</artifactId>
    32                 <version>2.18.1</version>
    33             </plugin>
    34         </plugins>
    35     </build>
    36 
    37 </project>
    pom.xml

     1.2 将java项目转换成web项目

    (1)调整项目结构

    调整后的项目结构如下:

    (2)web.xml

    这里使用Servlet3.0框架。其实Servlet3.0框架可以不使用web.xml,直接用注解配置即可。所以这里在后面也没有配置servlet的信息,直接在类上里加了注解@WebServlet。

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
    3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    5                            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    6          version="3.0">
    7        
    8 </web-app>
    web.xml

    (3)web项目所需要的依赖及属性配置

    • Servlet
    • JSP
    • JSTL
    • 打war包
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>org.smart4j</groupId>
     8     <artifactId>chapter1</artifactId>
     9     <version>1.0-SNAPSHOT</version>
    10 
    11     <properties>
    12         <!--编码方式-->
    13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    14     </properties>
    15 
    16     <packaging>war</packaging>
    17 
    18     <build>
    19         <plugins>
    20             <!--编译-->
    21             <plugin>
    22                 <groupId>org.apache.maven.plugins</groupId>
    23                 <artifactId>maven-compiler-plugin</artifactId>
    24                 <version>3.3</version>
    25                 <configuration>
    26                     <source>1.7</source>
    27                     <target>1.7</target>
    28                 </configuration>
    29             </plugin>
    30             <!--测试-->
    31             <plugin>
    32                 <groupId>org.apache.maven.plugins</groupId>
    33                 <artifactId>maven-surefire-plugin</artifactId>
    34                 <version>2.18.1</version>
    35             </plugin>
    36         </plugins>
    37     </build>
    38 
    39     <dependencies>
    40         <dependency>
    41             <!--servlet-->
    42             <groupId>javax.servlet</groupId>
    43             <artifactId>javax.servlet-api</artifactId>
    44             <version>3.1.0</version>
    45             <!--provided表示只参与编译,不参与打包,因为tomcat自带了这个jar包-->
    46             <scope>provided</scope>
    47         </dependency>
    48 
    49         <!--jsp-->
    50         <dependency>
    51             <groupId>javax.servlet.jsp</groupId>
    52             <artifactId>jsp-api</artifactId>
    53             <version>2.2</version>
    54             <!--provided表示只参与编译,不参与打包,因为tomcat自带了这个jar包-->
    55             <scope>provided</scope>
    56         </dependency>
    57 
    58         <!--JSTL-->
    59         <dependency>
    60             <groupId>javax.servlet</groupId>
    61             <artifactId>jstl</artifactId>
    62             <version>1.2</version>
    63             <!--runtime表示只参与运行,不参与编译-->
    64             <scope>runtime</scope>
    65         </dependency>
    66     </dependencies>
    67 </project>
    pom.xml

    1.3 编写简单的web应用

    需求很简单:有一个HelloServlet,接收GET /hello请求,转发至WEB-INF/jsp/hello.jsp页面,在hello.jsp页面上显示系统当前时间。

    (1)Servlet
     1 package org.smart4j.chapter1;
     2 
     3 import java.io.IOException;
     4 import java.text.DateFormat;
     5 import java.text.SimpleDateFormat;
     6 import java.util.Date;
     7 
     8 import javax.servlet.ServletException;
     9 import javax.servlet.annotation.WebServlet;
    10 import javax.servlet.http.HttpServlet;
    11 import javax.servlet.http.HttpServletRequest;
    12 import javax.servlet.http.HttpServletResponse;
    13 
    14 @WebServlet("/hello")
    15 public class HelloServlet extends HttpServlet{
    16     @Override
    17     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    18             throws ServletException, IOException {
    19         DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    20         String currentTime = dateFormat.format(new Date());
    21 
    22         req.setAttribute("currentTime",currentTime);
    23         req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
    24     }
    25 }
    View Code
    (2)JSP页面

    推荐将jsp文件放在WEB-INF文件夹内部,而非外部。因为用户无法通过浏览器直接访问放在WEB-INF文件夹内部的jsp文件,必须经过Servlet转发。

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <html>
     3 <head>
     4     <title>hello</title>
     5 </head>
     6 
     7 <body>
     8     <h1>lyh</h1>
     9     <h2>${currentTime}</h2>
    10 </body>
    11 </html>
    hello.jsp
    (3)项目结构

    1.4 运行web应用

    tomcat的配置和项目运行配置略。访问http://localhost:8080/chapter1/hello ,运行结果如下:

     

    1.5 代码git提交

    略。参看随笔:

  • 相关阅读:
    Unity给力插件之MegaFiers
    序列化存档之备忘脚本
    09 Spring Cloud的集群保护框架Hystrix
    08 在Spring Cloud中使用Feign
    07 REST客户端
    06 RestTemplate负载均衡
    05 第一个Ribbon程序
    04 Ribbon介绍
    01 在IDEA的同一目录下创建多个项目
    03 Eureka集群的搭建
  • 原文地址:https://www.cnblogs.com/lyh421/p/8027841.html
Copyright © 2011-2022 走看看