JavaWeb学生成绩管理系统

基于JavaWeb开发的学生成绩,使用MyEclipse2014开发,连接MySQL数据库,存储学生的身份信息、成绩,管理员的身份信息,课程信息。

功能说明:

学生注册。学生身份:跳转到学生登录界面,点击查询成绩按钮,以表格的形式在该页面显示该学生所有成绩。管理员身份:进入功能选择页面,以列表形式显示超链接,录入成绩、修改成绩、查询成绩。录入成绩页面中,管理员输入学号、课程号和成绩进行录入,点击添加跳出弹框提示是否录入成功;查询成绩界面,可选择按学号查询和按课程号查询,在页面下方以表格形式显示成绩;修改成绩界面,先按学号或课程号查询出成绩,显示当前成绩,可进行修改、删除操作,跳出弹框显示是否修改成功。

1.编写JavaBean,保存信息:

1.1学生信息Student.java

package beans;

public class Student {
	private String studentid;
	private String studentname;
	private String password;
	public Student(){}
	public Student(String studentid,String studentname,String password){
		this.studentid=studentid;
		this.studentname=studentname;
		this.password=password;
	}
	public String getStudentid(){
		return studentid;
	}
	public void setStudentid(String studentid){
		this.studentid=studentid;
	}
	public String getStudentname(){
		return studentname;
	}
	public void setStudentname(String studentname){
		this.studentname=studentname;
	}
	public String getPassword(){
		return password;
	}
	public void setPassword(String password){
		this.password=password;
	}
}

1.2管理员信息Admin.java

package beans;

public class Admin {
	private String adminid;
	private String adminname;
	private String password;
	public Admin(){}
	public Admin(String adminid,String adminname,String password){
		this.adminid=adminid;
		this.adminname=adminname;
		this.password=password;
	}
	public String getAdminid(){
		return adminid;
	}
	public void setAdminid(String studentid){
		this.adminid=studentid;
	}
	public String getAdminname(){
		return adminname;
	}
	public void setAdminname(String studentname){
		this.adminname=studentname;
	}
	public String getPassword(){
		return password;
	}
	public void setPassword(String password){
		this.password=password;
	}
}

1.3课程信息Course.java

package beans;

public class Course {
	private String courseid;
	private String coursename;
	public String getCourseid() {
		return courseid;
	}
	public void setCourseid(String courseid) {
		this.courseid = courseid;
	}
	public String getCoursename() {
		return coursename;
	}
	public void setCoursename(String coursename) {
		this.coursename = coursename;
	}

}

1.4学生分数Score.java

package beans;

public class Score {
	private String studentid;
	private String studentname;
	private String courseid;
	private String coursename;
	private String score;
	public Score(){}
	public Score(String studentid,String courseid,String coursename,String score){
		this.studentid=studentid;
		this.courseid=courseid;
		this.coursename=coursename;
		this.score=score;
	}
	public String getStudentid(){
		return studentid;
	}
	public void setStudentid(String studentid){
		this.studentid=studentid;
	}
	public String getScore(){
		return score;
	}
	public void setScore(String score){
		this.score=score;
	}
	public String getStudentname() {
		return studentname;
	}
	public void setStudentname(String studentname) {
		this.studentname = studentname;
	}
	public String getCourseid() {
		return courseid;
	}
	public void setCourseid(String courseid) {
		this.courseid = courseid;
	}
	public String getCoursename() {
		return coursename;
	}
	public void setCoursename(String coursename) {
		this.coursename = coursename;
	}
}

2.编写DAO,连接数据库,并定义相关查询分数、增加成绩、删除成绩方法

2.1CourseDAO.java。通过课程号查找课程名方法。

package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import beans.Course;
import beans.Student;

public class CourseDAO {
	private static Connection conn=null;
	
	public static void initConnection() throws Exception{//建立连接
		Class.forName("com.mysql.jdbc.Driver");
	    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","7277991");
	}
	
	public static Course findclasses(String courseid) throws Exception{//查找课程
	initConnection();
	Statement state =null;
	ResultSet rs = null;
	Course course=new Course();
	try{
	String sql = "select * from course where courseid = '"+courseid+"'";
	state = conn.createStatement();
	rs=state.executeQuery(sql);
	if(rs.next()){
		course.setCourseid(rs.getString("courseid"));
		course.setCoursename(rs.getString("coursename"));
	}
	}catch(Exception e){
		e.printStackTrace();
		}finally{
		closeConnection();
	}
	return course;
	}
	
