在本 Java 教程中,我们将帮助您了解编写基本 Java Web 应用程序的过程,该应用程序管理具有基本功能的书籍集合:列表、插入、更新、删除(或 CURD 操作 - 创建、更新、读取和删除)。该应用程序看起来像这样:
您将学习如何使用以下技术构建此应用程序:
- Java Servlet 和 Java 服务器页面 (JSP)
- JSP 标准标签库 (JSTL)
- Java 数据库连接 (JDBC)
- MySQL数据库
- Apache Tomcat 服务器
我们使用 NetBeans IDE 和 Maven 来开发项目。
1. 创建 MySQL 数据库
为简单起见,我们只有一张桌子。执行以下 MySQL 脚本以创建名为Bookstore 的数据库和名为Book的表:
CREATE DATABASE 'bookstore';
USE bookstore;
CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(128) NOT NULL,
`author` varchar(45) NOT NULL,
`price` float NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `title_UNIQUE` (`title`)
)
表书的结构如下:
您可以使用 MySQL 命令行客户端或 MySQL Workbench 工具来创建数据库。
2. 创建新的 Java Web maven应用程序项目
要创建新的 Java Web 应用程序项目,只需打开 Netbeans IDE,然后打开 File -> New Project。然后在 Categories 列中选择 Java with Maven,在 Projects 列中选择 Web Application。然后点击下一步。
给你的项目起个名字,我的是“SimpleWebApp”。将另一个字段保留为默认值。然后点击下一步。
为服务器和设置中的所有字段保留默认值,然后单击完成。
这将创建新的 java web 应用程序,其中包含用于构建 java web 应用程序的核心元素。
创建 Maven POM 文件需要输入信息,例如组 ID、工件 ID 等。然后在pom.xml 文件中添加以下依赖项:
<?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>com.example</groupId>
<artifactId>BookstoreMyBatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>BookstoreMyBatis</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
如您所见,这里的依赖项是针对 Servlet、JSP、JSTL 和 MySQL 连接器 Java(MySQL 的 JDBC 驱动程序)。
并且记得为项目创建一个 Java 包,这里我们使用包名com.example.bookstore。
3. 编写模型类
接下来,使用以下代码创建一个名为Book.java的 Java 类来对数据库中的图书实体进行建模:
package com.example.bookstore;
public class Book {
protected int id;
protected String title;
protected String author;
protected float price;
public Book() {
}
public Book(int id) {
this.id = id;
}
public Book(int id, String title, String author, float price) {
this(title, author, price);
this.id = id;
}
public Book(String title, String author, float price) {
this.title = title;
this.author = author;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
如您所见,该类根据数据库中表book中的 4 列有 4 个字段:id、title、author 和 price。
4.编码DAO类
接下来,我们需要实现一个数据访问层 (DAO) 类,该类为数据库中的表book提供 CRUD(创建、读取、更新、删除)操作。这是BookDAO类的完整源代码:
package com.example.bookstore;
import java.sql.SQLException;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
public class BookDAO {
public boolean insertBook(Book book) throws SQLException {
boolean rowInserted = false;
SqlSession session = null;
try {
SqlSessionFactory factory = ServiceLocator.getSessionFactory();
session = factory.openSession();
rowInserted = session.insert("insertBook", book) > 0;
session.commit();
} finally {
if (session != null) {
session.close();
}
}
return rowInserted;
}
public List<Book> listAllBooks() throws SQLException {
SqlSession session = null;
List<Book> retrieveList = null;
try {
SqlSessionFactory factory = ServiceLocator.getSessionFactory();
session = factory.openSession();
retrieveList = session.selectList("selectAllBooks");
} finally {
if (session != null) {
session.close();
}
}
return retrieveList;
}
public boolean deleteBook(Book book) throws SQLException {
boolean rowDeleted = false;
SqlSession session = null;
try {
SqlSessionFactory factory = ServiceLocator.getSessionFactory();
session = factory.openSession();
rowDeleted = session.delete("deleteById", book.getId()) > 0;
session.commit();
} finally {
if (session != null) {
session.close();
}
}
return rowDeleted;
}
public boolean updateBook(Book book) throws SQLException {
boolean rowUpdated = false;
SqlSession session = null;
try {
SqlSessionFactory factory = ServiceLocator.getSessionFactory();
session = factory.openSession();
rowUpdated = session.update("updateBook", book) > 0;
session.commit();
} finally {
if (session != null) {
session.close();
}
}
return rowUpdated;
}
public Book getBook(int id) throws SQLException {
SqlSession session = null;
Book book = null;
try {
SqlSessionFactory factory = ServiceLocator.getSessionFactory();
session = factory.openSession();
book = session.selectOne("selectBook", id);
} finally {
if (session != null) {
session.close();
}
}
return book;
}
}
ServiceLocator.java
package com.example.bookstore;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class ServiceLocator {
public static SqlSessionFactory getSessionFactory() {
InputStream inputStream = null;
SqlSessionFactory sqlSessionFactory = null;
try {
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException ex) {
Logger.getLogger(ServiceLocator.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException ex) {
Logger.getLogger(ServiceLocator.class.getName()).log(Level.WARNING, null, ex);
}
}
return sqlSessionFactory;
}
}
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="Book" type="com.example.bookstore.Book"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bookstoremybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="BookMapper.xml"/>
</mappers>
</configuration>
BookMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example">
<select id="selectAllBooks" resultType="Book">
select * from book
</select>
<select id="selectBook" parameterType="int" resultType="Book">
select * from book where id = #{id}
</select>
<insert id="insertBook" parameterType="Book" statementType="PREPARED" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
INSERT INTO book(title, author, price) VALUES
(#{title}, #{author}, #{price})
</insert>
<update id = "updateBook" parameterType = "Book">
UPDATE book SET title = #{title},
author = #{author},
price = #{price}
WHERE id = #{id};
</update>
<delete id = "deleteById" parameterType = "int">
DELETE from book WHERE id = #{id};
</delete>
</mapper>
如您所见,以下方法适用于 CRUD 操作:
- 创建:insertBook(Book) - 这会在表book中插入一个新行。
- 读取:listAllBooks() - 这会检索所有行;和getBook(id) - 根据主键值 (ID) 返回特定行。
- 更新:updateBook(Book) - 这会更新数据库中的现有行。
- 删除:deleteBook(Book) - 这会根据主键值 (ID) 删除数据库中的现有行。
5.编写图书列表JSP页面
接下来,创建一个 JSP 页面来显示数据库中的所有书籍。以下是项目中WebContent目录下BookList.jsp页面的代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Books Store Application</title>
</head>
<body>
<center>
<h1>Books Management</h1>
<h2>
<a href="new">Add New Book</a>
<a href="list">List All Books</a>
</h2>
</center>
<div align="center">
<table border="1" cellpadding="5">
<caption><h2>List of Books</h2></caption>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Price</th>
<th>Actions</th>
</tr>
<c:forEach var="book" items="${listBook}">
<tr>
<td><c:out value="${book.id}" /></td>
<td><c:out value="${book.title}" /></td>
<td><c:out value="${book.author}" /></td>
<td><c:out value="${book.price}" /></td>
<td>
<a href="edit?id=<c:out value='${book.id}' />">Edit</a>
<a href="delete?id=<c:out value='${book.id}' />">Delete</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
在这个 JSP 页面中,我们使用 JSTL 来显示来自数据库的表book的记录。listBook对象将从我们稍后创建的 servlet 传递。
运行时,此页面如下所示:
如您所见,在此页面的顶部菜单中有两个超链接,用于创建新书(添加新书)和显示所有书(列出所有书)。此外,对于每本书,都有两个用于编辑 ( Edit ) 和删除 ( Delete ) 的链接。
6.写书表单JSP页面
接下来,我们创建一个 JSP 页面来创建一本名为BookForm.jsp的新书。这是它的完整源代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Books Store Application</title>
</head>
<body>
<center>
<h1>Books Management</h1>
<h2>
<a href="new">Add New Book</a>
<a href="list">List All Books</a>
</h2>
</center>
<div align="center">
<c:if test="${book != null}">
<form action="update" method="post">
</c:if>
<c:if test="${book == null}">
<form action="insert" method="post">
</c:if>
<table border="1" cellpadding="5">
<caption>
<h2>
<c:if test="${book != null}">
Edit Book
</c:if>
<c:if test="${book == null}">
Add New Book
</c:if>
</h2>
</caption>
<c:if test="${book != null}">
<input type="hidden" name="id" value="<c:out value='${book.id}' />" />
</c:if>
<tr>
<th>Title: </th>
<td>
<input type="text" name="title" size="45"
value="<c:out value='${book.title}' />"
/>
</td>
</tr>
<tr>
<th>Author: </th>
<td>
<input type="text" name="author" size="45"
value="<c:out value='${book.author}' />"
/>
</td>
</tr>
<tr>
<th>Price: </th>
<td>
<input type="text" name="price" size="5"
value="<c:out value='${book.price}' />"
/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Save" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
此页面将用于创建新书和编辑现有书籍。在编辑模式下,servlet 将向请求传递一个 Book 对象,我们使用 JSTL 的<c:if>标记来确定该对象是否可用。如果可用(非空)表单处于编辑模式,否则处于创建模式。
运行时,此页面显示如下新表单:
在编辑模式下:
我们将在下一节中了解如何根据用户的请求将 DAO 类与 JSP 页面连接起来:创建 servlet 类。
7. 编码控制器Servlet类
现在,最困难但最有趣的部分是实现一个 Java Servlet,它充当页面控制器来处理来自客户端的所有请求。我们先来看代码:
package com.example.bookstore;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private BookDAO bookDAO;
public void init() {
bookDAO = new BookDAO();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getServletPath();
try {
switch (action) {
case "/new":
showNewForm(request, response);
break;
case "/insert":
insertBook(request, response);
break;
case "/delete":
deleteBook(request, response);
break;
case "/edit":
showEditForm(request, response);
break;
case "/update":
updateBook(request, response);
break;
default:
listBook(request, response);
break;
}
} catch (SQLException ex) {
throw new ServletException(ex);
}
}
private void listBook(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException, ServletException {
List<Book> listBook = bookDAO.listAllBooks();
request.setAttribute("listBook", listBook);
RequestDispatcher dispatcher = request.getRequestDispatcher("BookList.jsp");
dispatcher.forward(request, response);
}
private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("BookForm.jsp");
dispatcher.forward(request, response);
}
private void showEditForm(HttpServletRequest request, HttpServletResponse response)
throws SQLException, ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Book existingBook = bookDAO.getBook(id);
RequestDispatcher dispatcher = request.getRequestDispatcher("BookForm.jsp");
request.setAttribute("book", existingBook);
dispatcher.forward(request, response);
}
private void insertBook(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
String title = request.getParameter("title");
String author = request.getParameter("author");
float price = Float.parseFloat(request.getParameter("price"));
Book newBook = new Book(title, author, price);
bookDAO.insertBook(newBook);
response.sendRedirect("list");
}
private void updateBook(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String title = request.getParameter("title");
String author = request.getParameter("author");
float price = Float.parseFloat(request.getParameter("price"));
Book book = new Book(id, title, author, price);
bookDAO.updateBook(book);
response.sendRedirect("list");
}
private void deleteBook(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Book book = new Book(id);
bookDAO.deleteBook(book);
response.sendRedirect("list");
}
}
首先,查看init()方法,该方法在第一次实例化 servlet 时实例化BookDAO类的实例。
接下来,我们可以看到这个 servlet 同时处理GET和POST请求,因为doPost()方法调用了处理所有请求的doGet() :
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getServletPath();
try {
switch (action) {
case "/new":
showNewForm(request, response);
break;
case "/insert":
insertBook(request, response);
break;
case "/delete":
deleteBook(request, response);
break;
case "/edit":
showEditForm(request, response);
break;
case "/update":
updateBook(request, response);
break;
default:
listBook(request, response);
break;
}
} catch (SQLException ex) {
throw new ServletException(ex);
}
}
根据请求 URL(以 /edit、/list、/new 等开头),servlet 调用相应的方法。在这里,我们以一种方法为例:
private void listBook(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException, ServletException {
List<Book> listBook = bookDAO.listAllBooks();
request.setAttribute("listBook", listBook);
RequestDispatcher dispatcher = request.getRequestDispatcher("BookList.jsp");
dispatcher.forward(request, response);
}
该方法使用 DAO 类从数据库中检索所有书籍,然后转发到BookList.jsp页面以显示结果。其余方法也实现了类似的逻辑。
8.配置Web.xml
为了让ControllerServlet 拦截所有请求,我们必须在 web 部署描述符web.xml 文件中配置它的映射。打开WebContent\WEB-INF 目录下的 web.xml 文件并使用以下代码更新它:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Books Management Web Application</display-name>
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>com.example.bookstore.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/Error.jsp</location>
</error-page>
</web-app>
如您所见,< servlet>和<servlet-mapping>元素声明并指定ControllerServlet类的 URL 映射。URL 模式/表示这是处理所有请求的默认 servlet。
<error>页面元素为应用程序生命周期中可能发生的所有类型的异常( java.lang.Exception )指定错误处理页面。
9.编写错误JSP页面
这是Error.jsp页面的代码,它只显示异常消息:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Error</title>
</head>
<body>
<center>
<h1>Error</h1>
<h2><%=exception.getMessage() %><br/> </h2>
</center>
</body>
</html>
发生错误时看起来像这样:
10. 部署和测试应用程序
至此我们已经完成了项目的代码。是时候部署和测试应用程序以了解它是如何工作的了。
在 Web 浏览器中键入以下 URL 以访问 Bookstore 应用程序:
http://localhost:8080/bookstore
第一次,列表是空的,因为还没有任何书籍:
单击超链接添加新书开始添加新书:
输入图书信息(书名、作者和价格)并点击保存。应用程序保存图书并显示列表,如下图所示:
在此列表中,您可以单击编辑和删除超链接来编辑和删除特定书籍。
这就是使用 Servlet、JSP、JDBC 和 MySQL 构建简单的 Java Web 应用程序的方式。我们希望本教程对您有所帮助,您可以在下面的附件部分下载整个项目。