設計功能的實現:
----沒有業務層,直接由Servlet調用DAO,所以也沒有事務操作,所以從DAO中直接獲取connection對象
----采用MVC設計模式
----采用到的技術
。MVC設計模式,JSP,Servlet,POJO
。數據庫使用mysql
。數據庫連接池需要使用C3P0數據庫連接池
。頁面上的提示需要使用jQuery
----技術難點
。多個請求如何使用一個Servlet
。如何模糊查詢
。如何在創建和修改的情況下,驗證用戶信息是否已被使用,並給出提示
---------------------------------------------------------------------------------------------------
1.首先數據庫連接池C3P0的一些實現需要導五個開源的架包:
c3p0-0.9.1.2.jar
commons-dbcp-1.4.jar
commons-dbutils-1.3.jar
commons-pool-1.5.5.jar
mysql-connector-java-5.1.6-bin.jar
---------------------------------------------------------------------------
2.連接數據庫連接池的文件為:在src目錄下建立XML文件c3p0-config.xml;
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="mvcapp">
<!-- 指定連接數據源的基本屬性 -->
<property name="user">root</property>
<property name="password">lxn123</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///test</property>
<!-- 若數據庫中連接數不足時, 一次向數據庫服務器申請多少個連接 -->
<property name="acquireIncrement">5</property>
<!-- 初始化數據庫連接池時連接的數量 -->
<property name="initialPoolSize">10</property>
<!-- 數據庫連接池中的最小的數據庫連接數 -->
<property name="minPoolSize">10</property>
<!-- 數據庫連接池中的最大的數據庫連接數 -->
<property name="maxPoolSize">50</property>
<!-- C3P0 數據庫連接池可以維護的 Statement 的個數 -->
<property name="maxStatements">20</property>
<!-- 每個連接同時可以使用的 Statement 對象的個數 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
---------------------------------------------------------------------------
3.連接數據庫連接池的類:JdbcUtils
public class JdbcUtils {
//釋放connection連接
public static void releaseConnection(Connection connection){
try {
if (connection!=null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//該靜態代碼塊只執行一次
private static ComboPooledDataSource dataSource=null;
static{
//通過配置文件實現數據庫的連接,只能被創建一次
dataSource=new ComboPooledDataSource("mvcapp");
}
//獲取數據庫連接,返回數據源的Connection對象
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
}
---------------------------------------------------------------------------
4.建立一個測試類(建立方法類似於class的建立,在src,所在包下建立,建立時找java下的JUnit,建立就行了):JdbcUtilsTest類,對數據庫連接池進行測試,看是否連接成功!!!
public class JdbcUtilsTest {
//測試類包,測試數據庫是否連接成功
@Test
public void testGetConnection() throws SQLException{
Connection connection=JdbcUtils.getConnection();
System.out.println(connection);
}
}
---------------------------------------------------------------------------
5.java是以面向對象的思想編程的,所以建立customer類,CriteriaCustomer類,對數據庫里面的屬性進行封裝處理,便於后邊功能調用的實現
customer類:對數據庫里面的屬性進行封裝處理
public class Customer {
//建立了一個Customer類,對id,name,address,phone這些屬性進行封裝
private Integer id;
private String name;
private String address;
private String phone;
public Customer() {
super();
}
public Customer( String name, String address, String phone) {
this.name = name;
this.address = address;
this.phone = phone;
}
public Customer(Integer id, String name, String address, String phone) {
super();
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", phone=" + phone + "]";
}
}
-------------------
CriteriaCustomer類:模糊查詢的類
//模糊查詢的類
public class CriteriaCustomer {
private String name;
private String address;
private String phone;
public CriteriaCustomer() {
super();
}
public CriteriaCustomer(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}
public String getName() {
if (name==null) {
name="%%";
}
else{
name="%"+name+"%";
}
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
if (address==null) {
address="%%";
}
else{
address="%"+address+"%";
}
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
if (phone==null) {
phone="%%";
}
else{
phone="%"+phone+"%";
}
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
---------------------------------------------------------------------------
6.在該包下建立一個接口類(創建時找interface,創建):CustomerDAO,里邊封裝了一些方法:
(1)模糊查詢方法
(2)查詢的方法
(3)插入數據
(4)通過jsp超鏈接里面?后面的id,獲取id值的方法
(5)通過jsp超鏈接里面?后面的id,並且實現刪除的方法
(6)查村和該name值相等的個數的方法
(7)修改方法
//創建以CustomerDAO接口,可以實現其他類的調用
public interface CustomerDAO {
//模糊查詢方法,通過輸入的字符,查看數據表里的數據里,是否包含這些字符,
public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc);
//查詢的方法,將數據庫的所有數據放在list集合中,並實現輸出的查詢
public List<Customer> getAll();
//插入數據,在頁面上輸入數據name,address,phone,就直接插入
public void save(Customer customer);
//通過jsp超鏈接里面?后面的id,獲取該id下的各屬性的值
public Customer get(Integer id);
//通過jsp超鏈接里面?后面的id,並且實現刪除的功能
public void delete(Integer id);
//返回和該name相等的個數
public long getCountWithName(String name);
//修改的方法
void update(Customer customer);
}
---------------------------------------------------------------------------
7.建立一個DAO類,封裝了基本的增刪改查方法,以供子類繼承使用; 當前DAO沒有事務,直接在方法中獲取數據庫的鏈接
(1)類的構造方法,用到了反射,便有后面Servlet類實現*.do的操作
(2)返回數據庫里面一條記錄的值:比如返回某一條記錄的customerName,或返回有多少條記錄
(3)返回所對應的list集合,獲得的是數據庫的所有記錄值
(4)返回對應的T的一個實體類的對象,該類泛型類的方法
(5)該方法封裝了增刪改操作,可實現對數據庫中的數據進行增加,刪除,修改操作
/*
* 封裝了基本的增刪改查方法,以供子類繼承使用;
* 當前dao沒有事務,直接在方法中獲取數據庫的鏈接
* */
public class DAO <T>{
//這個是線程安全的
private QueryRunner queryRunner=new QueryRunner();
private Class<T> clazz;
//類的構造方法
public DAO() {
//得到父類帶泛型的類型
//type類型導包為import java.lang.reflect.Type;反射類型里面的
//反射。。。。。。Type所有超級類接口,ParameterizedType表示參數化類型,參數化類型在反射方法首次需要時創建(在此包中指定)。
//當創建參數化類型 p 時,p 實例化的一般類型聲明會被解析,並且按遞歸方式創建 p 的所有類型參數。
//superClass instanceof ParameterizedType,判斷superClass是否為首次出現的該類型,instanceof是用法,參數,例子的意思;
Type superClass=getClass().getGenericSuperclass();
if (superClass instanceof ParameterizedType) {
ParameterizedType parameterizedType=(ParameterizedType) superClass;
// getActualTypeArguments():返回表示此類型實際類型參數的 Type 對象的數組。
Type[] typeArgs=parameterizedType.getActualTypeArguments();
if (typeArgs!=null && typeArgs.length>0) {
if (typeArgs[0] instanceof Class) {
clazz=(Class<T>) typeArgs[0];
}
}
}
}
//返回數據庫里面一條記錄的值:比如返回某一條記錄的customerName,或返回有多少條記錄
public <E> E getForValue(String sql,Object...args){
Connection connection=null;
try {
connection=JdbcUtils.getConnection();
return (E) queryRunner.query(connection,sql,new ScalarHandler(),args);
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtils.releaseConnection(connection);
}
return null;
}
//返回所對應的list集合,獲得的是數據庫中所有數據的集合
public List<T> getForList(String sql,Object...args){
Connection connection=null;
try {
connection=JdbcUtils.getConnection();
return queryRunner.query(connection,sql,new BeanListHandler<>(clazz),args);
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtils.releaseConnection(connection);
}
return null;
}
//返回對應的T的一個實體類的對象
public T get(String sql,Object...args){
Connection connection=null;
try {
connection=JdbcUtils.getConnection();
return queryRunner.query(connection,sql,new BeanHandler<>(clazz),args);
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtils.releaseConnection(connection);
}
return null;
}
//該方法封裝了增刪改操作
public void update(String sql,Object...args){
Connection connection=null;
try {
connection=JdbcUtils.getConnection();
queryRunner.update(connection, sql, args);
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtils.releaseConnection(connection);
}
}
}
---------------------------------------------------------------------------
8.CustomerDAOJdbcImpl類,繼承了父類DAO和繼承了接口CustomerDAO,及可獲得父類DAO的非構造方法,和接口CustomerDAO封裝的方法,並在此類中將接口封裝的方法加以實現
CustomerDAOJdbcImpl extends DAO<Customer> implements CustomerDAO
//實現各個功能的類
public class CustomerDAOJdbcImpl extends DAO<Customer> implements CustomerDAO{
@Override
//模糊查詢的方法
public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc) {
String sql="select id,name,address,phone from customer "
+ "where name like ? and address like ? and phone like ?";
//修改了CriteriaCustomer的get方法,使其返回的字符串中含有%%
return getForList(sql,cc.getName(),cc.getAddress(),cc.getPhone());
}
@Override
//獲取整個數據庫中的所有數據
public List<Customer> getAll() {
String sql1="select id,name,address,phone from customer";
return getForList(sql1);
}
@Override
//插入數據到數據庫的方法
public void save(Customer customer) {
String sql2="insert into customer(name,address,phone) values(?,?,?)";
update(sql2, customer.getName(),customer.getAddress(),customer.getPhone());
}
@Override
//獲取某個數據
public Customer get(Integer id) {
String sql3="select id,name,address,phone from customer where id=?";
return get(sql3,id);
}
@Override
//刪除id=?的數據
public void delete(Integer id) {
String sql4="delete from customer where id=?";
update(sql4, id);
}
@Override
//用名字獲取count(name),即獲取用戶的數量
public long getCountWithName(String name) {
String sql5="select count(name) from customer where name=?";
return getForValue(sql5, name);
}
@Override
//對數據進行修改
public void update(Customer customer) {
String sql = "UPDATE customer SET name = ?, address = ?, phone = ? " +
"WHERE id = ?";
update(sql, customer.getName(), customer.getAddress(),
customer.getPhone(), customer.getId());
}
}
---------------------------------------------------------------------------
9.建立一個測試類(建立方法類似於class的建立,在src,所在包下建立,建立時找java下的JUnit,建立就行了):CustomerDAOJdbcImplTest類,對CustomerDAOJdbcImpl類里面的方法進行測試,看在CustomerDAOJdbcImpl類里面的方法是否都可以實現,為后邊在Servlet類里面實現這些方法進行測試,避免出錯
public class CustomerDAOJdbcImplTest1 {
//測試類:
private CustomerDAO customerDAO=new CustomerDAOJdbcImpl();
@Test
public void testGetForListWithCriteriaCustomer(){
CriteriaCustomer cc=new CriteriaCustomer("l",null,null);
List<Customer> list=customerDAO.getForListWithCriteriaCustomer(cc);
System.out.println(list);
}
public void testGetAll() {
List<Customer> list=customerDAO.getAll();
System.out.println(list);
}
public void testSave() {
Customer customer=new Customer();
customer.setName("jiafds");
customer.setAddress("fldskf");
customer.setPhone("5646566");
customerDAO.save(customer);
}
public void testGetInteger(){
Customer customer=customerDAO.get(3);
System.out.println(customer);
}
public void testDelete() {
customerDAO.delete(3);
}
public void testGetCountWithName() {
long count=customerDAO.getCountWithName("lxn");
System.out.println(count);
}
}
---------------------------------------------------------------------------
10.建立一個Servlet類:CustomerServlet(建立時選擇了doGet和doPost方法,間doPost方法放到doGet里邊可實現doPost的方法),doPost里邊的方法都實現,利用反射接受多個以.do結尾的請求,其他方法實現了模糊查詢,增加,刪除,修改的操作
public class CustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private CustomerDAO customerDAO=new CustomerDAOJdbcImpl();
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//利用反射,接受多個以.do結尾的請求
//1.變量 servletPath獲取所有后邊帶.do的方法名和.do ,例如這個/pass.do
String servletPath=request.getServletPath();
//2.在字符串后面除去.do這個字符
String methodName=servletPath.substring(1);
methodName=methodName.substring(0,methodName.length()-3);
//System.out.println(methodName);
try {
Method method=getClass().getDeclaredMethod
(methodName, HttpServletRequest.class,HttpServletResponse.class);
//這里面的this指通過這個方法的到方法名字,並且輸出,及this指methodName的到的方法名字
method.invoke(this,request,response);
} catch (Exception e) {
//如果出現錯誤,去error頁面
response.sendRedirect("error.jsp");
}
}
//查詢方法
private void query(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//獲取模糊查詢的請求參數
String name=request.getParameter("name");
String address=request.getParameter("address");
String phone=request.getParameter("phone");
//把請求參數封裝為一個CriteriaCustomer對象
CriteriaCustomer cc=new CriteriaCustomer(name,address,phone);
//1.調用CustomerDAO的getForListWithCriteriaCustomer(cc)方法得到lists的集合
List<Customer> lists=customerDAO.getForListWithCriteriaCustomer(cc);
//2.把list集合放到request中
request.setAttribute("list", lists);
//3.轉發頁面到index.jsp(不能使用重定向) /代表的是根目錄下的jsp文件;
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
//插入數據的方法:
private void addCustomer(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
//1.獲取表單參數,name,address,phone
String name=request.getParameter("name");
String address=request.getParameter("address");
String phone=request.getParameter("phone");
//檢驗名字是否已經被占用了:調用CustomerDAO的getCountWithName方法,獲取name參數是否大於0,如果大於....
//並且消息可以回顯:value="<%=request.getParameter("name")==null ? "" :%>"
long count=customerDAO.getCountWithName(name);
if (count>0) {
request.setAttribute("message","用戶名"+name+"已經被占用了,請重新選擇!!!");
//名字重復了,請求的轉發到/newcustomer.jsp
request.getRequestDispatcher("/newcustomer.jsp").forward(request, response);
//結束方法
return ;
}
//2.若驗證通過的話,把表單參數封裝為一個customer的對象
Customer customer=new Customer(name,address,phone);
//3.調用CustomerDAO的save方法執行保存
customerDAO.save(customer);
//數據插入成功后,請求的轉發到newcustomer.jsp
//request.getRequestDispatcher("/newcustomer.jsp").forward(request, response);
//4.數據插入成功后,重定向到success.jsp頁面:使用重定向可以避免出現表單的重復提交問題.
response.sendRedirect("success.jsp");
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
//1.獲取請求的id
String idStr=request.getParameter("id");
int id=0;
try {
id=Integer.parseInt(idStr);
//2.調用CustomerDAO的getId()方法執行刪除
customerDAO.delete(id);
} catch (Exception e) {
response.sendRedirect("query.do");
}
//重定向的頁面jsp,其前面不用加"/"
response.sendRedirect("success.jsp");
}
//修改數據表里的數據
private void edit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//出現了所查詢的數據不存在的錯誤顯示到error.jsp頁面
String forwardPath="/error.jsp";
//1.獲取請求的id
String idStr=request.getParameter("id");
//2. 調用 CustomerDAO 的 customerDAO.get(id) 獲取和 id 對應的 Customer 對象 customer
try {
Customer customer=customerDAO.get(Integer.parseInt(idStr));
//如果數據存在的跳轉到updatecustomer.jsp頁面,進行修改數據
if(customer!=null){
forwardPath="/updatecustomer.jsp";
//將數據放到request請求的轉發的里面
request.setAttribute("customer", customer);
}
} catch (Exception e) {
System.out.println("fsdlkf");
}
//4. 響應 updatecustomer.jsp 頁面: 轉發.
request.getRequestDispatcher(forwardPath).forward(request, response);
}
private void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1. 獲取表單參數: id, name, address, phone, oldName
String id=request.getParameter("id");
String name=request.getParameter("name");
String address=request.getParameter("address");
String phone=request.getParameter("phone");
String oldName=request.getParameter("oldName");
//2. 檢驗 name 是否已經被占用:
//2.1 比較 name 和 oldName 是否相同, 若相同說明 name 可用.
//2.1 若不相同, 則調用 CustomerDAO 的 getCountWithName(String name) 獲取 name 在數據庫中是否存在
if(!oldName.equalsIgnoreCase(name)){
long count=customerDAO.getCountWithName(name);
//2.2 若返回值大於 0, 則響應 updatecustomer.jsp 頁面: 通過轉發的方式來響應 newcustomer.jsp
if (count>0) {
//2.2.1 在 updatecustomer.jsp 頁面顯示一個錯誤消息: 用戶名 name 已經被占用, 請重新選擇!
//在 request 中放入一個屬性 message: 用戶名 name 已經被占用, 請重新選擇!,
//在頁面上通過 request.getAttribute("message") 的方式來顯示
request.setAttribute("message", "用戶名"+name+"已經被占用,請重現選擇!!!");
//2.2.2 newcustomer.jsp 的表單值可以回顯.
//address, phone 顯示提交表單的新的值, 而 name 顯示 oldName, 而不是新提交的 name
//2.2.3 結束方法: return
request.getRequestDispatcher("updatecustomer.jsp").forward(request, response);
return ;
}
}
//3. 若驗證通過, 則把表單參數封裝為一個 Customer 對象 customer
Customer customer=new Customer(name,address,phone);
customer.setId(Integer.parseInt(id));
//4. 調用 CustomerDAO 的 update(Customer customer) 執行更新操作
customerDAO.update(customer);
//5. 重定向到 query.do
response.sendRedirect("query.do");
}
}
---------------------------------------------------------------------------
11.error.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>
<h3>不好意思頁面無法打開!!!</h3>
</body>
</html>
--------------------------------------------------------------------------
12.index.jsp頁面,是主頁面,在這個里面有JQuary方法(要進行刪除操作時,閃現出一個小框,詢問是否確定刪除),可實現模糊查詢,按Query,顯示模糊查詢的結構,按Add New Customer可以進行數據的插入,填入數據插入后,顯示操作成功頁面success.jsp,點擊該頁面的Return...又返回到index.jsp頁面,同時點擊每個數據后面的delete和update可以實現對數據的刪除和修改,點擊update,到updatecustomer.jsp頁面,實現對數據的修改.
<%@page import="com.lanqiao.javatest2.Customer"%>
<%@page import="java.util.List"%>
<%@ 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>
<script type="text/javascript" src="JavaScript/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$(".delete12").click(function(){
var content = $(this).parent().parent().find("td:eq(1)").text();
var flag = confirm("確定要是刪除" + content + "的信息嗎?");
return flag;
});
});
</script>
</head>
<body>
<form action="query.do" method="post">
<table>
<tr>
<td>name:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>address:</td>
<td><input type="text" name="address"/></td>
</tr>
<tr>
<td>phone:</td>
<td><input type="text" name="phone"/></td>
</tr>
<tr>
<td><input type="submit" value="Query"/></td>
<td><a href="newcustomer.jsp">Add New Customer(insert new Customer)</a></td>
</tr>
</table>
</form>
<%
List<Customer> list=(List<Customer>)request.getAttribute("list");
if(list!=null && list.size()>0){
%>
<hr>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>Delete</th>
<th>Update</th>
</tr>
<%for(Customer list12:list){%>
<tr>
<td><%=list12.getId() %></td>
<td><%=list12.getName() %></td>
<td><%=list12.getAddress() %></td>
<td><%=list12.getPhone() %></td>
<td><a href="delete.do?id=<%=list12.getId() %>" class="delete12">Delete</a></td>
<td><a href="edit.do?id=<%=list12.getId() %>">Update</a></td>
</tr>
<%}%>
</table>
<%}%>
</body>
</html>
---------------------------------------------------------------------------
13.newcustomer.jsp頁面,是index.jsp頁面點擊Add New Customer(insert new Customer),跳轉到的,在該可以進行對數據的插入操作,其中,必須判斷是否數據表中已經含有該name.
<%@ 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>
<%
Object obj=request.getAttribute("message");
if(obj!=null){
out.println("<br>");
%>
<font color="red"><%=obj %></font>
<br><br>
<%}%>
<form action="addCustomer.do" method="post">
<table border="1" >
<tr>
<td>name:</td>
<td><input type="text" name="name"
value="<%=request.getParameter("name")==null ? "" : request.getParameter("name")%>" /></td>
</tr>
<tr>
<td>address:</td>
<td><input type="text" name="address"
value="<%=request.getParameter("address")==null ? "" : request.getParameter("address")%>"/></td>
</tr>
<tr>
<td>phone:</td>
<td><input type="text" name="phone"
value="<%=request.getParameter("phone")==null ? "" : request.getParameter("phone")%>"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>
</tr>
</table>
</form>
</body>
</html>
---------------------------------------------------------------------------
14.success.jsp頁面,插入,修改的頁面提交之后,如果成功的話,就出現在該頁面,作用是返回操作成功的信息給用戶,再點擊Return...,返回到index.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>
<h2>操作成功!!!</h2>
<a href="index.jsp">Return....</a>
</body>
</html>
---------------------------------------------------------------------------
15.updatecustomer.jsp頁面,實現對數據的修改,其中使用了<input type="hidden" name="id" value="<%= id %>"/>這個隱藏類型,在頁面上用戶看不到這個類型而已。
<%@page import="com.lanqiao.javatest2.Customer"%>
<%@ 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>
<%
Object str=request.getAttribute("message");
if(str!=null){
%>
<font color="red"><%=str %></font>
<%
}
%>
<%
String id=null;
String name=null;
String address=null;
String phone=null;
String oldName=null;
Customer customer=(Customer)request.getAttribute("customer");
if(customer!=null){
id=customer.getId()+"";
name=customer.getName();
oldName=customer.getName();
address=customer.getAddress();
phone=customer.getPhone();
}
else{
id=request.getParameter("id");
name=request.getParameter("name");
oldName=request.getParameter("oldName");
address=request.getParameter("address");
phone=request.getParameter("phone");
}
%>
<form action="update.do" method="post">
<input type="hidden" name="id" value="<%= id %>"/>
<input type="hidden" name="oldName" value="<%= oldName %>"/>
<table border="1" >
<tr> <td>name:</td>
<td><input type="text" name="name" value="<%=name %>" /></td>
</tr>
<tr> <td>address:</td>
<td><input type="text" name="address" value="<%=address%>"/></td>
</tr>
<tr> <td>phone:</td>
<td><input type="text" name="phone" value="<%=phone%>"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>
</tr>
</table>
</form>
</body>
</html>
---------------------------------------------------------------------------
頁面及數據庫截圖:
操作前數據庫表:
index.jsp頁面顯示:在表格里填入數據,點擊Query可以進行模糊查詢。
點擊Add New。。。可以插入數據;
插入數據后顯示:點擊后返回到index.jsp頁面
點擊表格里面的delete出現:點擊確定和取消,完成此方法
點擊表格里面的update出現這個頁面,表格里面還有原來的值,並進行修改
---------------------------------------------------------------------------
16.在lib下邊的web.xml文件,實現對Servlet和jsp文件之間的配置和反射;*.do可以對后邊帶.do的Servlet類里面的方法進行反射和獲取
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<description></description>
<display-name>CustomerServlet1</display-name>
<servlet-name>CustomerServlet1</servlet-name>
<servlet-class>com.lanqiao.javatest6.CustomerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CustomerServlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>