您的位置:首页 > 编程语言

图解:学生管理系统代码实现

2019-01-16 10:57 351 查看

1.效果图
2.流程图解
3.代码实现

一.效果图:

添加学生效果图:

删除学生效果图:

更新学生效果图

模糊查询效果图

分页效果图

二.流程图解

添加学生:

删除学生:

更新学生

查询学生:

模糊查询:

分页功能:

三.代码实现

数据库名称为:stus
表名称为:stu
属性为:sid,sname,gender,phone,birthday,hobby,info

src下,七个包,16个文件,一个c3p0配置文件:

需要导入的 6个 jar包(记得添加路径):

5个JSP文件:

c3p0配置文件

c3p0-config.xml(必须是这个名字!!!)

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost/stus</property>
<property name="user">root</property>
<property name="password">1234</property>

<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
</c3p0-config>

1.dao层

StudentDao.java:

package com.dao;

import java.sql.SQLException;
import java.util.List;
import com.domain.Student;

//这是对学生表的数据库操作

public interface StudentDao {

//查询所有学生
List<Student> findAll() throws SQLException;

//添加学生,需要添加到数据库的学生对象
void insert(Student student) throws SQLException;

//根据sid删除学生
void delete(int sid) throws SQLException;

//根据ID查询单个学生对象
Student findStudentById(int sid) throws SQLException;

//更新学生信息
void update(Student student) throws SQLException;

//模糊查询,根据姓名或者性别,或者两者兼有
List<Student> searchStudent(String sname,String sgender) throws SQLException;

//查询当页的学生数据
List<Student> findStudentByPage(int currentPage) throws SQLException;

int PAGE_SIZE = 5;//一页显示多少条纪录

//查询总的学生纪录数
int findCount() throws SQLException;
}

2. dao.impl层

StudentDaoImpl.java:

package com.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.dao.StudentDao;
import com.domain.Student;
import com.util.JDBCUtils;
import com.util.TestUtils;

//StudentDao的实现。针对前面定义的规范,做出具体的实现

public class StudentDaoImpl implements StudentDao {

//	查询所有学生

@Override
public List<Student> findAll() throws SQLException {

QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
String sql = "select * from stu";
List<Student> list = runner.query(sql, new BeanListHandler<Student>(Student.class));
return list;
}

@Override
public void insert(Student student) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
String sql = "insert into stu values(null,?,?,?,?,?,?)";
runner.update(sql,
student.getSname(),
student.getGender(),
student.getPhone(),
student.getBirthday(),
student.getHobby(),
student.getInfo()
);
}

@Override
public void delete(int sid) throws SQLException {

QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
String sql = "delete from stu where sid = ?";
runner.update(sql, sid);
}

@Override
public Student findStudentById(int sid) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
String sql = "select * from stu where sid = ?";
return runner.query(sql, new BeanHandler<Student>(Student.class),sid);

}

@Override
public void update(Student student) throws SQLException {

QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
runner.update("update stu set sname=? , gender=? , phone=? , birthday=? , hobby=? , info=?  where sid=?",
student.getSname(),
student.getGender(),
student.getPhone(),
student.getBirthday(),
student.getHobby(),
student.getInfo(),
student.getSid()
);

}

@Override
public List<Student> searchStudent(String sname, String sgender) throws SQLException {

QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());

/*
* 这里要分析一下:
* 	如果只有姓名 ,select * from stu where sname like ? ;
* 	如果只有性别 , select * from stu where gender = ?
*
* 如果两个都有 select * from stu where sname like ? and gender=?
*
* 如果两个都没有就查询所有。
*
*/

String sql = "select * from stu where 1=1 ";
List<String> list = new ArrayList<String>();

//判断有没有姓名,如果有,就组拼到sql语句里面
if(!TestUtils.isEmpty(sname)){
sql = sql+" and sname like ? ";
list.add("%" + sname + "%");
}
//判断有没有性别,如果有,就组拼到sql语句里面
if(!TestUtils.isEmpty(sgender)){
sql = sql+" and gender = ? ";
list.add(sgender);
}

return runner.query(sql, new BeanListHandler<Student>(Student.class),list.toArray());

}

@Override
public List<Student> findStudentByPage(int currentPage) throws SQLException {

QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
//第一个问号,代表一页返回多少条纪录,第二个问号,跳过前面的多少条纪录
return  runner.query("select * from stu limit ? offset ? ",
new BeanListHandler<Student>(Student.class) , PAGE_SIZE , (currentPage - 1) * PAGE_SIZE);
}

