1. 程式人生 > >javaweb的一個增刪查改的小案例(續)

javaweb的一個增刪查改的小案例(續)

1.建立一個可以進行查詢也可以進入到新增到資料的頁面的連結

如下為jsp頁面

<%@ page language="java" import="java.util.*" import="com.mvcapp.domain.Customer" 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 'index.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">
	<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$(".delete").click(function(){
				var content=$(this).parent().parent().find("td:eq(1)").text();
				var flag=confirm("確定要刪除"+content+"的資訊嗎");
				return true;
			});
		});
	</script>
  </head>
  <body style="height: 5px; ">
	<a href="ListAppStudentServlet">listallstudentservlet</a>
	<form action="query.do" method="post">
		<table border="1px" cellpadding="10px" cellspacing="1px">
			<tr>
				<td>CustomerName</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</a></td>
			</tr>
		</table>
	</form>
	<br><br>
	<%
		List<Customer> customerlist=(List<Customer>)request.getAttribute("listall");
		if(customerlist!=null&&customerlist.size()>0){
		%>
		<br><br>
		<table border="1" cellpadding="10" cellspacing="0">
			<tr>
				<td>ID</td>
				<td>CustomerName</td>
				<td>Address</td>
				<td>Phone</td>
				<td>update</td>
				<td>delete</td>
			</tr>
			<%
				for(Customer c:customerlist){
				%>
				<tr>
					<td><%=c.getId() %></td>
					<td><%=c.getName()%></td>
					<td><%=c.getAddress() %></td>
					<td><%=c.getPhone()%></td>
					<td><a href="edit.do?id=<%=c.getId()%>">update</a></td>
					<td><a href="delete.do?id=<%=c.getId()%>" class="delete">delete</a></td>
				</tr>
				<%				
				}
			 %>
		</table>
		
		
		<%
		}
	 %>
	
	
	
  </body>
</html>
2.建立一個list.jsp頁面我們可以在這裡便利集合中的物件通過便利物件放置在table中可以更好的額顯示出來,
<%@ page language="java" import="java.util.*" import="com.mvc.test.Student" 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 'list.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">
  </head>
  <body>
	<%
		List<Student> ad=(List)request.getAttribute("students");
		System.out.println(ad);
		%>
		<table border="1" cellpadding="10" cellspacing="0">
			<tr>
				<td>Id</td>
				<td>UserName</td>
				<td>Password</td>
				<td>Delete</td>
			</tr>
			<%
				for(Student a:ad){
				%>
				<tr>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<td><%=a.getId() %></td><td><%=a.getUsername() %></td><td><%=a.getPassword()%></td><td><a href="DeleteServlet?id=<%=a.getId()%>">刪除</a></td></tr><%} %></table> </body></html> 3.建立一個error.jsp頁面,我們在多對一servlet中如果找不到指定的方法我們可以通過相關方式指定到該頁面,所以這樣可以更好的對使用者提供更為方便的顯示
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'error.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" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    This is my JSP page. <br>
    <h2>對不起沒有指定頁面</h2>
  </body>
</html>

4.建立一個lis.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 'newcustomer.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">
  </head>
  <body>
 <%=request.getAttribute("message")==null?"":request.getAttribute("message")%>
  <form action="addCustomer.do" method="post">
		<table border="1px" cellpadding="10px" cellspacing="1px">
			<tr>
				<td>CustomerName</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"/></td>
			</tr>
			<tr>
				<td>Phone</td>
				<td><input type="text" name="phone"/></td>
			</tr>
			<tr>
				<td colspan="2"><input  type="submit" value="submit"/></td>
			</tr>
		</table>
	</form>
  </body>
</html>
5.對資料進行更新操作我們可以建立一個updateCustomer.jsp頁面。並且可以將需要修改的原資料回顯到頁面,這樣可以清晰顯示
<%@ page language="java" import="java.util.*" import="com.mvcapp.domain.Customer" 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 'updatecustomer.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">
  </head>
  <body>
   <%=request.getAttribute("message")==null?"":request.getAttribute("message")%>
    	<%
    		Customer customer=(Customer)request.getAttribute("customer");
    	 %>
    	 <form action="update.do" method="post">
    	 <input type="hidden" name="id" value="<%=customer.getId()%>">
    	 <input type="hidden" name="oldName"  value="<%=customer.getName()%>">
		<table border="1px" cellpadding="10px" cellspacing="1px">
			<tr>
				<td>CustomerName</td>
				<td><input type="text" name="name" value="<%=customer.getName()%>"></td>
			</tr>
			<tr>
				<td>Address</td>
				<td><input type="text" name="address"  value="<%=customer.getAddress()%>"/></td>
			</tr>
			<tr>
				<td>Phone</td>
				<td><input type="text" name="phone" value="<%=customer.getPhone()%>"/></td>
			</tr>
			<tr>
				<td colspan="2"><input  type="submit" value="update"/></td>
			</tr>
		</table>
	</form>
  </body>
</html>