图书信息管理的增删改查(三)

一、接上篇文章图书信息管理的增删改查(二),链接如下:图书信息管理的增删改查(二)
注:最后有惊喜哦
本文接着写图书分类的增删改查,跟图书信息增删改查的思路基本都差不多,我们直接看代码:
1、这里我们写第一篇文章里面,效果图的第三张图的界面及功能
a、查看图书分类,跟查看图书信息的原理一样, servlet代码如下:

package com.hnpi.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hnpi.dao.ClassifyDao;
import com.hnpi.dao.impl.ClassifyDaoImpl;
import com.hnpi.model.Classify;

public class ClassifyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		
		HttpSession session = request.getSession();
		//实例化接口
		ClassifyDao classifyDao = new ClassifyDaoImpl();
		//获取ClassifyDaoImpl()中的select()方法
		List<Classify> classifyList = classifyDao.select();
		//把获取到的数组放到session中
		session.setAttribute("classifyList", classifyList);
		//跳转到book_classify.jsp界面,把数值展示出来
		request.getRequestDispatcher("book_classify.jsp").forward(request, response);	
	}
}

然后就是把查询出来的数据通过book_classify.jsp界面展示出来,代码如下:
book_classify.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>图书分类</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

		<style type="text/css">
#a table,tr,th,td {
	border: 1px #000 solid;
	border-collapse: 0px;
	text-align: center;
	height: 30px;
	background-color:rgba(255,204,51,0.5) ;
}

table {
	width: 1000px;
	font-size: 18px;
}

table tr:hover {
	background-color: rgba(200, 104, 101, 1);
}

a {
	text-decoration: none; /* 去掉超链接的下划线 */
	color: rgba(51, 153, 255, 1); /* 设置超链接的字体颜色 */
	font-family: '微软雅黑'; /*  设置超链接的字体样式 */
	display: block;
	width: 100%;
	height: 100%;
}

a:hover {
	background-color: rgba(255, 0, 6, 1);
}

body {
	background-image: url("WebContent/imgs/t5.jpg");
	background-repeat: no-repeat;
}
</style>

	</head>

	<body>
		<jsp:include page="lie_biao.jsp"></jsp:include>
		<div style="width: 1000px; height: auto; margin-left: 100px">
			<table>
				<tr>
					<th>
						分类ID
					</th>
					<th>
						分类名称
					</th>
					<th colspan="2">
						操作
					</th>
				</tr>
				<c:forEach items="${sessionScope.classifyList }" var="classify"
					varStatus="status">
					<tr>
						<td>
							${classify.classifyId }
						</td>
						<td>
							${classify.classifyName }
						</td>
						<td>
							<a href="DelClassifyServlet?id=${classify.classifyId }"
								onClick="return confirm('确定删除?');">删除</a>
						</td>
						<td>
							<a href="update_classify.jsp?id=${classify.classifyId }&classifyName=${classify.classifyName }">更新</a>
						</td>
					</tr>
				</c:forEach>
			</table>
		</div>
	</body>
</html>

b、下面就是图书分类的删除和更新操作了,代码如下:
1、(DelClassifyServlet界面) 删除,原理和上一篇的删除原理一样

package com.hnpi.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hnpi.dao.ClassifyDao;
import com.hnpi.dao.impl.ClassifyDaoImpl;

public class DelClassifyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		
		//获取book_classify.jsp里面删除传过来的ID
		String id = request.getParameter("id");
		ClassifyDao classifyDao = new ClassifyDaoImpl();
		//判断如果true则跳转到ClassifyServlet界面,否则跳转到book_classify.jsp界面
		if(classifyDao.delete(Integer.parseInt(id))){
			response.sendRedirect("ClassifyServlet");
		}else{
			response.sendRedirect("book_classify.jsp");
		}		
	}
}

2、(update_classify.jsp界面)更新操作

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>更新图书分类</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

		<style>
#tu {
	background-image: url("WebContent/imgs/t4.jpg");
	background-repeat: no-repeat;
	width: 800px;
	height: 600px;
	margin-left: 240px;
	background-size: 700px;
}
</style>
		<link rel="stylesheet" href="yangshi/css/classify.css" type="text/css"></link>

	</head>

	<body>
		<%
		//判断从book_classify.jsp界面传过来的值是否为空,如果为空则不转换编码,跳转到ClassifyServlet界面,不为空则转换编码
			if (!"".equals(request.getParameter("classifyName"))
					&& request.getParameter("classifyName") != null) {
				String name = new String(request.getParameter("classifyName")
						.getBytes("ISO-8859-1"), "utf-8");
				session.setAttribute("name", name);
			} else {
				response.sendRedirect("ClassifyServlet");
			}
		%>
		<jsp:include page="lie_biao.jsp"></jsp:include>
		<div style="float: left; margin-top: 320px">
			<img src="WebContent/imgs/t1.jpg" style="width: 230px;"></img>
		</div>
		<div id="tu">
			<form action="UpdateClassify" method="post">
				<div style="width: 100px; height: 230px;"></div>
				<table id="tab">
					<tr>
						<td>
							分类ID:
						</td>
						<td>
							<input type="text" name="id" value="${param.id }" required="true"
								onkeyup="this.value=this.value.replace(/\D/g,'')"
								placeholder="请输入数字" />
						</td>
					</tr>
					<tr>
						<td>
							分类名称:
						</td>
						<td>
							<input type="text" name="name"
								value="<%=session.getAttribute("name")%>" required="true" />
						</td>
					</tr>
					<tr>
						<td colspan="2" style="text-align: center;">
							<input type="submit" value="提交" style="font-size: 20px" />
						</td>
					</tr>
				</table>
			</form>
		</div>
	</body>
