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>






















  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uniquewdl

匆忙的人生,总有你喜欢的文章

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值