JSP删除增加的问题

qiyetianya 2013-08-10 02:26:47
新手上路,做个表格,没有连接数据库
首先,我表格里有10条数据
public class VMDB {
static List<Student> stus = new ArrayList<Student>();

static {
stus.add(new Student(1,"刘睿","1234"));
stus.add(new Student(1,"刘睿","1234"));
stus.add(new Student(1,"刘睿","1234"));
stus.add(new Student(1,"刘睿","1234"));
stus.add(new Student(1,"刘睿","1234"));
stus.add(new Student(1,"刘睿","1234"));
stus.add(new Student(1,"刘睿","1234"));
stus.add(new Student(1,"刘睿","1234"));
stus.add(new Student(1,"刘睿","1234"));
stus.add(new Student(1,"刘睿","1234"));
}

public static List<Student> findAll(){
return stus;
}
通过stus传的,然后通过java页面的request传到jsp页面去
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setAttribute("abc",VMDB.findAll() );
req.getRequestDispatcher("/list.jsp").forward(req, resp);
jsp页面是这么接收的
List<Student> stus =(List<Student>) request.getAttribute("abc") ;
我就是想问,我表格是想通过选择复选框删除,可以选择多个,我看过网上的说吧name设置为一样的,再接收,接收之后呢?然后这个不在jsp页面,在java页面可以写吗?最好给个简易的代码。增加一个用户,用什么增加,这个我是新手,不太明白,虽然学过C语言,但是觉得和java不一样
...全文
195 点赞 收藏 9
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
效林少爷 2013-08-12
引用 3 楼 longtian1213 的回复:
算了,在这里给你把! VMDB

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class VMDB {
	static List<Student> stus = new ArrayList<Student>();

	static {
		for(int i = 0 ; i < 10 ; i++){
			stus.add(new Student(i, "刘睿" + i, "123" + i));		
		}
	}

	public static List<Student> findAll() {
		return stus;
	}
	public static void add(Student s){
		stus.add(s);
	}
	public static void deleteById(Integer id){
		Iterator<Student> it = stus.iterator();
		while(it.hasNext()){
			Student s = it.next();
			if(s.getId().equals(id)){
				it.remove();
			}
		}
	}
}
ListServlet

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute("stus", VMDB.findAll());
		request.getRequestDispatcher("/list.jsp").forward(request, response);

	}
list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>list</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>
	<form action="servlet/DeleteServlet" method="post">
		<table width="100%" border="1">
		<tr>
			<td colspan="4">
				<a href="add.jsp">新增</a>
				<a href="javascript:void(0)" onclick="deleteSelect()">删除选中</a>
			</td>
		</tr>
			<c:forEach var="stu" items="${stus}">
				<tr>
					<td>${stu.id}</td>
					<td>${stu.name}</td>
					<td>${stu.phone}</td>	
					<td>
					  <input type="checkbox" name="id" value="${stu.id}">
					</td>		
				</tr>
			</c:forEach>
		</table>
	</form>
  </body>
  <script type="text/javascript">
	function deleteSelect(){
		if(confirm("是否确定删除?")){
			document.forms[0].submit();			
		}
	}  
  </script>
</html>

add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>add</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>
	<form action="servlet/AddServlet" method="post">
		<table width="100%" border="1">
			<tr>
				<td>ID</td>
				<td>
					<input name="id" type="text">
				</td>
			</tr>
			<tr>
				<td>姓名</td>
				<td>
					<input type="text" name="name">
				</td>
			</tr>
			<tr>
				<td>电话</td>
				<td>
					<input type="text" name="phone">
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="submit" value="保存">
				</td>
			</tr>
		</table>
	</form>
  </body>
</html>

AddServlet

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		Student stu = new Student(Integer.valueOf(id), name, phone);
		VMDB.add(stu);
		response.sendRedirect("../servlet/ListServlet");
	}
DeleteServlet

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String[] ids = request.getParameterValues("id");
		for(String id : ids){
			VMDB.deleteById(Integer.valueOf(id));
		}
		response.sendRedirect("../servlet/ListServlet");
	}
