2017.12.12 架构探险-第一章-从一个简单的web应用开始

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

1 使用IDEA搭建MAVEN项目

1.1 搭建java项目

(1)创建java项目

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

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

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

2017.12.12 架构探险-第一章-从一个简单的web应用开始

(2)调整pom配置
  • 统一源代码的编码方式
  • 统一源代码和编译输出所用的JDK版本
  • 打包时跳过单元测试(可选)
 <?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.smart4j</groupId>
<artifactId>chapter1</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<!--编码方式-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <build>
<plugins>
<!--编译-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!--测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
</plugins>
</build> </project>

pom.xml

1.2 将java项目转换成web项目

(1)调整项目结构

调整后的项目结构如下:

2017.12.12 架构探险-第一章-从一个简单的web应用开始

(2)web.xml

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

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

web.xml

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

  • Servlet
  • JSP
  • JSTL
  • 打war包
 <?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.smart4j</groupId>
<artifactId>chapter1</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<!--编码方式-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <packaging>war</packaging> <build>
<plugins>
<!--编译-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!--测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
</plugins>
</build> <dependencies>
<dependency>
<!--servlet-->
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!--provided表示只参与编译,不参与打包,因为tomcat自带了这个jar包-->
<scope>provided</scope>
</dependency> <!--jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<!--provided表示只参与编译,不参与打包,因为tomcat自带了这个jar包-->
<scope>provided</scope>
</dependency> <!--JSTL-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<!--runtime表示只参与运行,不参与编译-->
<scope>runtime</scope>
</dependency>
</dependencies>
</project>

pom.xml

1.3 编写简单的web应用

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

(1)Servlet
 package org.smart4j.chapter1;

 import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebServlet("/hello")
public class HelloServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String currentTime = dateFormat.format(new Date()); req.setAttribute("currentTime",currentTime);
req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req,resp);
}
}
(2)JSP页面

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

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello</title>
</head> <body>
<h1>lyh</h1>
<h2>${currentTime}</h2>
</body>
</html>

hello.jsp

(3)项目结构

2017.12.12 架构探险-第一章-从一个简单的web应用开始

1.4 运行web应用

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

2017.12.12 架构探险-第一章-从一个简单的web应用开始

1.5 代码git提交

略。参看随笔:

2017.12.12 架构探险-第一章-从一个简单的web应用开始

上一篇:jqgrid 设置隔行换色


下一篇:underscore.js源码研究(3)