The book management system developed by jsp+Servlet+mysql

foreword

I am using eclipse, jdk1.8, Tomcat9, mysql8.0.18. This is my big assignment for dynamic web design. emmm record the experience and code. In fact, you can directly use jsp jump and javabean call to achieve the same function. It's just that the address accessed in that way will have .jsp. I don't think so, so I use Servlet.
Code download address: https://download.csdn.net/download/quanzhouren/12549852

data layer

Book class

This is the information used to encapsulate the Book

package com.entity;
public class Book {
    private int bookid;
    private String bookname;
    private String author;
    private String publish;
    private String type;
    public int getBookid() {
        return bookid;
    }
    public void setBookid(int bookid) {
        this.bookid = bookid;
    }
    public String getBookname() {
        return bookname;
    }
    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getPublish() {
        return publish;
    }
    public void setPublish(String publish) {
        this.publish = publish;
    }  
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
}

User class

Encapsulate user information

package com.entity;
public class User {
    private String name;
    private String password;
    private String telenumber;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getTel() {
        return telenumber;
    }
    public void setTel(String telenumber) {
        this.telenumber = telenumber;
    }
}

DataCon class

Mainly responsible for connecting to the database, I use the mysql-connector-java package. To separate two different connections, one selects the book table and the other selects the user table.

package com.DAO;

import java.sql.*;

public class DataCon {
	public static final String url = "jdbc:mysql://127.0.0.1:3306/library?useSSL=false&serverTimezone=UTC";
	public static final String name = "com.mysql.cj.jdbc.Driver";
	public static final String username = "root";
	public static final String password = "123456";
	public String sql = null;
	public Statement stt = null;
	public Connection connection = null;
	public PreparedStatement preparedStatement = null;
	public ResultSet result = null;

