JavaWeb实现学生信息管理系统(2)
本文接着,继续为大家分享了javaweb实现学生信息管理系统的第二篇,供大家参考,具体内容如下
今日任务:实现学生管理系统的查找和添加功能!
一、查询学生信息
1. index.jsp
先写一个jsp页面【webcontent/index.jsp】,里面放一个超链接
<a href="studentlistservlet" rel="external nofollow" >显示所有学生列表</a>
2. studentlistservlet.java
写studentlistservlet【com.servlet包下的studentlistservlet.java】,接受请求,去调用service,再由service调用dao
package com.servlet; import java.io.ioexception; import java.sql.sqlexception; import java.util.list; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; 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页面上 * */ @webservlet("/studentlistservlet") 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.先把数据存储到作用域中 //3..跳转页面 } catch (sqlexception e) { e.printstacktrace(); } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request, response); } }
3. studentdao.java & studentdaoimpl.java
3.1 studentdao.java【com.dao包下】
package com.dao; import java.sql.sqlexception; import java.util.list; import com.domain.student; /** * 这是针对学生表的数据访问 * @author administrator * */ public interface studentdao { /** * 查询所有学生 * @return list<student> */ list<student> findall() throws sqlexception; }
3.2 studentdaoimpl.java【com.dao.impl包下】
实现studentdao里的findall()方法。
public class studentdaoimpl implements studentdao { /** * 查询所有学生 * @throws sqlexception */ @override public list<student> findall() throws sqlexception { queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); string sql = "select * from stu"; list<student> list = runner.query(sql, new beanlisthandler<student>(student.class)); return list; } }
4. studentservice.java 和 studentservice.java
4.1 studentservice.java
代码同studentdao.java,
public interface studentservice { /** * 查询所有学生 * @return list<student> * */ list<student> findall() throws sqlexception; }
4.2 studentservice.java
public class studentserviceimpl implements studentservice{ @override public list<student> findall() throws sqlexception { studentdao dao = new studentdaoimpl(); return dao.findall(); } }
5. 在studentlistservlet存储数据,并作出页面响应
//2.先把数据存储到作用域中 request.setattribute("list", list); //3..跳转页面 request.getrequestdispatcher("list.jsp").forward(request, response);
6. list.jsp
在list.jsp【webcontent/list.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> <html> <head> <meta charset="utf-8"> <title>学生列表页面 </title> </head> <body> <table border="1" width="700"> <tr> <td colspan="8"> <a href="add.jsp" rel="external nofollow" >添加</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="#" rel="external nofollow" rel="external nofollow" >更新</a> <a href="#" rel="external nofollow" rel="external nofollow" >删除</a></td> </tr> </c:foreach> </table> </body> </html>
7. 查找结果如下:
二、添加学生信息
1. add.jsp
我们需要先跳转到增加页面,编写增加页面add.jsp【webcontent/add.jsp】
<body> <h3>添加学生页面</h3> <form action="addservlet" method="post"> <table border="1" width="600"> <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="text" 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> <td>简介</td> <td> <textarea rows="3" cols="20" name="info"></textarea> </td> </tr> <tr> <td colspan="2"><input type="submit" value="添加"></td> </tr> </table> </form> </body>
实现结果如下:
2. addservlet.java
点击添加,提交数据到addservlet,处理数据。
【备:com.servlet包下的addservlet.java】
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"); //传过来是1989-10-18 string info = request.getparameter("info"); //string hobby = request.getparameter("hobby"); string [] h = request.getparametervalues("hobby"); //[篮球,足球,写字]-----篮球,足球,写字 string hobby = arrays.tostring(h); hobby = hobby.substring(1,hobby.length()-1); //2.添加到数据库 //string-------date 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); } catch (exception e) { e.printstacktrace(); } }
3. studentdao & studentservice
/** * 添加学生 * @param student 需要添加到数据库的学生对象 * @throws sqlexception */ void insert(student student) throws sqlexception;
4. dao & service的实现
4.1 studentdaoimpl.java
@override public void insert(student student) throws sqlexception { queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); string sql = "insert into stu values(null,?,?,?,?,?,?)"; runner.update(sql, student.getsname(), student.getgender(), student.getphone(), student.getbirthday(), student.gethobby(), student.getinfo() ); }
4.2 studentserviceimpl.java
@override public void insert(student student) throws sqlexception { studentdao dao = new studentdaoimpl(); dao.insert(student); }
5. 注意
完成了上述存储工作之后,需要跳转到列表页面,这里不能直接跳转到列表页面,否则没有什么内容显示。应该先跳转到查询所有学生信息的那个servlet,即studentlistservlet,再由这个servlet跳转到列表页面。
request.getrequestdispatcher("studentlistservlet").forward(request, response);
hobby的value有多个值。处理时需多次转化:
//string hobby = request.getparameter("hobby"); string [] h = request.getparametervalues("hobby"); //[篮球,足球,写字]-----篮球,足球,写字 string hobby = arrays.tostring(h); hobby = hobby.substring(1,hobby.length()-1);
6. 添加结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。