2017.12.12 架構探險-第一章-從一個簡單的web應用開始

參考來自:《架構探險》黃勇 著html

 

1 使用IDEA搭建MAVEN項目

1.1 搭建java項目

(1)建立java項目

爲了整個書籍的項目,我建立了一個工程,在這個工程裏建立了每一個章節的module。建立過程見隨筆《待定》。java

建立完成後,項目結構以下:git

ps:對maven項目而言,classpath是java和resources兩個根目錄。web

 

(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)調整項目結構

調整後的項目結構以下:apache

 

(2)web.xml

這裏使用Servlet3.0框架。其實Servlet3.0框架能夠不使用web.xml,直接用註解配置便可。因此這裏在後面也沒有配置servlet的信息,直接在類上里加了註解@WebServlet。api

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轉發。tomcat

 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提交

略。參看隨筆:app

相關文章
相關標籤/搜索