一实验题目
在线学生管理系统
二开发背景简介
随着网络技术的发展和普及许多以前基于CS的应用都开始向BS方向发展,而Java在这方面的应用有着许多其他技术不具备的优点,Java开发Web应用的主要技术是Servlet和Jsp技术,其实Jsp只Servlet的一种进化方便了开发者的使用。它们都具有强大的功能适用于许多大型项目的开发。在未来基于BS的应用也必将得到较好的发展。本实验是在讲完Jsp和Servlet课程后为掌握所学的知识而做的实验。
三开发工具
本次试验是在Windows平台下开发采用的集成开发工具是Myeclipse8.5,数据库使用的是MySql5.5,服务器采用的是Tomcat7.0。
四实验设计
在本实验中模仿的是MVC设计模式,但由于系统比较小所以省略了业务层,在页面层中直接调用了数据层。在做这个在线学生管理系统中业务基本上都是由Jsp页面完成的没有用到Servlet控制器。
五实现的功能
1. 登陆界面
登录可以选择是身份,可以是学生,老师,和管理员。
登录界面是login.html,它想login.jsp页面传了3个参数来进行页面的验证,其中验证的部分代码是,
String uname = request.getParameter("userName");
String pwd = request.getParameter("userPass");
int i =Integer.parseInt(request.getParameter("identity"));
String sql =
"select * from userManger where username='"+uname+"' and userpassword='"+pwd+"'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
session.setAttribute("userId",rs.getString("id"));
session.setAttribute("userMark",rs.getString("identitymark"));
response.sendRedirect("index.jsp");
}else{
response.sendRedirect("2.html");
}
2. 其中系统用的学生表是
这是学生表里面的初始化的数据,你可以看到登录的用户名和密码
3. 如果你输入登录的密码不正确,就会跳转到错误界面
4. 这是学生表的登录成功的界面在这页面显示用户名称和上次登录的时间,这里是从数据库里面查询的,用到了session的功能。当然这个系统也可以在一台电脑上登录多个用户,多个用户之间是不会发生冲突的,因为链接的代码是学生的id号。
int userId=Integer.parseInt(session.getAttribute("userId").toString());
int userMark =Integer.parseInt(session.getAttribute("userMark").toString());
String sql ="select * from student_table where id=? and identitymark=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,userId);
ps.setInt(2,userMark);
ResultSet rs=ps.executeQuery();
rs.next();
取得用户的显示的信息的代码如下:
<div id="header">
<font class="font1" color="white"> 你好,
<img src="userfile/userimage/messenger_people.png" style="width:20px;height:20px" >
<font color="#FF00FF">
<%=rs.getString("username")%>【管理员】
</font>
欢迎登录 你上次登录的时间是:
<font color="#ffooff">
****年*月*日
</font>
</font>
</div>
其中的页面的代码是:
<%@ page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<%@ include file="../../conn.jsp"%>
<%
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
%>
<html>
<head>
</head>
<%
int userId=Integer.parseInt(request.getParameter("id"));
int userMark =Integer.parseInt(request.getParameter("mark"));
String sql ="select * from student_table where id=? and identitymark=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,userId);
ps.setInt(2,userMark);
ResultSet rs=ps.executeQuery();
rs.next();
%>
<script>
function judgeSex()
{
//alert(<%=rs.getInt(4)%>);
var sel=document.getElementById("sex");
if(<%=rs.getInt(4)%>==0){
sel.innerText="男";
}else{
sel.innerText="女";
}
}
</script>
<body onload="judgeSex()" background="../../images/bg4.gif">
<table border="1" align="center" width="600" height="200" background="../../images/bg.bmp" bordercolorlight=#72C8D4 bordercolordark=#ffffff cellpadding="0" cellspacing="0">
<caption><b>个人基本信息表</b></caption>
<tr>
<td >姓名:</td>
<td><%=rs.getString(2)%></td>
<td rowspan="5" style="width:100px;height:150px;"width="100" height="150" ><img src="../../<%= rs.getString(11)%>"></td>
</tr>
<tr>
<td >性别:</td>
<td><div id="sex"></div></td>
</tr><!--辅导员,照片,爱好,身份标记,备注-->
<tr>
<td >年龄:</td>
<td><%=rs.getString(5)%></td>
</tr>
<tr>
<td>手机:</td>
<td><%=rs.getString(6)%></td>
</tr>
<tr>
<td >所属系名称:</td>
<td><%=rs.getString(8)%></td>
</tr>
<tr>
<td>家庭住址:</td>
<td colspan="2"><%=rs.getString(7)%></td>
</tr>
<tr>
<td>辅导员:</td>
<td colspan="2"><%=rs.getString(9)%></td>
</tr>
<tr>
<td>宿舍号:</td>
<td colspan="2"><%=rs.getString(10)%></td>
</tr>
<tr>
<td>爱好:</td>
<td colspan="2"><%=rs.getString(12)%></td>
</tr>
<tr>
<td>备注:</td>
<td colspan="2"><%=rs.getString(14)%></td>
</tr>
</table>
</body>
</html>
老师登录的时候在身份栏里面选择教师就行了,当然必须要用户名和密码必须正确,不然又跳转到errorr.html了
修改的页面用的代码:
<%@ page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<%@ include file="conn.jsp"%>
<%
request.setCharacterEncoding("GBK");
String id = request.getParameter("id");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String phone = request.getParameter("phone");
String zy = request.getParameter("zy");
String qq = request.getParameter("qq");
String sql = "update student set name='"+name+"',sex='"+sex+"',age="+age+",qq='"+qq+"',phone='"+phone+"',specialty='"+zy+"' where id="+id;
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
response.sendRedirect("index.jsp");
%>
5. 当然我们可以对上面的信息进行删除,比如我们还是删除第一条记录,我们删除后,就是这样了。
<%@ page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<%@ include file="conn.jsp"%>
<%
String id = request.getParameter("id");
String sql = "delete from student where id="+id;
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
response.sendRedirect("index.jsp");
%>
在添加页面用到了页面的验证,我写到了一个通用的js文件里面了,代码如下:
/*
* 判断是否为空!第一个参数是id,第二个参数是判断不为空的元件名称。
*
*/
function checkIsEmpty(id,message)
{
var flag=true;
if(document.getElementById(id).value=="")
{
alert(message+"不能为空!");
//鼠标的焦点停留在那个文本框,并设置背景色为红色
document.getElementById(id).focus();
document.getElementById(id).style.backgroundColor="red";
flag=false;
}else{
document.getEmelentById(id).style.backgroundColor="white";
}
return flag;
}
/*
*让文本框输入只能是数字
*
*/
function checkNumber()
{
if(event.keyCode>57||event.keyCode<48)
return false;
else{
return true;
}
}
/*
*检查文本输入框的字符的长度,要传两个参数,一个是id,一个是限制的长度。
*
*/
function checkLength(id,len)
{
//alert(document.getElementById(id).value.length+","+len);
if(document.getElementById(id).value.length>len)
{
alert("长度必须小于"+len+"位!");
document.getElementById(id).focus();
document.getElementById(id).style.backgroundColor="red";
return false;
}
else{
document.getElementById(id).style.backgroundColor="white";
return true;
}
}
6. 这里面有课程号和课程名称,都是从数据库中取的。
修改会产生修改记录,我们也保存起来了,以供日后查询使用。
7. 学生的成绩表在数据库中是这样的。这里是学号和课程号得键值对。
8. 查询后得到登录的信息表。它也是从数据库中取得的。
七.实验代码【部分主要功能的代码】
1.我用到的的jsp链接的代码是:
<%@ page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/student";
String uid = "root";
String psd = "******";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,uid,psd);
%>
2.常用的更新页面的代码(这是更新学生信息表的,admin和老师用户都要用它,所以我写在外面了):
<%@ page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<%@ include file="../../conn.jsp"%>
<%
int id = Integer.parseInt(request.getParameter("id"));
//由于得到的中文会出现乱码的现象,所以要进行转码的工作
String stuname =new String(request.getParameter("txtName").getBytes("ISO8859-1"),"gb2312");
String stupassword =new String(request.getParameter("txtPassword").getBytes("ISO8859-1"),"gb2312");
int age = Integer.parseInt(request.getParameter("chkAge"));
String mobile=new String(request.getParameter("txtMobile").getBytes("ISO8859-1"),"gb2312");
String home_address =new String(request.getParameter( "txtHomeAddress").getBytes("ISO8859-1"), "gb2312");
String series_name=new String(request.getParameter("txtSeriesName").getBytes("ISO8859-1"),"gb2312");
String counsellor=new String(request.getParameter("txtCounsellor").getBytes("ISO8859-1"),"gb2312");
String dorm=new String(request.getParameter("txtDorm").getBytes("ISO8859-1"),"gb2312");
//由于没有解决拷贝文件的方法,所以暂时 不更新照片的路径。
//String photo= request.getParameter("txtPhoto");
//String hobby=new String(request.getParameter("chkHobby").getBytes("ISO8859-1"),"gb2312");
String hobby="";
String[] boxes=request.getParameterValues("chkHobby");
//循环取得复选框里面的值
if (boxes != null && boxes.length != 0) {
for(int i=0;i<boxes.length;i++){
if(i!=(boxes.length-1)){
hobby+=boxes[i]+",";
}else{
hobby+=boxes[i];
}
}
}else{
hobby="the student no have anything hobby!";
}
hobby=new String(hobby.getBytes("ISO8859-1"),"gb2312");
String commentinfo=new String(request.getParameter("txaCommentInfo").getBytes("ISO8859-1"),"gb2312");
//response.setContentType("text/html;charset=gb2312");
//out.print(stuname+","+stupassword+","+age+","+mobile+","+home_address+","+series_name+","+counsellor+","+dorm+","+hobby+","+commentinfo+","+id);
String sql="update student_table set stuname=?,stupassword=?,age=?,mobile=?,home_address=?,series_name=?,counsellor=?,dorm=?,hobby=?,commentinfo=? where id=?;";
PreparedStatement ps =conn.prepareStatement(sql);
//对上面的问号进行赋值操作
ps.setString(1,stuname);
ps.setString(2,stupassword);
ps.setInt(3,age);
ps.setString(4,mobile);
ps.setString(5,home_address);
ps.setString(6,series_name);
ps.setString(7,counsellor);
ps.setString(8,dorm);
ps.setString(9,hobby);
ps.setString(10,commentinfo);
ps.setInt(11,id);
int rows=0;
rows=ps.executeUpdate();
if(rows!=0){
out.print("ok");
}else{
out.print("error");
}
%>
3.用到重文件中解析学生信息的代码:
package excel;
import java.io.File;
import java.util.ArrayList;
import beans.Student;
import jxl.Sheet;
import jxl.Workbook;
public class ImportDataFromExcel {
public static void main(String args[]){
ImportDataFromExcel data = new ImportDataFromExcel();
String fileName = "学生信息.XLS";
ArrayList<Student> students = data.getStuentInfo(fileName,"04计算机");
for(Student stu:students){
System.out.println(stu.toString());
}
}
public Workbook getWorkBook(String fileName) throws Exception{
Workbook workbook = Workbook.getWorkbook(new File(fileName));
return workbook;
}
public ArrayList getStuentInfo(String fileName, String sheetName){
Workbook workbook = null;
try{
workbook = getWorkBook(fileName);
}catch(Exception e){
System.out.println(e.toString());
}
if(workbook == null)
return null;
Sheet sheet = workbook.getSheet(sheetName);
ArrayList<Student> students = new ArrayList<Student>();
for(int i=3;i<11;i++){
Student stu = new Student();
stu.setSid(sheet.getCell(1,i).getContents());
stu.setSname(sheet.getCell(2,i).getContents());
stu.setSex(sheet.getCell(3,i).getContents());
stu.setDepartment(sheet.getCell(4,i).getContents());
stu.setMajor(sheet.getCell(5,i).getContents());
students.add(stu);
}
return students;
}
}
4.在index.html用到了css样式表的控制,代码如下:
#container{
width:100%;
background:url(../images/bg4.gif);
margin:auto;
}
#nav{
background:url(../images/bg1.jpg);
height:70px;
}
#header{
background:url(../images/bg2.jpg);
height:25px;
}
#left{
float:left;
width:175px;
background:url(../images/bg3.jpg);
}
#right{
float:left;
}
#bottom{
clear:both;
background:#BACEEF;
height:50px;
}
html,body{
font-size:14/16px;
font-family:Arial,宋体,微软雅黑;
}
.font1{
font-size:14px;
}
.font2{
font-size:16px;
}
a{
font-size:14px;
font-family:华文中宋;
}
a:link{
color:#1B58A6;
text-decoration:none;
}
a:visited{
color:#728EB3;
text-decoration:none;
}
a:hover{
color:red;
text-decoration:underline;
}
a:active{
color:#BAD0E3;
text-decoration:none;
}
八.实验心得体会
总的来说从这个实验中我学到了不少的东西,比如如何保存用登录,我刚开始一直不会用session但是通过查询资料看了课本后,我知道了,我学会了独立解决问题的能力,遇到的问题主要是中文信息乱码的问题,不过最后知道用设置字符默认的编码方式,所以问题就解决了,我从这个实验中学到的还有很多,确实从中学到了不少东西。