@Override
public int findCount() throws SQLException {

QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
//new ScalarHandler()  用于处理均值、总的个数...
Long result = (Long) runner.query("select count(*) from stu " , new ScalarHandler());
return result.intValue();
}
}

3. domain层

PageBean.java:

package com.domain;

import java.util.List;

//这是一个封装了分页的数据
//里面包含:
/*  该页的学生集合数据
总的纪录数
总的页数
当前页
每页显示的纪录数*/
public class PageBean<T> {

private int currentPage;  //当前页
private int totalPage;  //总页数
private int pageSize;   //每页的记录数
private int totalSize;   //总的纪录数
private List<T> list;   // 当前页的学生集合
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}

Student.java

package com.domain;

import java.util.Date;

//这是学生封装的对象bean

public class Student {

private int sid;
private String sname;
private String gender;
private String phone;
private String hobby;
private String info;
private Date birthday;

public Student() {
super();
// TODO Auto-generated constructor stub
}

public Student(int sid,String sname, String gender, String phone, String hobby, String info, Date birthday) {
super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.phone = phone;
this.hobby = hobby;
this.info = info;
this.birthday = birthday;
}

public Student(String sname, String gender, String phone, String hobby, String info, Date birthday) {
super();
this.sname = sname;
this.gender = gender;
this.phone = phone;
this.hobby = hobby;
this.info = info;
this.birthday = birthday;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}

@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", phone=" + phone + ", hobby="
+ hobby + ", info=" + info + ", birthday=" + birthday + "]";
}
}

4. service层

StudentService.java

package com.service;

import java.sql.SQLException;
import java.util.List;

import com.domain.PageBean;
import com.domain.Student;

//这是学生的业务处理规范

public interface StudentService {

//查询所有学生
List<Student> findAll() throws SQLException;

//添加学生,需要添加到数据库的学生对象
void insert(Student student) throws SQLException;

//根据sid删除学生
void delete(int sid) throws SQLException;

//根据ID查询单个学生对象
Student findStudentById(int sid) throws SQLException;

//更新学生信息
void update(Student student) throws SQLException;

//模糊查询,根据姓名或者性别,或者两者兼有
List<Student> searchStudent(String sname,String sgender) throws SQLException;

//查询当页的学生数据
PageBean findStudentByPage(int currentPage) throws SQLException;

}

5. service.impl层

StudentServiceImpl.java

package com.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.dao.StudentDao;
import com.dao.impl.StudentDaoImpl;
import com.domain.PageBean;
import com.domain.Student;
import com.service.StudentService;

//这是学生业务实现

public class StudentServiceImpl implements StudentService {

@Override
public List<Student> findAll() throws SQLException {

StudentDao dao = new StudentDaoImpl();
return dao.findAll();
}

@Override
public void insert(Student student) throws SQLException {
StudentDao dao = new StudentDaoImpl();
dao.insert(student);

}

@Override
public void delete(int sid) throws SQLException {

StudentDao dao = new StudentDaoImpl();
dao.delete(sid);

}

@Override
public Student findStudentById(int sid) throws SQLException {

StudentDao dao = new StudentDaoImpl();
return dao.findStudentById(sid);
}

@Override
public void update(Student student) throws SQLException {

StudentDao dao = new StudentDaoImpl();
dao.update(student);

}

@Override
public List<Student> searchStudent(String sname, String sgender) throws SQLException {

StudentDao dao = new StudentDaoImpl();
return dao.searchStudent(sname, sgender);
}

@Override
public PageBean findStudentByPage(int currentPage) throws SQLException {

//封装分页的该页数据
PageBean<Student> pageBean = new PageBean<Student>();

int pageSize = StudentDao.PAGE_SIZE;
pageBean.setCurrentPage(currentPage);   //设置当前页
pageBean.setPageSize(pageSize);   //设置每页显示多少条纪录

StudentDao dao = new StudentDaoImpl();
List<Student> list = new StudentDaoImpl().findStudentByPage(currentPage);
pageBean.setList(list);    //设置这一页的学生数据

//总的记录数,总的页数
int count = dao.findCount();
pageBean.setTotalSize(count);   //设置总的纪录数
pageBean.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1); //总页数

return pageBean;
}
}

6. Servlet层

AddServlet.java:

package com.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.domain.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