你应该看的懂把!
回复
S117 2013-08-12
引用 6 楼 qiyetianya 的回复:
你给的代码很好用,谢啦,但是我有个问题:在VMDB中已经有10个人了,可是在我运行list的时候,这10个人并没传到jsp页面上,如下: 然后在点完新增的提交之后,会出现这个问题: 这个问题如何解决?
你的servlet路径写错啦
回复
yjyzsl 2013-08-11
引用 6 楼 qiyetianya 的回复:
你给的代码很好用,谢啦,但是我有个问题:在VMDB中已经有10个人了,可是在我运行list的时候,这10个人并没传到jsp页面上,如下: 然后在点完新增的提交之后,会出现这个问题: 这个问题如何解决?
是找不到资源,come/servlet/AddServlet请求找不到
回复
我的波塞冬 2013-08-11
回复
qiyetianya 2013-08-11
你给的代码很好用,谢啦,但是我有个问题:在VMDB中已经有10个人了,可是在我运行list的时候,这10个人并没传到jsp页面上,如下:

然后在点完新增的提交之后,会出现这个问题:

这个问题如何解决?
回复
吴红军 2013-08-11
QQ:54963779,详细告诉你呗。
回复
S117 2013-08-10
算了,在这里给你把! VMDB

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class VMDB {
	static List<Student> stus = new ArrayList<Student>();

	static {
		for(int i = 0 ; i < 10 ; i++){
			stus.add(new Student(i, "刘睿" + i, "123" + i));		
		}
	}

	public static List<Student> findAll() {
		return stus;
	}
	public static void add(Student s){
		stus.add(s);
	}
	public static void deleteById(Integer id){
		Iterator<Student> it = stus.iterator();
		while(it.hasNext()){
			Student s = it.next();
			if(s.getId().equals(id)){
				it.remove();
			}
		}
	}
}
ListServlet

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute("stus", VMDB.findAll());
		request.getRequestDispatcher("/list.jsp").forward(request, response);

	}
list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>list</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>
	<form action="servlet/DeleteServlet" method="post">
		<table width="100%" border="1">
		<tr>
			<td colspan="4">
				<a href="add.jsp">新增</a>
				<a href="javascript:void(0)" onclick="deleteSelect()">删除选中</a>
			</td>
		</tr>
			<c:forEach var="stu" items="${stus}">
				<tr>
					<td>${stu.id}</td>
					<td>${stu.name}</td>
					<td>${stu.phone}</td>	
					<td>
					  <input type="checkbox" name="id" value="${stu.id}">
					</td>		
				</tr>
			</c:forEach>
		</table>
	</form>
  </body>
  <script type="text/javascript">
	function deleteSelect(){
		if(confirm("是否确定删除?")){
			document.forms[0].submit();			
		}
	}  
  </script>
</html>

add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>add</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>
	<form action="servlet/AddServlet" method="post">
		<table width="100%" border="1">
			<tr>
				<td>ID</td>
				<td>
					<input name="id" type="text">
				</td>
			</tr>
			<tr>
				<td>姓名</td>
				<td>
					<input type="text" name="name">
				</td>
			</tr>
			<tr>
				<td>电话</td>
				<td>
					<input type="text" name="phone">
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="submit" value="保存">
				</td>
			</tr>
		</table>
	</form>
  </body>
</html>

AddServlet

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		Student stu = new Student(Integer.valueOf(id), name, phone);
		VMDB.add(stu);
		response.sendRedirect("../servlet/ListServlet");
	}
DeleteServlet

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String[] ids = request.getParameterValues("id");
		for(String id : ids){
			VMDB.deleteById(Integer.valueOf(id));
		}
		response.sendRedirect("../servlet/ListServlet");
	}
你应该看的懂把!
回复
S117 2013-08-10
闲着帮你简单写了个 留给邮箱我把代码发给你
回复
下一秒地狱 2013-08-10
没看明白你的问题。
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告