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>
<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中如果找不到指定的方法我们可以通过相关方式指定到该页面,所以这样可以更好的对用户提供更为方便的显示<%@ 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 '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页面,我们可以在该页面中进行对数据的修改。然后提交到数据库中予以保存5.对数据进行更新操作我们可以建立一个updateCustomer.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>
<%@ 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>