/**
* Servlet implementation class AddServlet
*/
public class AddServlet extends HttpServlet {

//用于处理学生的添加请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");

try {

//1.获取客户端提交上来的数据
String sname = request.getParameter("sname");
String gender = request.getParameter("gender");
String phone = request.getParameter("phone");
String birthday = request.getParameter("birthday");
//			String hobby = request.getParameter("hobby");  (getParameter只能得到一条数据)
String info = request.getParameter("info");
String [] h = request.getParameterValues("hobby");
//			[篮球,足球,写字]  ----->  篮球,足球,写字
String hobby = Arrays.toString(h);
hobby = hobby.substring(1, hobby.length()-1);

//2.添加到数据库
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
Student student = new Student(sname, gender, phone, hobby, info, date);
StudentService service = new StudentServiceImpl();
service.insert(student);

//3.跳转到列表页
//再查一次数据库,然后再装到作用域中,然后再跳转
//这里是直接跳转到页面上,那么这个页面会重新翻译一次,上面的那个request请求存放的数据就没有了
//			request.getRequestDispatcher("list.jsp").forward(request, response);
//			{所以这方法不行}

//			servlet除了能跳JSP之外,还能跳servlet
request.getRequestDispatcher("StudentListServlet").forward(request, response);
return;

} catch (Exception e) {
e.printStackTrace();
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}
}

DeleteServlet.java:

package com.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

//用于处理删除学生

public class DeleteServlet extends HttpServlet {

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

//		System.out.println(sid);

try {
//1.接收sid
int sid = Integer.parseInt(request.getParameter("sid"));//本来是String类型,强转int类型

//2.执行删除
StudentService service = new StudentServiceImpl();
service.delete(sid);

//3.跳转到列表页
request.getRequestDispatcher("StudentListServlet").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
}

EditServlet.java:

package com.servlet;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

//处理单个学生的更新,查询一个学生的信息,然后跳转到更新页面

public class EditServlet extends HttpServlet {

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

try {

//1.接收ID
int sid = Integer.parseInt(request.getParameter("sid"));

//2.查询学生数据
StudentService service = new StudentServiceImpl();
Student stu = service.findStudentById(sid);

//3.存数据
request.setAttribute("stu", stu);

//4.跳转
request.getRequestDispatcher("edit.jsp").forward(request, response);

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}

SearchStudentServlet.java

package com.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.domain.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

public class SearchStudentServlet extends HttpServlet {

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

request.setCharacterEncoding("UTF-8");

try {
//1.取到了要查询的关键数据:姓名,性别
String sname = request.getParameter("sname");
String sgender = request.getParameter("sgender");

//2.找service去查询
StudentService service = new StudentServiceImpl();
List<Student> list = service.searchStudent(sname, sgender);

//3.存数据
request.setAttribute("list", list);

//4.跳转
request.getRequestDispatcher("list.jsp").forward(request, response);
return;

} catch (SQLException e) {
e.printStackTrace();
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}
}

StudentListPageServlet.java

package com.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.domain.PageBean;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

public class StudentListPageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

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

try {

//1.获取需要显示的页码数
int currentPage = Integer.parseInt(request.getParameter("currentPage"));

//2.根据指定的页数,去获取该页的数据回来
StudentService service = new StudentServiceImpl();
PageBean pageBean = service.findStudentByPage(currentPage);

request.setAttribute("pageBean", pageBean);

//3.跳转页面
request.getRequestDispatcher("list_page.jsp").forward(request, response);

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}
}

StudentListServlet.java

package com.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

/**
* 负责查询所有的学生信息,然后呈现到list.jsp页面上。
*/
public class StudentListServlet extends HttpServlet {

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

try {
//			1.查询出来所有的学生
StudentService service = new StudentServiceImpl();
List<Student> list = service.findAll();

//			2.先把数据存储到作用域中
request.setAttribute("list", list);

//			3.跳转页面
request.getRequestDispatcher("list.jsp").forward(request, response);

} catch (SQLException e) {
e.printStackTrace();
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}
}

UpdateServlet.java:

package com.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

public class UpdateServlet extends HttpServlet {

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

request.setCharacterEncoding("UTF-8");

try {

//1.获取客户端提交上来的数据
int sid = Integer.parseInt(request.getParameter("sid"));
String sname = request.getParameter("sname");
String gender = request.getParameter("gender");
String phone = request.getParameter("phone");
String birthday = request.getParameter("birthday");
//			String hobby = request.getParameter("hobby");  (getParameter只能得到一条数据)
String info = request.getParameter("info");
String [] h = request.getParameterValues("hobby");
//			[篮球,足球,写字]  ----->  篮球,足球,写字
String hobby = Arrays.toString(h);
hobby = hobby.substring(1, hobby.length()-1);

Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
Student student = new Student(sid,sname, gender, phone, hobby, info, date);

//2.更新数据库数据
StudentService service = new StudentServiceImpl();
service.update(student);

//3.跳转
request.getRequestDispatcher("StudentListServlet").forward(request, response);

} catch (Exception e) {
e.printStackTrace();
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}
}

7. util层

JDBCUtils.java

package com.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCUtils {

static ComboPooledDataSource dataSource =null;

static{
dataSource = new ComboPooledDataSource();
}

public static DataSource getDataSource(){
return dataSource;
}

public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}

public static void close(Connection con,Statement stat){
if(stat != null){
try{
stat.close();
}catch(SQLException ex){};
}
if(con != null){
try{
con.close();
}catch(SQLException ex){};
}}
public static void close(Connection con,Statement stat,ResultSet rs){
if(stat != null){
try{
stat.close();
}catch(SQLException ex){};
}
if(con != null){
try{
con.close();
}catch(SQLException ex){};
}
if(rs != null){
try{
rs.close();
}catch(SQLException ex){};
}}}