</html>

里面的css样式代码如下(和添加用的同一个):

@CHARSET "UTF-8";

#tab {
	font-size: 20px;
	margin-left: 200px;
	width: 400px;
	border-collapse: collapse;
}

#tab tr:hover {
	background-color: rgba(100, 204, 101, 1);
}

#tab th {
	text-align: right;
	height: 50px;
	border: 1px solid green;
}

#tab td {
	height: 50px;
	border: 1px solid green;
}

input {
	width: 250px;
	height: 40px;
}

(UpdateClassify界面)更新servlet界面

package com.hnpi.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hnpi.dao.ClassifyDao;
import com.hnpi.dao.impl.ClassifyDaoImpl;
import com.hnpi.model.Classify;

public class UpdateClassify extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		
		ClassifyDao classifyDao = new ClassifyDaoImpl();
		Classify classify = new Classify();
		//获取update_classify.jsp界面输入框的数值,然后设置到相对应的方法属性中
		int id = Integer.parseInt(request.getParameter("id"));
		classify.setClassifyId(id);
		classify.setClassifyName(request.getParameter("name"));
		//判断如果true则跳转到ClassifyServlet界面,否则跳转到update_classify.jsp界面
		if(classifyDao.update(classify)){
			response.sendRedirect("ClassifyServlet");
		}else{
			response.sendRedirect("update_classify.jsp");
		}
	}
}

c、添加图书分类的jsp界面,代码如下:
(add_classify.jsp界面)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'add_classify.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">


		<link rel="stylesheet" href="yangshi/css/classify.css" type="text/css"></link>
		<style type="text/css">
#tu {
	background-image: url("WebContent/imgs/t4.jpg");
	background-repeat: no-repeat;
	width: 800px;
	height: 600px;
	margin-left: 240px;
	background-size: 700px;
}
</style>
	</head>

	<body>
		<jsp:include page="lie_biao.jsp"></jsp:include>
		<div style="float: left; margin-top: 320px">
			<img src="WebContent/imgs/t1.jpg" style="width: 230px;"></img>
		</div>
		<div id="tu">
			<form action="AddClassifyServlet" method="post">
				<div style="width: 100px; height: 230px;"></div>
				<table id="tab">
					<tr>
						<th>
							分类ID:
						</th>
						<td>
							<input type="text" name="id" required="true"
								onkeyup="this.value=this.value.replace(/\D/g,'')"
								placeholder="请输入数字" />
						</td>
					</tr>
					<tr>
						<th>
							分类名称:
						</th>
						<td>
							<input type="text" name="name" required="true" />
						</td>
					</tr>
					<tr>
						<td colspan="2" style="text-align: center;">
							<input type="submit" value="提交" style="font-size: 20px" />
						</td>
					</tr>
				</table>
			</form>
		</div>
	</body>
</html>

(AddClassifyServlet界面)执行添加操作的servlet代码如下:

package com.hnpi.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hnpi.dao.ClassifyDao;
import com.hnpi.dao.impl.ClassifyDaoImpl;
import com.hnpi.model.Classify;

public class AddClassifyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		//实例化接口
		ClassifyDao classifyDao = new ClassifyDaoImpl();
		//实例化图书分类 类
		Classify classify = new Classify();
		//给Classify类里面的属性设置值,值从add_classify.jsp界面传过来
		classify.setClassifyId(Integer.parseInt(request.getParameter("id")));
		classify.setClassifyName(request.getParameter("name"));
		//判断如果true则跳转到ClassifyServlet界面,否则跳转到add_classify.jsp界面
		if(classifyDao.add(classify)){
			response.sendRedirect("ClassifyServlet");
		}else{
			response.sendRedirect("add_classify.jsp");
		}
	}
}

二、总结
这三篇文章写的只是,一个简单地图书信息管理的增删改查,由于代码量比较多,就分开来写了,谢谢大家的观看,如有代码比较累赘的地方,还请告知。

扫下面二维码关注微信公众号 回复:jsp1020 领取源代码
在这里插入图片描述

发布了36 篇原创文章 · 获赞 7 · 访问量 2089

猜你喜欢

转载自blog.csdn.net/q_2540638774/article/details/102651305