JAVA 入门简单的增删改查(Servlet+JSP)二

接着上次的
DAO层
删除操作
package com.zhouxiaobo.demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
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.swing.DefaultBoundedRangeModel;

import com.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.dao.BookDao;
import com.zhouxiaobo.demo.jdbc.service.BussinessService;
import com.zhouxiaobo.demo.jdbc.service.BussinessServiceImpl;
import com.zhouxiaobo.demo.jdbc.util.DBUtil;
import com.zhouxiaobo.demo.jdbc.util.DefaultDBUtil;

/**
* Servlet implementation class DeleteBookServlet
*/
public class DeleteBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String sn=request.getParameter("sn");
DBUtil dbUtil=new DefaultDBUtil();
try{
Connection conn=dbUtil.openConn();
BussinessService b=new BussinessServiceImpl(conn);
b.deletebook(sn, conn);

List<Book>books=b.showallbooks();
request.setAttribute("book", books);
request.setAttribute("flag", sn);
            request.getRequestDispatcher("booklist.jsp").forward(request, response);
}finally{
dbUtil.closeConn();
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}



package com.zhouxiaobo.demo;

import java.io.IOException;
import java.sql.Connection;
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.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.service.BussinessService;
import com.zhouxiaobo.demo.jdbc.service.BussinessServiceImpl;
import com.zhouxiaobo.demo.jdbc.util.DBUtil;
import com.zhouxiaobo.demo.jdbc.util.DefaultDBUtil;

/**
* Servlet implementation class EditBookServlet
*/
public class EditBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
DBUtil dbUtil=new DefaultDBUtil();
try{

Connection conn=dbUtil.openConn();
BussinessService service=new BussinessServiceImpl(conn);
List<Book>listbook=service.showallbooks();
    request.setAttribute("book", listbook);
    request.getRequestDispatcher("editBook.jsp").forward(request, response);
}finally{
dbUtil.closeConn();
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}
先把数据set到jsp中,再得到参数
package com.zhouxiaobo.demo;

import java.io.IOException;
import java.sql.Connection;
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.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.service.BussinessService;
import com.zhouxiaobo.demo.jdbc.service.BussinessServiceImpl;
import com.zhouxiaobo.demo.jdbc.util.DBUtil;
import com.zhouxiaobo.demo.jdbc.util.DefaultDBUtil;

/**
* Servlet implementation class SureEdit
*/
public class SureEdit extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
DBUtil dbUtil=new DefaultDBUtil();
Connection conn=dbUtil.openConn();
String sn=request.getParameter("sn");
String name=request.getParameter("name");
BussinessService service=new BussinessServiceImpl(conn);
try{
service.updatebook(sn, name, conn);
List<Book>listBooks=service.showallbooks();
request.setAttribute("book", listBooks);
request.getRequestDispatcher("booklist.jsp").forward(request, response);
}
finally{
dbUtil.closeConn();
}



}

}

<%@page import="java.util.List"%>
<%@page import="com.zhouxiaobo.demo.jdbc.bean.Book"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<table border="1" cellspacing="0" cellpadding="0 width="100%" style="align:center;">
<tr bgcolor="CCC000" style="font-weight:bold;">


<c:forEach var="bk" items="${book}">
<tr>
<form action="/LIB/sureEdit" method="post">
  书号<input type="text" name="sn" value="${bk.sn}">
书名<input type="text" name="name" value="${bk.name}">
<input type="submit" value="更改"><br>
</form>
</c:forEach>
</table>



package com.zhouxiaobo.demo;

import java.io.IOException;
import java.sql.Connection;
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.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.service.BussinessService;
import com.zhouxiaobo.demo.jdbc.service.BussinessServiceImpl;
import com.zhouxiaobo.demo.jdbc.util.DBUtil;
import com.zhouxiaobo.demo.jdbc.util.DefaultDBUtil;

/**
* Servlet implementation class Findbysn
*/
public class Findbysn extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
DBUtil dbUtil=new DefaultDBUtil();
try{
Connection conn=dbUtil.openConn();
String sn=request.getParameter("sn");
BussinessService service=new BussinessServiceImpl(conn);


List<Book> books=service.findbysn(sn, conn);
request.setAttribute("book", books);
request.getRequestDispatcher("booklist.jsp").forward(request, response);

}finally{
dbUtil.closeConn();
}

}

}


JSP

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/LIB/findbysn" method="post">
  书号<input type="text" name="sn"><br>
<input type="submit" value="查找"><br>
<input type="hidden" name="kind" value="insert"><br>
</form>
</body>
</html>

你可能感兴趣的:(java,jsp,servlet,jstl)