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

1 使用IDEA搭建MAVEN项目

1.1 搭建java项目

(1)创建java项目

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

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

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

(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)调整项目结构

调整后的项目结构如下:

(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)项目结构

1.4 运行web应用

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

1.5 代码git提交

略。参看随笔:

2017.12.12 架构探险-第一章-从一个简单的web应用开始的更多相关文章

  1. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第一章:向量代数

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第一章:向量代数 学习目标: 学习如何使用几何学和数字描述 Vecto ...

  2. 第一章 第一个spring boot程序(转载)

    第一章 第一个spring boot程序 本编博客转发自:http://www.cnblogs.com/java-zhao/p/5324185.html   环境: jdk:1.8.0_73 mave ...

  3. jQuery系列 第一章 jQuery框架简单介绍

    第一章 jQuery框架简单介绍 1.1 jQuery简介 jQuery是一款优秀的javaScript库(框架),该框架凭借简洁的语法和跨平台的兼容性,极大的简化了开发人员对HTML文档,DOM,事 ...

  4. C#语言————第一章 第一个C#程序

    第一章    第一个C#程序 ******************C#程序***************     ①:建立项目:文件-->新建-->项目-->c#-->控制台程 ...

  5. 《深度解析Tomcat》 第一章 一个简单的Web服务器

    本章介绍Java Web服务器是如何运行的.从中可以知道Tomcat是如何工作的. 基于Java的Web服务器会使用java.net.Socket类和java.net.ServerSocket类这两个 ...

  6. Unity 2D游戏开发高速入门第1章创建一个简单的2D游戏

    Unity 2D游戏开发高速入门第1章创建一个简单的2D游戏 即使是如今,非常多初学游戏开发的同学.在谈到Unity的时候.依旧会觉得Unity仅仅能用于制作3D游戏的. 实际上.Unity在2013 ...

  7. 2017.2.20 activiti实战--第一章--认识Activiti

    学习资料:<Activiti实战> 第一章 认识Activiti 内容概览:讲解activiti的特点.接口概览.架构等基本信息. 1.3 Activiti的特点 1.使用mybatis ...

  8. 第一章 第一个spring boot程序

    环境: jdk:1.8.0_73 maven:3.3.9 spring-boot:1.2.5.RELEASE(在pom.xml中指定了) 注意:关于spring-boot的支持, 最少使用jdk7(j ...

  9. C#第一章 第一个C#程序

    第一个C#程序 namespace 是C#中组织代码的方式,它的作用那个类似java中的包 using 在Java中作用如果导入其他包 应该是用import关键字而在C#中应使用using关键字来引用 ...

随机推荐

  1. Java之IO流的关闭

    1.在finally中关闭流: OutputStream out = null; try { out = new FileOutputStream(""); // ...操作流代码 ...

  2. thinkphp的where方法的使用

    1.Thinkphp中where()条件的使用 总是有人觉得,thinkphp的where()就是写我要进行增加.查询.修改.删除数据的条件,很简单的,其实我想告诉你,where()是写条件语句的,但 ...

  3. 数学【CF743C】Vladik and fractions

    Description 请找出一组合法的解使得\(\frac {1}{x} + \frac{1}{y} + \frac {1}{z} = \frac {2}{n}\)成立 其中\(x,y,z\)为正整 ...

  4. 【Leetcode】264. Ugly Number II ,丑数

    原题 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime facto ...

  5. 排序小记【2】对 struct 的排序

    有了前面的内容,对于一般的排序已经没有问题了,但是有时候排序的要求可能会有点刁... 举个简单的例子,应该是NOIP2009的分数线划定,差不多算是一个比较高级的排序(吧). 多关键字排序(?) 我一 ...

  6. 【构造】AtCoder Regular Contest 079 F - Namori Grundy

    对每个点的取值都取最小的可能值. 那个图最多一个环,非环的点的取值很容易唯一确定. 对于环上的点v,其最小可能取值要么是mex{c1,c2,...,ck}(ci这些是v直接相连的非环点)(mex是). ...

  7. Redis源码解析之ziplist

    Ziplist是用字符串来实现的双向链表,对于容量较小的键值对,为其创建一个结构复杂的哈希表太浪费内存,所以redis 创建了ziplist来存放这些键值对,这可以减少存放节点指针的空间,因此它被用来 ...

  8. python基础之条件判断和循环

    1.条件判断 age = 3 if age >= 18: print('adult') elif age >= 6: print('teenager') else: print('kid' ...

  9. [转]Spring Security学习总结一

    [总结-含源码]Spring Security学习总结一(补命名空间配置) Posted on 2008-08-20 10:25 tangtb 阅读(43111) 评论(27)  编辑  收藏 所属分 ...

  10. opensuse搜狐镜像

    openSUSE-13.1-Update-sohu-mirror http://mirrors.sohu.com/opensuse/update/13.1/ openSUSE-13.1-Oss-soh ...