	public void CnnectBook() {
		try {
			sql = "SELECT * FROM book";
			Class.forName(name);
			connection = DriverManager.getConnection(url, username, password);
			preparedStatement = connection.prepareStatement(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void CnnectUser() {
		try {
			sql = "SELECT * FROM user";
			Class.forName(name);
			connection = DriverManager.getConnection(url, username, password);
			preparedStatement = connection.prepareStatement(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void Close() {
		try {
			this.connection.close();
			this.preparedStatement.close();
			this.result.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

BookDao class

With the class that connects to the database table, the next step is to have the class that operates on the data table. It implements the function of adding, deleting and checking. There is also a linked list to get the book. emmmmm the part I want to change is to delete first and then add = change. Although it is a little less efficient, it saves a piece of code 23333.

package com.DAO;

import java.util.ArrayList;
import java.util.List;
import com.entity.*;

public class BookDao {
	DataCon dbc = new DataCon();
	String sql = null;

	public boolean addBook(Book book) {
		try {
			dbc.CnnectBook();
			dbc.result = dbc.preparedStatement.executeQuery();
			while (dbc.result.next())
				if (book.getBookid() == dbc.result.getInt(1))
					return false;
			dbc.stt = dbc.connection.createStatement();
			sql = "insert into book values('" + book.getBookid() + "' , '" + book.getBookname() + "' , '"
					+ book.getAuthor() + "' , '" + book.getPublish() + "' , '" + book.getType() + "');";
			dbc.stt.executeUpdate(sql);
			dbc.Close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean delBook(int id) {
		try {
			DataCon dbc = new DataCon();
			dbc.CnnectBook();
			dbc.result = dbc.preparedStatement.executeQuery();
			while (dbc.result.next()) {
				if (id == dbc.result.getInt(1)) {
					dbc.stt = dbc.connection.createStatement();
					sql = "DELETE FROM book WHERE id='" + id + "';";
					dbc.stt.executeUpdate(sql);
					return true;
				}
			}
			dbc.Close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;

	}

	public Book getBook(int a) {
		try {
			dbc.CnnectBook();
			dbc.result = dbc.preparedStatement.executeQuery();
			while (dbc.result.next()) {
				if (a == dbc.result.getInt(1)) {
					Book book = new Book();
					book.setBookid(dbc.result.getInt(1));
					book.setBookname(dbc.result.getString(2));
					book.setAuthor(dbc.result.getString(3));
					book.setPublish(dbc.result.getString(4));
					book.setType(dbc.result.getString(5));
					return book;
				}
			}
			dbc.Close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<Book> getAllBook() {
		ArrayList<Book> list = new ArrayList<Book>();
		try {
			dbc.CnnectBook();
			dbc.result = dbc.preparedStatement.executeQuery();
			while (dbc.result.next()) {
				Book book = new Book();
				book.setBookid(dbc.result.getInt(1));
				book.setBookname(dbc.result.getString(2));
				book.setAuthor(dbc.result.getString(3));
				book.setPublish(dbc.result.getString(4));
				book.setType(dbc.result.getString(5));
				list.add(book);
			}
			dbc.Close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}

UserDao class

Similarly, there are also parts that operate on user data. Here I only get the login and registration functions (we only test this)

package com.DAO;

import com.entity.*;

public class UserDao {

	public boolean login(User user) {
		DataCon cd = new DataCon();
		try {
			cd.CnnectUser();
			cd.result = cd.preparedStatement.executeQuery();
			while (cd.result.next())
				if (user.getName().equals(cd.result.getString(1)))
					if (user.getPassword().equals(cd.result.getString(2)))
						return true;
			cd.Close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean register(User user) {
		DataCon cd = new DataCon();
		String sql = null;
		try {
			cd.CnnectUser();
			cd.result = cd.preparedStatement.executeQuery();
			while (cd.result.next())
				if (user.getName().equals(cd.result.getString(1)))
					return false;
			cd.stt = cd.connection.createStatement();
			sql = "insert into user values('" + user.getName() + "' , '" + user.getPassword() + "' , '" + user.getTel()
					+ "');";
			cd.stt.executeUpdate(sql);
			cd.Close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
}

Aerror class

This class is purely to reduce the amount of code and setup. Mainly for error reporting.

package com.error;
public class Aerror {
	public static String getmessage(String erms,String url) {
		String message=
		"   <!DOCTYPE html> \n"+"<html style=\"height: 100%; margin: 0\">\n" +
	    "	<head><title>"+erms+"</title></head>\n" +
	    "	<body style=\"height: 100%; margin: 0\">\n" +
		"	<table style=\"height:100%; width:100%;\">\r\n" + 
		"    	<tr>\r\n" + 
		"        	<td align=\"center\" valign=\"middle\">     \r\n" + 
		"        		<font  size=\"5\"> "+erms+"<br><br>\r\n" + 
		"				三秒后将返回 <br> <br> 如果没有跳转,请按 <a href=\""+url+"\">这里</a>!!!\r\n" + 
		"				<br> </font>\r\n" + 
		"			</td>\r\n" + 
		"		</tr>\r\n" + 
		"	</table>	"+"</body></html>";
		return message;
	}
}

view layer

Login.jsp

First I got a servlet responsible for page redirection. Apart from this I will not 2333 so when I visit

At http://localhost:8080/book/index , you can see the content of Login.jsp, but at this time, it should be noted that the page is also at the book level. It's just that the content is the content of Login.jsp. Of course, some do not need to be redirected specifically, and only need to be redirected directly when the controller handles events.

package com.Redirect;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import java.io.*;

@WebServlet("/index")
public class index extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("view/Login.jsp").forward(req, resp);
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<title>用户登陆页面</title>
<style>
html, body {
	height: 100%;
	margin: 0;
}
</style>
</head>
<body>
	<form action="Login" method="post" id="form1" style="height: 100%;">
		<table style="height: 100%; width: 100%;">
			<tr>
				<td align="center" valign="middle">
					<table width="350" height="30%" align="center" cellspacing="10"
						cellpadding="10" style="">
						<tr>
							<th colspan="3"><font size="11"> 图书管理系统</font></th>
						</tr>
						<tr></tr>
						<tr></tr>
						<tr></tr>
						<tr></tr>
						<tr>
							<td align="right"><font size="4">账号:</font></td>
							<td colspan="2"><input type="text" name="Name" id="Name"
								style="width: 80%; height: 100%"></td>
						</tr>
						<tr>
							<td align="right"><font size="4">密码:</font></td>
							<td colspan="2"><input type="password" name="Password"
								id="Password" style="width: 80%; height: 100%"></td>
						</tr>
						<tr>
							<td align="center" valign="middle" colspan="3"><input
								type="submit" value="登陆" /> <input type="button" value="注册"
								onClick="test1();" /></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</form>
	<script language="javascript">
		function test1() {
			window.location.href = "register";
		}
	</script>
</body>
</html>

register.jsp

There must be a registration button next to the login interface, and also redirected

package com.Redirect;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import java.io.*;

@WebServlet("/register")
public class register extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("view/Register.jsp").forward(req, resp);
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用户注册</title>
<style>
html, body {
	height: 100%;
	margin: 0;
}
</style>
</head>
<body>
	<table style="height: 100%; width: 100%;">
		<tr>
			<td align="center" valign="middle">
				<form action="Register" method="post">
					<table width="350" height="100%" align="center" cellspacing="7"
						cellpadding="7">
						<tr>
							<th colspan="2"><font size="5"> 注册账号</font></th>
						</tr>
						<tr>
							<td>账号:</td>
							<td><input type="text" name="ID" id="ID">
						</tr>
						<tr>
							<td>密码:</td>
							<td><input type="text" name="Password" id="Password"></td>
						</tr>
						<tr>
							<td>电话号码:</td>
							<td><input type="text" name="Tel" id="Tel"></td>
						</tr>
						<tr>
							<td colspan="1"></td>
							<td><input type="submit" value="确认" /> <input type="button"
								value="返回" onClick="test1();" /></td>
						</tr>
					</table>
				</form>
			</td>
		</tr>
	</table>
</body>
<script language="javascript">
	function test1() {
		window.location.href = "index";
	}
</script>
</html>

Book.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page import="com.entity.*"%>
<%@ page import="com.error.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图书列表</title>
<style>
html, body {
	height: 100%;
	margin: 0;
}
</style>
</head>
<%
	String Islogin = (String) session.getAttribute("isLogin");
	if (Islogin == null) {
		out.println(Aerror.getmessage("未登录!!!", "index"));
		response.setHeader("refresh", "3;URL=index");
	} else {
		List<Book> list = (List<Book>) session.getAttribute("list");
%>
<body>
	<table style="height: 100%; width: 100%; font-size: 25px;">
		<tr>
			<td align="center" valign="middle">
				<h1>图书列表</h1>
				<table align="center" cellpadding="10">
					<tr>
						<td align="center">编号</td>
						<td align="center">书名</td>
						<td align="center">作者</td>
						<td align="center">出版社</td>
					</tr>
					<%
						String bg = "";
							for (int i = 0; i < list.size(); i++) {
								Book b = list.get(i);
					%>
					<tr>
						<td align="center"><%=b.getBookid()%></td>
						<td align="center"><a
							href="GetDetailBook?bookid=<%=b.getBookid()%>"><%=b.getBookname()%></a></td>
						<td align="center"><%=b.getAuthor()%></td>
						<td align="center"><%=b.getPublish()%></td>
					</tr>
					<%
						}
					%>
				</table>
				<form action="addbook" method="post">
					<table width="0" height="100%" align="center">
						<tr>
							<td><input type=submit name="sub1" id="sub1" value="添加图书">
								<input type="button" value="退出登陆" onClick="test1();" /></td>
						</tr>
					</table>
				</form>
			</td>
		</tr>
	</table>
</body>
<%
	}
%>
<script language="javascript">
	function test1() {
		window.location.href = "exit";
	}
</script>
</html>

AddBook.jsp

This page also redirects.

package com.Redirect;

import java.io.IOException;
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("/addbook")
public class addbook extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("view/AddBook.jsp").forward(req, resp);
	}

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.error.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>添加图书</title>
<style>
html, body {
	height: 100%;
	margin: 0;
}
</style>
</head>
<%
	String Islogin = (String) session.getAttribute("isLogin");
	if (Islogin == null) {
		out.println(Aerror.getmessage("未登录!!!", "index"));
		response.setHeader("refresh", "3;URL=index");
	} else {
%>
<body>
	<table style="height: 100%; width: 100%;">
		<tr>
			<td align="center" valign="middle">
				<form action="AddBook" method="post">
					<table width="400" height="100%" align="center" cellspacing="7"
						cellpadding="7" style="font-size: 25px;">
						<tr>
							<th colspan="2"><font size="11"> 添加图书</font></th>
						</tr>
						<tr>
							<td>图书编号:</td>
							<td><input type="text" name="Id" id="Id"></td>
						</tr>
						<tr>
							<td>图书名称:</td>
							<td><input type="text" name="Name" id="Name"></td>
						</tr>
						<tr>
							<td>图书作者:</td>
							<td><input type="text" name="Author" id="Author"></td>
						</tr>
						<tr>
							<td>出版社:</td>
							<td><input type="text" name="Publish" id="Publish"></td>
						</tr>
						<tr>
							<td>图书类型:</td>
							<td><input type="text" name="Type" id="Type"></td>
						</tr>
						<tr>
							<td colspan="1"></td>
							<td><input type="submit" value="提交" /> <input type="button"
								value="返回" onClick="test1();" /></td>
						</tr>
					</table>
				</form>
			</td>
		</tr>
	</table>
</body>
<%
	}
%>
<script language="javascript">
	function test1() {
		window.location.href = "javascript: window.history.go(-1)";
	}
</script>
</html>

DetailBook.jsp

Details of the book.

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.Controller.*"%>
<%@ page import="com.entity.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图书详细信息</title>
<style>
html, body {
	height: 100%;
	margin: 0;
}
</style>
</head>
<body>
	<%Book book = (Book) request.getAttribute("book");%>
	<table style="height: 100%; width: 100%;">
		<tr>
			<td align="center" valign="middle">
				<h1 style="font-size: 35px;">图书详细信息</h1>
				<table align="center" cellpadding="10">
					<tr>
						<td align="right">图书编号:</td>
						<td></td>
						<td><%=book.getBookid()%></td>
					</tr>
					<tr>
						<td align="right">图书名称:</td>
						<td></td>
						<td><%=book.getBookname()%></td>
					</tr>
					<tr>
						<td align="right">图书作者:</td>
						<td></td>
						<td><%=book.getAuthor()%></td>
					</tr>
					<tr>
						<td align="right">出版社:</td>
						<td></td>
						<td><%=book.getPublish()%></td>
					</tr>
					<tr>
						<td align="right">图书类型:</td>
						<td></td>
						<td><%=book.getType()%></td>
					</tr>
				</table>
				<table>
					<tr>
						<td align="center"><input type=button name="sub1" id="sub1"
							value="编辑图书" onClick="test1();"></td>
						<td align="center"><input type=button name="sub1" id="sub1"
							value="删除图书" onClick="test2();"></td>
						<td align="center"><input type=button name="sub1" id="sub1"
							value="返回" onClick="test3();"></td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	<script language="javascript">
		function test1(){
			window.location.href="updabook?bookid=<%=book.getBookid()%>";
		}
		function test2(){
			window.location.href="DelBook?bookid=<%=book.getBookid()%>";
		}
		function test3() {
			window.location.href = "Book";
		}
	</script>
</body>
</html>

UpdaBook

Editing the book interface, directed 2333 separately.

package com.Redirect;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.DAO.BookDao;
import com.entity.Book;

@WebServlet("/updabook")
public class updabook extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String sid = req.getParameter("bookid");
		Book book = new BookDao().getBook(Integer.parseInt(sid));
		req.setAttribute("book", book);
		req.getRequestDispatcher("view/UpdaBook.jsp").forward(req, resp);
	}

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.entity.*"%>
<%@ page import="com.error.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>编辑图书</title>
<style>
html, body {
	height: 100%;
	margin: 0;
	background: url(../img/sky.jpg);
	background-size: 100% 100%;
	background-attachment: fixed
}
</style>
</head>
<%
	String Islogin = (String) session.getAttribute("isLogin");
	if (Islogin == null) {
		out.println(Aerror.getmessage("未登录!!!", "index"));
		response.setHeader("refresh", "3;URL=index");
	} else {
		Book book = (Book) request.getAttribute("book");
%>
<body>
	<table style="height: 100%; width: 100%;">
		<tr>
			<td align="center" valign="middle">
				<form action="UpdaBook?bookid=<%=book.getBookid()%>" method="post">
					<table width="350" height="100%" align="center" cellpadding="7"
						style="font-size: 25px;">
						<tr>
							<th colspan="2"><font size="5">编辑图书</font></th>
						</tr>
						<tr>
							<td>图书编号:</td>
							<td><input type="text" name="Id" id="Id"
								value="<%=book.getBookid()%>"></td>
						</tr>
						<tr>
							<td>图书名称:</td>
							<td><input type="text" name="Name" id="Name"
								value="<%=book.getBookname()%>"></td>
						</tr>
						<tr>
							<td>图书作者:</td>
							<td><input type="text" name="Author" id="Bookprice"
								value="<%=book.getAuthor()%>"></td>
						</tr>
						<tr>
							<td>出版社:</td>
							<td><input type="text" name="Publish" id="Publish"
								value="<%=book.getPublish()%>"></td>
						</tr>
						<tr>
							<td>图书类型:</td>
							<td><input type="text" name="Type" id="Type"
								value="<%=book.getType()%>"></td>
						</tr>
						<tr>
							<td colspan="2" align="center"><input type="submit"
								value="提交" /> <input type="button" value="返回" onClick="test1();" />
							</td>
						</tr>
					</table>
				</form>
			</td>
		</tr>
	</table>
</body>
<script language="javascript">
		function test1(){window.location.href="GetDetailBook?bookid=<%=book.getBookid()%>
	";
	}
</script>
<%
	}
%>
</html>

control layer (controller)

Finally, the interaction between the data layer and the display layer is realized. I implemented this part with Servlet. The main way is to get data from the display layer, then call DAO, get the returned data, and then return to the display layer.

LoginServlet class

Responsible for handling login events

package com.Controller;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.*;
import com.error.*;
import com.entity.*;
import com.DAO.*;

@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("Name");
		String password = req.getParameter("Password");
		User user = new User();
		user.setName(new String(id.getBytes("ISO-8859-1"), "UTF-8"));
		user.setPassword(new String(password.getBytes("ISO-8859-1"), "UTF-8"));
		UserDao userdao = new UserDao();
		if (userdao.login(user)) {
			HttpSession session = req.getSession();
			session.setAttribute("isLogin", id);
			resp.setHeader("refresh", "0;URL=Book");
		} else {
			resp.setContentType("text/html;charset=UTF-8");
			PrintWriter out = resp.getWriter();
			out.println(Aerror.getmessage("账号密码输入错误", "index"));
			resp.setHeader("refresh", "3;URL=index");
		}
	}
}

RegisterServlet class

Responsible for handling registration events

package com.Controller;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.*;
import com.error.*;
import com.entity.*;
import com.DAO.*;

@WebServlet("/Register")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		boolean isregister = false;
		User user = new User();
		user.setName(new String(req.getParameter("ID").getBytes("ISO-8859-1"), "UTF-8"));
		user.setPassword(new String(req.getParameter("Password").getBytes("ISO-8859-1"), "UTF-8"));
		user.setTel(new String(req.getParameter("Tel").getBytes("ISO-8859-1"), "UTF-8"));
		UserDao userdao = new UserDao();
		if (user.getName().equals("") == false) {
			if (userdao.register(user)) {
				isregister = true;
			}
		}
		if (isregister) {
			resp.setContentType("text/html;charset=UTF-8");
			PrintWriter out = resp.getWriter();
			out.println(Aerror.getmessage("注册成功", "index"));
			resp.setHeader("refresh", "3;URL=addbook");
		} else {
			resp.setContentType("text/html;charset=UTF-8");
			PrintWriter out = resp.getWriter();
			out.println(Aerror.getmessage("注册失败", "register"));
			resp.setHeader("refresh", "3;URL=register");
		}

	}
}

ShowBookServlet class

This is responsible for getting the book list and then redirecting to Book.jsp

package com.Controller;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.entity.*;
import com.DAO.*;

@WebServlet("/Book")
public class ShowBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		BookDao dao = new BookDao();
		List<Book> list = dao.getAllBook();
		HttpSession session = req.getSession();
		session.setAttribute("list", list);
		req.getRequestDispatcher("view/Book.jsp").forward(req, resp);
	}
}

AddBookServlet class

Add book handling event

package com.Controller;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import com.DAO.*;
import com.entity.*;
import com.error.Aerror;

@WebServlet("/AddBook")
public class AddBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Book book = new Book();
		book.setBookid(Integer.parseInt(new String(req.getParameter("Id").getBytes("iso-8859-1"), "utf-8")));
		book.setBookname(new String(req.getParameter("Name").getBytes("iso-8859-1"), "utf-8"));
		book.setAuthor(new String(req.getParameter("Author").getBytes("iso-8859-1"), "utf-8"));
		book.setPublish(new String(req.getParameter("Publish").getBytes("iso-8859-1"), "utf-8"));
		book.setType(new String(req.getParameter("Type").getBytes("iso-8859-1"), "utf-8"));
		if (new BookDao().addBook(book)) {
			resp.setHeader("refresh", "0;URL=Book");
		} else {
			resp.setContentType("text/html;charset=UTF-8");
			PrintWriter out = resp.getWriter();
			out.println(Aerror.getmessage("图书编号输入错误", "addbook"));
			resp.setHeader("refresh", "3;URL=addbook");
		}
	}
}

class exitServlet

exit event

package com.Controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/exit")
public class exitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		session.invalidate();
		resp.setHeader("refresh", "0;URL=index");
	}
}

GetDetailBookServlet class

This is to get the id of the book you clicked, then get all the information of the book from the data layer, and then direct it to the DetailBook.jsp interface

package com.Controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.DAO.BookDao;
import com.entity.Book;

@WebServlet("/GetDetailBook")
public class GetDetailBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String sid = req.getParameter("bookid");
		Book book = new BookDao().getBook(Integer.parseInt(sid));
		req.setAttribute("book", book);
		req.getRequestDispatcher("view/DetailBook.jsp").forward(req, resp);
	}
}

DelBookServlet class

delete book event

package com.Controller;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import com.DAO.*;
import com.error.Aerror;

@WebServlet("/DelBook")
public class DelBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int id = Integer.parseInt(req.getParameter("bookid"));
		if (new BookDao().delBook(id)) {
			resp.setHeader("refresh", "0;URL=Book");
		} else {
			resp.setContentType("text/html;charset=UTF-8");
			PrintWriter out = resp.getWriter();
			out.println(Aerror.getmessage("删除失败", "GetDetailBookServlet?bookid=" + id));
			resp.setHeader("refresh", "3;URL=\"GetDetailBookServlet?bookid=\"+id");
		}
	}
}

UpdateBookServlet类

Edit Book Events

package com.Controller;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import com.DAO.*;
import com.entity.*;
import com.error.Aerror;

@WebServlet("/UpdaBook")
public class UpdateBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int booklastid = Integer.parseInt(req.getParameter("bookid"));
		Book book = new Book();
		book.setBookid(Integer.parseInt(new String(req.getParameter("Id").getBytes("iso-8859-1"), "utf-8")));
		book.setBookname(new String(req.getParameter("Name").getBytes("iso-8859-1"), "utf-8"));
		book.setAuthor(new String(req.getParameter("Author").getBytes("iso-8859-1"), "utf-8"));
		book.setPublish(new String(req.getParameter("Publish").getBytes("iso-8859-1"), "utf-8"));
		book.setType(new String(req.getParameter("Type").getBytes("iso-8859-1"), "utf-8"));
		boolean flag = false;
		BookDao bookDAO = new BookDao();
		if (bookDAO.getBook(book.getBookid()) == null) {
			if (booklastid != book.getBookid()) {
				bookDAO.addBook(book);
				bookDAO.delBook(booklastid);
				flag = true;
			}
		} else {
			if (booklastid == book.getBookid()) {
				bookDAO.delBook(booklastid);
				bookDAO.addBook(book);
				flag = true;
			}
		}
		if (flag) {
			String url = "GetDetailBook?bookid=" + book.getBookid();
			resp.setHeader("refresh", "0;URL=" + url);
		} else {
			resp.setContentType("text/html;charset=UTF-8");
			PrintWriter out = resp.getWriter();
			String url = "updabook?bookid=" + booklastid;
			out.println(Aerror.getmessage("图书编辑错误", url));
			resp.setHeader("refresh", "3;URL=" + url);
		}

	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324133671&siteId=291194637
Recommended