package com.spring.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 com.spring.dao.IBookInfoDao;
import com.spring.dao.impl.BookInfoDaoImpl;
import com.spring.entity.BookInfo;
public class BookInfoServlet extends HttpServlet {
private IBookInfoDao bookInfoDao=new BookInfoDaoImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求方法的标示
String method=request.getParameter("method");
if("add".equals(method)){
this.addBookInfo(request, response);
}else if("list".equals(method)){
this.listBookInfo(request, response);
}else if("updateUI".equals(method)){
this.updateUI(request, response);
}else if("update".equals(method)){
this.update(request, response);
}else if("delete".equals(method)){
this.delete(request, response);
}
}
//处理添加书籍
public void addBookInfo(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取addBookInfo.jsp的数据
String bookname = request.getParameter("bookname");
String bookauthor = request.getParameter("bookauthor");
String bookprice = request.getParameter("bookprice");
// 把请求中获得值包装到对象
BookInfo bookInfo = new BookInfo();
bookInfo.setBookname(bookname);
bookInfo.setBookauthor(bookauthor);
bookInfo.setBookprice(new Float(bookprice));
try {
bookInfoDao.addBookInfo(bookInfo);
} catch (Exception e) {
response.sendRedirect("error.jsp");
return ;
}
response.sendRedirect(request.getContextPath()+"/BookInfoServlet?method=list");
}
//显示所有的书籍
public void listBookInfo(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
List<BookInfo> list =bookInfoDao.findAllBookInfo();
// 保存集合对象到request作用域
request.setAttribute("books", list);
} catch (Exception e) {
e.printStackTrace();
}
// listBook.jsp
request.getRequestDispatcher("admin/listBook.jsp").forward(request, response);
}
// 把将要修改的书籍信息读取到表单中
public void updateUI(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// 获得将要修改书籍对象对应编号
String bookid = request.getParameter("bookid");
// 根据编号查询对象书籍
BookInfo updateBook = bookInfoDao.findBookInfoById(new Integer(bookid));
request.setAttribute("book", updateBook);
request.getRequestDispatcher("admin/updateBook.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
public void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// 获得addBookInfo.jsp的数据
String bookname = request.getParameter("bookname");
String bookauthor = request.getParameter("bookauthor");
String bookprice = request.getParameter("bookprice");
String bookid = request.getParameter("bookid");
// 把请求中获得值包装到对象
BookInfo bookInfo = new BookInfo();
bookInfo.setBookname(bookname);
bookInfo.setBookauthor(bookauthor);
bookInfo.setBookprice(new Float(bookprice));
bookInfo.setBookid(new Integer(bookid));
// 操作数据
this.bookInfoDao.updateBookInfo(bookInfo);
response.sendRedirect(request.getContextPath() + "/BookInfoServlet?method=list");
} catch (Exception e) {
e.printStackTrace();
}
}
//删除图书信息
public void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String bookid=request.getParameter("bookid");
try {
//根据图书编号查询图书信息
BookInfo bookInfo=bookInfoDao.findBookInfoById(new Integer(bookid));
//根据图书编号删除图书信息
bookInfoDao.deleteBookInfoById(new Integer(bookid).intValue());
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect("error.jsp");
}
response.sendRedirect(request.getContextPath() + "/BookInfoServlet?method=list");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
带验证的图书管理.zip (43个子文件)
带验证的图书管理
JavaWeb
.project 1KB
bin
WebRoot
WEB-INF
classes
com
spring
dao
impl
servlet
filter
entity
util
src
com
spring
filter
.classpath 151B
.settings
.jsdtscope 406B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.mymetadata 294B
WebRoot
WEB-INF
web.xml 2KB
lib
sqljdbc.jar 247KB
classes
com
spring
dao
IUserInfoDao.class 222B
IBookInfoDao.class 416B
impl
BookInfoDaoImpl.class 4KB
UserInfoDaoImpl.class 2KB
servlet
LoginServlet.class 2KB
BookInfoServlet.class 4KB
filter
AdminFilter.class 2KB
EncodingFilter.class 1KB
entity
BookInfo.class 1KB
UserInfo.class 943B
util
DBConnection.class 2KB
DBHelper.class 2KB
jdbc.properties 129B
index.jsp 399B
META-INF
MANIFEST.MF 39B
error.jsp 262B
admin
updateBook.jsp 1024B
showInfo.jsp 917B
addBookInfo.jsp 885B
listBook.jsp 890B
.myeclipse
src
com
spring
dao
IBookInfoDao.java 451B
impl
UserInfoDaoImpl.java 1KB
BookInfoDaoImpl.java 3KB
IUserInfoDao.java 190B
servlet
LoginServlet.java 2KB
BookInfoServlet.java 4KB
filter
EncodingFilter.java 769B
AdminFilter.java 1KB
entity
UserInfo.java 513B
BookInfo.java 698B
util
DBHelper.java 1KB
DBConnection.java 2KB
jdbc.properties 129B
.classpath 151B
mysql.sql 929B
共 43 条
- 1
资源评论
- zhuke03242012-07-05运行报错!不知道怎么回事!
- wangchuankun12014-06-15出错啦,怎么整的
- 卟帶吙2012-11-02有错误,部门代码可以借鉴
- jfkdf2012-10-23部分模块拆出来可以用,一起用就用不了了,有出错
喜之郎~果冻
- 粉丝: 0
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功