TestUtils.java:

package com.util;

public class TestUtils {

public static boolean isEmpty(CharSequence s){
return s==null || s.length()==0;

}
}

8. JSP系列文件

list.jsp:

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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; charset=GBK">
<title>学生列表页面</title>

<script type="text/javascript">
function doDelete(sid) {

var flag = confirm("是否确定删除?");
if(flag){
window.location.href="DeleteServlet?sid="+sid
}
}

</script>
</head>
<body>
<form action="SearchStudentServlet" method="post">
<table border="1" width="700">

<tr>
<td colspan="8">

按姓名查询:<input type="text" name="sname">
&nbsp;
按性别查询:<select name="sgender">
<option value="">--请选择--
<option value="男">男
<option value="女">女
</select>
&nbsp;&nbsp;&nbsp;
<input type="submit" value="查询">
&nbsp;&nbsp;&nbsp;
<a href="add.jsp">添加</a>
</td>
</tr>

<tr align="center">
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>电话</td>
<td>生日</td>
<td>爱好</td>
<td>简介</td>
<td>操作</td>
</tr>

<c:forEach items="${list }" var="stu">
<tr align="center">
<td>${stu.sid }</td>
<td>${stu.sname }</td>
<td>${stu.gender }</td>
<td>${stu.phone }</td>
<td>${stu.birthday }</td>
<td>${stu.hobby }</td>
<td>${stu.info }</td>
<td><a href="EditServlet?sid=${stu.sid }">更新</a>  <a href="#" onclick="doDelete(${stu.sid});">删除</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>

add.jsp

<%@ page language="java" contentType="text/html; charset=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; charset=UTF-8">
<title>添加学生页面</title>
</head>
<body>
<h3>添加学生页面</h3>
<form method="post" action="AddServlet">
<table border="1" width="700">
<tr>
<td>姓名</td>
<td><input type="text" name="sname" /></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女" />女
</td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="phone"/></td>
</tr>
<tr>
<td>生日</td>
<td><input type="date" name="birthday"/></td>
</tr>
<tr>
<td>爱好</td>
<td>
<input type="checkbox" name="hobby" value="游泳"/>游泳
<input type="checkbox" name="hobby" value="篮球"/>篮球
<input type="checkbox" name="hobby" value="足球"/>足球
<input type="checkbox" name="hobby" value="看书"/>看书
<input type="checkbox" name="hobby" value="写字"/>写字
</td>
</tr>
<tr height="50" >
<td>简介</td>
<td><textarea name="info" rows="3" cols="30"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加" /></td>
</tr>
</table>
</form>
</body>
</html>

