实现了添加,查询,删除,修改。
先展示运行效果
注册界面
点击查询所有学生
在点击修改或删除会先弹出提示框,确认是否删除
确认之后就会删除掉了
点击修改后会跳转到修改界面,会将客户信息回显在输入框中
.jsp文件代码
查询所有学生界面
<%@page import="com.cxy.bean.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; 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; UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
function delstu(cid){
if(confirm("您确定删除该客户吗?")){
location.href="del?cid="+cid;
}
}
function setstu(cid){
if(confirm("您确定修改该客户吗?")){
location.href="revise?cid="+cid;
}
}
</script>
<body>
<table align='center' border='1px' cellspacing='0px' cellpadding='0px' width='80%'>
<tr><td>学生编号</td><td>学生姓名</td><td>学生性别</td><td>学生年龄</td><td>学生班级</td><td colspan='2'>操作</td></tr>
<%List<Student> list=(List<Student>)request.getAttribute("c");
for(int i=0;i<list.size();i++){
Student student=list.get(i);
%>
<tr><td><%=student.getId() %></td><td><%=student.getName() %></td><td><%=student.getSex() %></td><td><%=student.getAge() %></td><td><%=student.getClassname() %></td>
<td><a href="javascript:void(0);" onclick="delstu('<%=student.getId()%>')">删除</a></td>
<td><a href="javascript:void(0);" onclick="setstu('<%=student.getId()%>')">修改</a></td></tr>
<%}%>
</table>
</body>
</html>
修改界面
<%@page import="com.cxy.bean.Student"%>
<%@ page language="java" contentType="text/html; 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; UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>修改</h1>
<form action="set" method="get">
编号:<input type="text" name="userid" value="${sss.id}"><br>
姓名:<input type="text" name="username" value="${sss.name}"><br>
性别:<input type="text" name="usersex" value="${sss.sex}"><br>
年龄:<input type="text" name="userage" value="${sss.age}"><br>
班级:<input type="text" name="userclass" value="${sss.classname}"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
下面是部分servlet代码
修改的
package com.cxy.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.cxy.bean.Student;
import com.cxy.service.CustomerServiceImpl;
public class Reviseservlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
String id=req.getParameter("cid");
int cid=Integer.parseInt(id);
CustomerServiceImpl impl=new CustomerServiceImpl();
Student s=impl.aastu(cid);
//Student s=new Student("cxy", "sex", "age", "classname");
//impl.revise(s, 4);
//System.out.println(s);
//会话
HttpSession session=req.getSession();
session.setAttribute("sss", s);
session.setAttribute("cidd", cid);
req.getRequestDispatcher("revise32.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
<%@page import="com.cxy.bean.Student"%>
<%@ page language="java" contentType="text/html; 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; UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>修改</h1>
<form action="set" method="get">
编号:<input type="text" name="userid" value="${sss.id}"><br>
姓名:<input type="text" name="username" value="${sss.name}"><br>
性别:<input type="text" name="usersex" value="${sss.sex}"><br>
年龄:<input type="text" name="userage" value="${sss.age}"><br>
班级:<input type="text" name="userclass" value="${sss.classname}"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
删除的
package com.cxy.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cxy.service.CustomerServiceImpl;
public class Delservlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
String id=req.getParameter("cid");
CustomerServiceImpl impl=new CustomerServiceImpl();
int cid=Integer.parseInt(id);
boolean a=impl.delstudent(cid);
if(a){
PrintWriter writer=resp.getWriter();
writer.write("删除成功<br><a href='chaxun'>查询信息</a>");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
感谢观看