	public static void closeConnection() throws Exception{//断开连接
		conn.close();
	}
}

2.2UserDAO.java。登录时通过学号或管理员账号查找学生或管理员进行登录验证。注册时学生信息增加。

 package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import beans.Admin;
import beans.Student;

public class UserDAO {
	private static Connection conn=null;
	
	public static void initConnection() throws Exception{//建立连接
		Class.forName("com.mysql.jdbc.Driver");
	    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","7277991");
	}
	
	public static Student findstudent(String studentid) throws Exception{//查找学生
	initConnection();
	Statement state =null;
	ResultSet rs = null;
	Student student=new Student();
	try{
	String sql = "select * from student where studentid = '"+studentid+"'";
	state = conn.createStatement();
	rs=state.executeQuery(sql);
	if(rs.next()){
		student.setStudentid(rs.getString("studentid"));
		student.setPassword(rs.getString("password"));
		student.setStudentname(rs.getString("studentname"));
	}
	}catch(Exception e){
		e.printStackTrace();
		}finally{
		closeConnection();
	}
	return student;
	}
	
	public static boolean addStudent(Student student) {//增加学生
	    boolean flag=false;
	    int i=0;
			try {
				initConnection();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	    Statement state =null;
	    String sql= "insert into student (studentid,studentname,password) values('"+student.getStudentid()+"','"+student.getStudentname()+"','"+student.getPassword()+"')"; 
	    try {
	    	state = conn.createStatement();
	    	i=state.executeUpdate(sql);
	      if(i>0){
	        flag=true;
	      }
	    }catch (SQLException e) {
	      e.printStackTrace();
	    }
	    try {
			closeConnection();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return flag;
	  }

	
	public static Admin findadmin(String adminid) throws Exception{//查找管理员
	initConnection();
	Statement state =null;
	ResultSet rs = null;
	Admin admin=new Admin();
	try{
	String sql = "select * from admin where adminid = '"+adminid+"'";
	state = conn.createStatement();
	rs=state.executeQuery(sql);
	if(rs.next()){
		admin.setAdminid(rs.getString("adminid"));
		admin.setPassword(rs.getString("password"));
		admin.setAdminname(rs.getString("adminname"));
	}
	}catch(Exception e){
		e.printStackTrace();
		}finally{
		closeConnection();
	}
	return admin;
	}
	
	public static void closeConnection() throws Exception{//断开连接
		conn.close();
	}
}

2.3ScoreDAO.java。通过学号或课程号查找成绩。增加、删除、修改成绩等方法。

package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import beans.Score;
import beans.Student;

public class ScoreDAO {
	private static Connection conn=null;
	
	public static void initConnection() throws Exception{//建立连接
		Class.forName("com.mysql.jdbc.Driver");
	    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","7277991");
	}
	
	public static ArrayList<Score> findscore1(String studentid) throws Exception{//按学号查找成绩
	initConnection();
	Statement state =null;
	ResultSet rs = null;
	ArrayList<Score> list=new ArrayList<Score>();
	try{
	String sql = "select score.*,student.studentname,course.coursename from score,student,course where score.studentid=student.studentid and score.courseid=course.courseid and score.studentid = '"+studentid+"'";
	state = conn.createStatement();
	rs=state.executeQuery(sql);
	while(rs.next()){
		Score score=new Score();
		score.setStudentid(rs.getString("studentid"));
		score.setStudentname(rs.getString("studentname"));
		score.setCourseid(rs.getString("courseid"));
		score.setCoursename(rs.getString("coursename"));
		score.setScore(rs.getString("score"));
		list.add(score);
	}
	}catch(Exception e){
		e.printStackTrace();
		}finally{
		closeConnection();
	}
	return list;
	}
	
	public static ArrayList<Score> findscore2(String courseid) throws Exception{//按课程号查找成绩
	initConnection();
	Statement state =null;
	ResultSet rs = null;
	ArrayList<Score> list=new ArrayList<Score>();
	try{
	String sql = "select score.*,student.studentname,course.coursename from score,student,course where score.studentid=student.studentid and score.courseid=course.courseid and score.courseid = '"+courseid+"'";
	state = conn.createStatement();
	rs=state.executeQuery(sql);
	while(rs.next()){
		Score score=new Score();
		score.setStudentid(rs.getString("studentid"));
		score.setStudentname(rs.getString("studentname"));
		score.setCourseid(rs.getString("courseid"));
		score.setCoursename(rs.getString("coursename"));
		score.setScore(rs.getString("score"));
		list.add(score);
	}
	}catch(Exception e){
		e.printStackTrace();
		}finally{
		closeConnection();
	}
	return list;
	}
	
	public static int addscore(String studentid,String courseid,String score) throws Exception{//增加成绩
	initConnection();
	Statement state =null;
	int rs = 0;
	try{
	String sql = "insert into score(studentid,courseid,score) values('"+studentid+"','"+courseid+"','"+score+"')";
	state = conn.createStatement();
	rs=state.executeUpdate(sql);
	}catch(Exception e){
		e.printStackTrace();
		}finally{
		closeConnection();
	}
	return rs;
	}
	
	public static int updatescore(String studentid,String courseid,String score) throws Exception{//修改成绩
	initConnection();
	Statement state =null;
	int rs = 0;
	try{
	String sql = "update score set score='"+score+"' where studentid = '"+studentid+"' and courseid='"+courseid+"'";
	state = conn.createStatement();
	rs=state.executeUpdate(sql);
	}catch(Exception e){
		e.printStackTrace();
		}finally{
		closeConnection();
	}
	return rs;
	}
	
	public static int deletescore(String studentid,String courseid) throws Exception{//删除成绩
	initConnection();
	Statement state =null;
	int rs = 0;
	try{
	String sql = "delete from score where studentid = '"+studentid+"' and courseid='"+courseid+"'";
	state = conn.createStatement();
	rs=state.executeUpdate(sql);
	}catch(Exception e){
		e.printStackTrace();
		}finally{
		closeConnection();
	}
	return rs;
	}
	
	public static void closeConnection() throws Exception{//断开连接
		conn.close();
	}
}

3.jsp及相关Servlet的编写

3.1登录界面的编写

index.jsp

  <body>
  <div id="top"><p>欢迎登录学生成绩管理系统</p></div>
  <div id="index">
    <form action="servlet\login" method="post">
    <p> 账号<input name="userid" type="text" onBlur="f0()"/><br></p>
   <p> 密码<input name="password" type="password" onBlur="f1()"/><br></p>
    <p><input name="user" type="radio" value="student" checked>学生
    <input name="user" type="radio" value="administrator">管理员<br></p>
    <p><input type="submit" value="登录"></p>
    </form>
    <p><a href="register.jsp">没有账号?点击注册吧</a></p>
    </div>
  </body>

用CSS简单美化后登录界面长这样


登录界面的Servlet,login.java

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import DAO.UserDAO;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import beans.Admin;
import beans.Student;

public class login extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String userid = request.getParameter("userid");//获取账号
		String password = request.getParameter("password");//获取密码
		String user=request.getParameter("user");//判断用户身份,学生还是管理员
		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = response.getWriter();
		if(user==null||password==null){//未输入密码
			System.out.println("登录失败!");
			out.print("<script>alert('登录失败!请输入账号和密码');"
					+ "window.location.href='../index.jsp'</script>");
		}else{
			if(user.equals("student")){//学生登录时
				try {
					Student student=UserDAO.findstudent(userid);
					if(student.getPassword().equals(password)){
						HttpSession session = request.getSession();
						session.setAttribute("student",student);
						System.out.println("登录成功!");
						response.sendRedirect("../stu_showscore.jsp");
					}else{
						System.out.println("登录失败!");
						out.print("<script>alert('登录失败!请确认账号和密码');"
								+ "window.location.href='../index.jsp'</script>");
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{//管理员登录时
				try {
					Admin admin=UserDAO.findadmin(userid);
					if(admin.getPassword().equals(password)){
						HttpSession session = request.getSession();
						session.setAttribute("admin",admin);
						System.out.println("登录成功!");
						response.sendRedirect("../admin_menu.jsp");
					}else{
						System.out.println("登录失败!");
						out.print("<script>alert('登录失败!请确认账号和密码');"
								+ "window.location.href='../index.jsp'</script>");
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					
			}
		}
		
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
		
	}

}
3.2注册界面

注册界面的jsp,利用JavaScript对输入的账号、密码等输入信息进行简单验证,利用Servlet对输入的账号是否已存在、两次密码是否一致进行验证。

 <body>
  <script type="text/javascript">
    var emt = document.getElementsByTagName("em");
    var t=1;
     function  f0(){
     if(register.userid.value.length<6||register.userid.value.length>12){
                emt[0].innerHTML="账号长度6-12位!";
                t=0;
            }
      else{
           emt[0].innerHTML="";
           t=1;
      }
    } 
    function  f1(){
     if(register.username.value.length<1||register.username.value.length>4){
                emt[1].innerHTML="用户名长度1-4位!";
                t=0;
            }
      else{
           emt[1].innerHTML="";
           t=1;
      }
    } 
    function  f2(){
        var mm=/^[0-9a-zA-Z]{6,12}$/;
     var flag=mm.test(register.password1.value);
     if(!flag){
                emt[2].innerHTML="密码要求长度6-12位;字母数字的组合,不能有其他字符!";
                t=0;
            }
      else{
           emt[2].innerHTML="";
           t=1;
      }
    } 
    function  f3(){
       if(register.password1.value != register.password2.value){
            emt[3].innerHTML="密码两次应一致!";
                t=0;
       }else{
           emt[3].innerHTML="";
           t=1;
      }
    }
</script>
   <div id="top"><p>欢迎登录学生成绩管理系统</p></div>
     <div id="index">
     <form name="register" action="servlet\register" method="post">
   <p> 账号<input name="userid" type="text" onBlur="f0()"/><br><em></em></p>
   <p> 姓名<input name="username" type="text" onBlur="f1()"/><br><em></em></p>
   <p> 密码<input name="password1" type="password" onBlur="f2()"/><br><em></em></p>
    <p>确认密码<input name="password2" type="password" onBlur="f3()"/><br><em></em></p>
    <p><input type="submit" value="确认"></p>
    </form>
    <p><a href="index.jsp">返回登录</a></p>
    </div>
  </body>

注册页面的提示效果


注册界面的Servlet,register.java

package servlets;

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 DAO.UserDAO;
import beans.Student;

public class register extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String userid = request.getParameter("userid");//获取账号
		String password1 = request.getParameter("password1");//获取第一次输入的密码
		String username = request.getParameter("username");//获取姓名
		String password2 = request.getParameter("password2");//获取第二次输入的密码
		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = response.getWriter();
		System.out.print(userid);
		System.out.print(username);
		System.out.print(password1);
		System.out.print(password2);
		if(userid!=null&&username!=null&&password1!=null&&password2!=null){//页面信息是否填写完整
			if(password1.equals(password2)){//两次输入的密码一致
				try {
					Student student = UserDAO.findstudent(userid);
					if(student.getStudentid()!=null){//账号存在
						out.print("<script>alert('该账号存在,注册失败!');"
								+ "window.location.href='../register.jsp'</script><");
					}else{
						student.setStudentid(userid);
						student.setStudentname(username);
						student.setPassword(password1);
						boolean flag=UserDAO.addStudent(student);
						if(flag){
							out.print("<script>alert('注册成功!现在去登录');"
									+ "window.location.href='../index.jsp'</script>");
						}else{
							out.print("<script>alert('注册失败!稍后重试');"
									+ "window.location.href='../register.jsp'</script>");
						}
					}
				} catch (Exception e) {
			// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				out.print("<script>alert('两次密码不一致,请确认');"
						+ "window.location.href='../register.jsp'</script>");
		}
		}else{
			out.print("<script>alert('请将信息填写完整');"
					+ "window.location.href='../register.jsp'</script>");
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}

3.3管理员功能页面

3.3.1管理员登录成功菜单,登录成功后该页面有三个超链接,分别指向具体的功能页面

3.3.2管理员查询成绩页面

成绩查询有两种形式:按学号或按课程号查询,利用表单中的radio进行选择。在servlet中查询,并把结果存到Session中,并在jsp页面判断Session中是否有值,若有,则以表格的形式显示,导入beans包,调用类中的get方法获得相应的值。

<body>
  <div id="top"><p>学生成绩管理系统-查询成绩</p></div>
  <div id="admin">
    <form action="servlet\showscore" method="post">
    <input name="func" type="hidden" value="show">选择成绩查询方式:
    <input name="flag" type="radio" value="course" checked>课程号查询
    <input name="flag" type="radio" value="stu">学号查询<br>
    <input name="id" type="text">
    <input type="submit" value="查询">
    </form>
    </div>
     <table align="center" cellpadding="15" cellspacing="15">
    <%
    ArrayList<Score> list=(ArrayList<Score>) session.getAttribute("score"); 
    String flag=(String) session.getAttribute("flag");
    if(list!=null&&list.size()!=0){
    if(flag.equals("stu")){%>
        <tr>
     	<td colspan="4" align="center">学号为<%=list.get(0).getStudentid() %>的学生成绩如下</td>
     	</tr>
    <% }else{%>
    <tr>
     	<td colspan="4" align="center">  课程号为<%=list.get(0).getCourseid()%>的全体学生成绩如下</td>
     	</tr>
    <%}%>
    <tr>
         <td>学生学号</td>
         <td>学生姓名</td>
         <td>课程名</td>
         <td>成绩</td>
     	</tr>
    <%
            for(int j=0;j<list.size();j++){%>
            <tr>
            <td><%=list.get(j).getStudentid() %></td>
            <td><%=list.get(j).getStudentname()%></td>
            <td><%=list.get(j).getCoursename()%></td>
            <td><%=list.get(j).getScore()%></td>
            </tr>
			         <% }}%>
			         <tr>
	<td colspan="4" align="center">
	<form action="servlet\exit" method="post">
	<input name="exit" type="hidden" value="score">
    <input type="submit" value="返回">
    </form>
	</td>
	</tr>
	</table>
  </body>

查询结果显示如下


查询成绩操作的servlet如下

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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 DAO.CourseDAO;
import DAO.ScoreDAO;
import DAO.UserDAO;
import beans.Course;
import beans.Score;
import beans.Student;

public class showscore extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=gb2312");
		String flag=request.getParameter("flag");//按学号还是课程号查找
		String id=request.getParameter("id");//获取的id值
		String func=request.getParameter("func");//从哪个页面传来的表单
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		if(flag.equals("stu")){//按学号查找成绩
			session.setAttribute("flag", flag);
			try {
				Student student=UserDAO.findstudent(id);
				if(student.getStudentid()!=null){//输入的学号存在
					try {
						ArrayList<Score> list=(ArrayList<Score>) ScoreDAO.findscore1(id);
						session.setAttribute("score", list); 
						if(func.equals("show"))//从admin_showscore.jsp传来的值
							response.sendRedirect("../admin_showscore.jsp");
						else//从admin_updatescore.jsp传来的值
							response.sendRedirect("../admin_updatescore.jsp");
					}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
					}
				}else{//学号不存在
					if(func.equals("show")){
					out.print("<script>alert('不存在该学生');"
							+ "window.location.href='../admin_showscore.jsp'</script>");
					}else{
						out.print("<script>alert('不存在该学生');"
								+ "window.location.href='../admin_updatescore.jsp'</script>");
					}
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
					
		}else{//按课程号查找成绩
			try {
				Course course=CourseDAO.findclasses(id);
				if(course.getCourseid()!=null){
					ArrayList<Score> list;
					try {
						session.setAttribute("flag", flag);
						list = (ArrayList<Score>) ScoreDAO.findscore2(id);
						session.setAttribute("score", list); 
						if(func.equals("show"))
							 response.sendRedirect("../admin_showscore.jsp");
						 else
							 response.sendRedirect("../admin_updatescore.jsp");
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					if(func.equals("show")){
						out.print("<script>alert('不存在该课程');"
								+ "window.location.href='../admin_showscore.jsp'</script>");
						}else{
							out.print("<script>alert('不存在该课程');"
									+ "window.location.href='../admin_updatescore.jsp'</script>");
						}
					
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			 
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

				doGet(request,response);
	}

}

3.3.3管理员录入成绩页面

管理员需输入学生号、课程号、成绩,利用form表单,把其提交到servlet中,在servlet中要判断学生号和课程号是否在数据库中存在,不存在则跳出相应的警告框。

  <body>
  <div id="top"><p>学生成绩管理系统-录入成绩</p></div>
  <div id="admin">输出下列信息进行分数录入</div>
  <table align="center" cellpadding="15" cellspacing="15">
  <tr>
         <td>学生学号</td>
         <td>课程号</td>
         <td>成绩</td>
     	</tr>
   <tr>
   <form action="servlet\addscore" method="post">
   <td><input type="text" name="studentid"></td>
   <td><input type="text" name="courseid"></td>
   <td><input type="text" name="score"></td>
   </tr>
   <tr>
   <td colspan="3" align="center"><input type="submit" value="添加"></td>
   </form>
	</tr>
	<tr>
	<td colspan="3" align="center">
	<form action="servlet\exit" method="post">
	<input name="exit" type="hidden" value="score">
    <input type="submit" value="返回">
    </form></td>
	</tr>
    </table>
  </body>

页面如下


录入成绩的servlet

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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 DAO.CourseDAO;
import DAO.ScoreDAO;
import DAO.UserDAO;
import beans.Course;
import beans.Score;
import beans.Student;

public class addscore extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=gb2312");
		String studentid=request.getParameter("studentid");//获取学号
		String courseid=request.getParameter("courseid");//获取课程号
		String score=request.getParameter("score");//获取成绩
		PrintWriter out = response.getWriter();
		try {
			Student student=UserDAO.findstudent(studentid);//查找学生
			Course course=CourseDAO.findclasses(courseid);//查找课程
			System.out.print(student.getStudentid());
			if(student.getStudentid()!=null){//存在该学生
				if(course.getCourseid()!=null){//存在该课程
				 try {
			        	int i=ScoreDAO.addscore(studentid,courseid,score);
						if(i!=0){
							out.print("<script>alert('添加成功');"
									+ "window.location.href='../admin_addscore.jsp'</script>");
						}else{
							out.print("<script>alert('添加失败');"
									+ "window.location.href='../admin_addscore.jsp'</script>");
						}
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}else{
					out.print("<script>alert('课程不存在,添加失败');"
							+ "window.location.href='../admin_addscore.jsp'</script>");
				}
			}else{
				out.print("<script>alert('学生不存在,添加失败');"
						+ "window.location.href='../admin_addscore.jsp'</script>");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}

3.3.4管理员修改、删除成绩

管理员首先通过学号或课程号查询成绩,查询成绩的servlet是3.3.2中的servlet,在提交表单时,定义了一个名为func的隐藏表单,判断是哪个页面传来的查询操作,并在查询结束后,返回到该页面,在本页面显示成绩,成绩为一个输入框,且在每行成绩后有修改和删除两个按钮。两个按钮分别对应两个表单,提交表单时把隐藏表单中的相应学生信息传入servlet中处理。

页面jsp代码

<body>
    <div id="top"><p>学生成绩管理系统-修改成绩</p></div>
  <div id="admin">
    <form action="servlet\showscore" method="post">
    <input name="func" type="hidden" value="update">选择方式进行查询修改
    <input name="flag" type="radio" value="course" checked>课程号查询
    <input name="flag" type="radio" value="stu">学号查询<br>
    <input name="id" type="text">
    <input type="submit" value="查询">
    </form>
    </div>
    <table align="center" cellpadding="15" cellspacing="15">
    <%
    ArrayList<Score> list=(ArrayList<Score>)session.getAttribute("score"); 
    String flag=(String) session.getAttribute("flag");
    if(list!=null&&list.size()!=0){
    
    if(flag.equals("stu")){%>
        <tr>
     	<td colspan="6" align="center">学号为<%=list.get(0).getStudentid() %>的学生成绩如下</td>
     	</tr>
    <% }else{%>
    <tr>
     	<td colspan="6" align="center">  课程号为<%=list.get(0).getCourseid()%>的全体学生成绩如下</td>
     	</tr>
  <br>
    <%}%><tr>
         <td>学生学号</td>
         <td>学生姓名</td>
         <td>课程名</td>
         <td>成绩</td>
         <td>修改</td>
         <td>是否删除</td>
     	</tr>
    <%
            for(int j=0;j<list.size();j++){%>
            
            <tr>
            <td><%=list.get(j).getStudentid() %></td>
            <td><%=list.get(j).getStudentname()%></td>
            <td><%=list.get(j).getCoursename()%></td>
            <form action="servlet\updatescore" method="post">
            <input type="hidden" name="studentid" value="<%=list.get(j).getStudentid() %>">
            <input type="hidden" name="courseid" value="<%=list.get(j).getCourseid() %>">
            <td><input name="score" type="text" value="<%=list.get(j).getScore()%>"></td>
            <td><input type="submit" value="修改"></td>
            </form>
            <form action="servlet\deletescore" method="post">
            <input type="hidden" name="studentid" value="<%=list.get(j).getStudentid() %>">
            <input type="hidden" name="courseid" value="<%=list.get(j).getCourseid() %>">
            <td><input type="submit" value="删除"></td>
            </form>
            </tr>
			<%}
        } %>
        <tr>
	<td colspan="6" align="center">
	<form action="servlet\exit" method="post">
	<input name="exit" type="hidden" value="score">
    <input type="submit" value="返回">
    </form>
	</td>
	</tr>
        </table>
</body>

页面如下


修改成绩的servlet代码

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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 DAO.ScoreDAO;
import beans.Score;

public class updatescore extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=gb2312");
		String studentid=request.getParameter("studentid");//获取学号
		String courseid=request.getParameter("courseid");//获取课程号
		String score=request.getParameter("score");//获取成绩
		int i = 0;
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
        ArrayList<Score> list = (ArrayList<Score>) session.getAttribute("score");
        try {
			i=ScoreDAO.updatescore(studentid,courseid,score);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
         for(int j=0;j<list.size();j++){
             if(list.get(j)==null){
                 list.remove(j);
             }
             if(list.get(j)!=null && list.get(j).getStudentid().equals(studentid)&&list.get(j).getCourseid().equals(courseid)){
            	     list.get(j).setScore(score);
            		 break;
            	 }
             }
         if(i!=0){
        	 out.print("<script>alert('修改成功');"
 					+ "window.location.href='../admin_updatescore.jsp'</script>");
			}else{
				out.print("<script>alert('修改失败');"
						+ "window.location.href='../admin_updatescore.jsp'</script>");
			}
        
        //response.sendRedirect("../admin_updatescore.jsp");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}

删除成绩的servlet

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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 DAO.ScoreDAO;
import beans.Score;

public class deletescore extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=gb2312");
		String studentid=request.getParameter("studentid");//获取学号
		String courseid=request.getParameter("courseid");//获取课程号
		int i=0;
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
        ArrayList<Score> list = (ArrayList<Score>) session.getAttribute("score");
        try {
			i=ScoreDAO.deletescore(studentid,courseid);//进行删除操作
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
         for(int j=0;j<list.size();j++){
             if(list.get(j)==null){
                 list.remove(j);
             }
             if(list.get(j)!=null && list.get(j).getStudentid().equals(studentid)&&list.get(j).getCourseid().equals(courseid)){
            		 list.remove(j);
            		 break;
            	 }
             }
         if(i!=0){
        	 out.print("<script>alert('删除成功');"
 					+ "window.location.href='../admin_updatescore.jsp'</script>");
         }else{
        	 out.print("<script>alert('删除失败');"
 					+ "window.location.href='../admin_updatescore.jsp'</script>");
         }
         
       // response.sendRedirect("../admin_updatescore.jsp");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}

3.4学生页面

学生在登录时,登录信息存储在session中,session中的学号以隐藏表单形式存在,学生点击查询成绩时,跳转到相应的servlet页面。与管理员查询成绩操作相同,就不赘述了。

3.5返回、退出界面

返回或登录时,利用表单传入servlet中进行操作,在session中把相应的session值进行清除再返回上个页面

package servlets;

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 javax.servlet.http.HttpSession;
public class exit extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=gb2312");
		String exit=request.getParameter("exit");
		HttpSession session = request.getSession();
		if(exit.equals("user")){
			session.invalidate();
			response.sendRedirect("../index.jsp");
		}else{
			session.removeAttribute("score");
			response.sendRedirect("../admin_menu.jsp");
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}






©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值