edit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!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; charset=UTF-8">
<title>更新学生页面</title>
</head>
<body>
<h3>更新学生页面</h3>
<form method="get" action="UpdateServlet">
<input type="hidden" name="sid" value="${stu.sid}">
<table border="1" width="700">
<tr>
<td>姓名</td>
<td><input type="text" name="sname" value="${stu.sname }"></td>
</tr>
<tr>
<td>性别</td>
<td>
<!-- 	如果性别是男的,可以在男的性别input标签里面,出现check,
如果性别是女的,可以在女的性别input标签里面,出现check, -->
<input type="radio" name="gender" value="男" <c:if test="${stu.gender == '男' }">checked</c:if>>男
<input type="radio" name="gender" value="女" <c:if test="${stu.gender == '女' }">checked</c:if>>女
</td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="phone" value="${stu.phone }"></td>
</tr>
<tr>
<td>生日</td>
<td><input type="date" name="birthday" value="${stu.birthday }"></td>
</tr>
<tr>
<td>爱好</td>
<td>
<input type="checkbox" name="hobby" value="游泳" <c:if test="${fn:contains(stu.hobby,'游泳') }">checked</c:if>>游泳
<input type="checkbox" name="hobby" value="篮球" <c:if test="${fn:contains(stu.hobby,'篮球') }">checked</c:if>>篮球
<input type="checkbox" name="hobby" value="足球" <c:if test="${fn:contains(stu.hobby,'足球') }">checked</c:if>>足球
<input type="checkbox" name="hobby" value="看书" <c:if test="${fn:contains(stu.hobby,'看书') }">checked</c:if>>看书
<input type="checkbox" name="hobby" value="写字" <c:if test="${fn:contains(stu.hobby,'写字') }">checked</c:if>>写字
</td>
</tr>
<tr height="50" >
<td>简介</td>
<td><textarea name="info" rows="3" cols="30">${stu.info }</textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="更新" /></td>
</tr>
</table>
</form>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=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; charset=ISO-8859-1">
<title>首页</title>
</head>
<body>
<h3><a href="StudentListServlet">显示所有的学生列表</a></h3>
<h3><a href="StudentListPageServlet?currentPage=1">分页显示所有的学生</a></h3>
</body>
</html>

list_page.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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; charset=UTF-8">
<title>学生列表页面</title>

<script type="text/javascript">
function doDelete(sid) {

var flag = confirm("是否确定删除?");
if(flag){
window.location.href="DeleteServlet?sid="+sid
}
}

</script>
</head>
<body>
<form action="SearchStudentServlet" method="post">
<table border="1" width="700">

<tr>
<td colspan="8">

按姓名查询:<input type="text" name="sname">
&nbsp;
按性别查询:<select name="sgender">
<option value="">--请选择--
<option value="男">男
<option value="女">女
</select>
&nbsp;&nbsp;&nbsp;
<input type="submit" value="查询">
&nbsp;&nbsp;&nbsp;
<a href="add.jsp">添加</a>
</td>
</tr>

<tr align="center">
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>电话</td>
<td>生日</td>
<td>爱好</td>
<td>简介</td>
<td>操作</td>
</tr>

<c:forEach items="${pageBean.list }" var="stu">
<tr align="center">
<td>${stu.sid }</td>
<td>${stu.sname }</td>
<td>${stu.gender }</td>
<td>${stu.phone }</td>
<td>${stu.birthday }</td>
<td>${stu.hobby }</td>
<td>${stu.info }</td>
<td><a href="EditServlet?sid=${stu.sid }">更新</a>  <a href="#" onclick="doDelete(${stu.sid});">删除</a></td>
</tr>
</c:forEach>

<tr>
<td colspan="8">
第${pageBean.currentPage } / ${pageBean.totalPage }
&nbsp;&nbsp;
每页显示${pageBean.pageSize }条   &nbsp;&nbsp;&nbsp;
总的纪录数${pageBean.totalSize }   &nbsp;&nbsp;&nbsp;
<c:if test="${pageBean.currentPage !=1 }">
<a href="StudentListPageServlet?currentPage=1">首页  |
<a href="StudentListPageServlet?currentPage=${pageBean.currentPage-1 }">上一页
</c:if>

&nbsp;&nbsp;&nbsp;

<c:forEach begin="1" end="${pageBean.totalPage }" var="i">
<c:if test="${pageBean.currentPage == i }">
${i }
</c:if>
<c:if test="${pageBean.currentPage != i }">
<a href="StudentListPageServlet?currentPage=${i }">${i }</a>
</c:if>

</c:forEach>

&nbsp;&nbsp;&nbsp;

<c:if test="${pageBean.currentPage != pageBean.totalPage}">
<a href="StudentListPageServlet?currentPage=${pageBean.currentPage+1 }">下一页</a>  |
<a href="StudentListPageServlet?currentPage=${pageBean.totalPage }">尾页</a>
</c:if>
</td>
</tr>

</table>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: