学生选课系统--学生功能的实现
学生选课系统--学生功能的实现
小总结:
包括查看个人信息、修改个人基本信息、修改个人密码、查询课程信息、自助选课、查询个人课表六项功能模块
我感觉教师的功能和这个差不多,我想着能否将这两个统一一下,然后添加一些标志来表示是学生、教师身份,这样就不用写太多页面了,我在管理员的密码重置的功能中就使用了这个办法,在第一个jsp页面选择重置学生还是教师的密码,然后Servlet中接受type,对type指向的表进行查询,然后再设立type传到另一个jsp,这样就可以根据转过来的type表示查询学生还是教师,就可以使用对应的mapper(StuMapper、TeaMapper),也可以对学生或者教师的详细信息进行查询(虽然那个地方其实直接传用户名就可以实现,但是这个方式对要查找的特定的表来说应该用起来也不错,可以少一个表)
但是这个要求不管怎样,一个jsp页面显示的的格式大体上都是一样的,比如说教师显示姓名id职称院系,学生显示姓名id性别班级,所以对这样子处理有一定的要求吧。<%%><tr><td>嵌套有规则,,应该不行..
在登录那里,有问题,就是登录的人不是学生,也可以选择是学生,只要账号密码正确就可以进入学生页面,但是,学生表那里根本没有这个人,所以会出现一些报错,就是由于学生表没有这个人导致的。所以应该再在登录功能中加点判断,可以在login表中再添加一条属性,就是身份,先找到身份,再找到uname用户名,再找到密码看是否相同...而我之前的是直接找uname然后比较密码是否相同。如果学生表里面没这个人,但是这个人账号密码正确且选择的学生身份登录,就会报错,下面这个页面显示错误信息
还有一种办法就是直接在学生表、教师表都添加账号密码信息,感觉这个更可行,也不用建多一个login表(下次一定.)
在自助选课的时候,问题出在按钮,本来选课应该是通过按钮实现的。
注意代码起名的规则,不然写到后面功能多了就乱了..
学生登录后的功能选择页面
1.stu.jsp(学生功能选择页面)
<%@ page import="com.xxxx.entity.Stu" %> <%@ page import="com.xxxx.mapper.StuMapper" %> <%@ page import="com.xxxx.util.GetSqlSession" %> <%@ page import="org.apache.ibatis.session.SqlSession" %><%-- Created by IntelliJ IDEA. User: 22466 Date: 2022/11/13 Time: 0:53 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>stu.jsp</title> </head> <%--学生功能页--%> <body> <% String uname= (String) request.getSession().getAttribute("uname");//获取一下uname,方便日后chaxun request.getSession().setAttribute("uname",uname); // request.getSession().setAttribute("type","1");//1为学生,2为教师 // 查找学生的id,因为有了id就可以直接使用管理员修改学生信息页面,不用再写,只需要把id传过去 SqlSession sqlSession = GetSqlSession.CreateSqlSession(); StuMapper stuMapper = sqlSession.getMapper(StuMapper.class); Stu stu=stuMapper.selectByName(uname); request.getSession().setAttribute("stuid",stu.getId()); %> <h3>欢迎${uname}登录</h3> <a href="stuselect.jsp">查询个人信息</a><br> <a href="upstu.jsp?id=<%=stu.getId()%>">修改个人信息</a><br> <a href="stupwd.jsp">修改个人密码</a><br> <a href="stuallcla.jsp">查询课程信息</a><br> <a href="stuxuan.jsp?id=<%=stu.getId()%>">自助选课</a><br> <a href="stucla.jsp">查询个人课表</a> </body> </html>
查询个人信息功能:
2.stuselect.jsp(查询学生个人信息页面)
<%@ page import="org.apache.ibatis.session.SqlSession" %> <%@ page import="com.xxxx.util.GetSqlSession" %> <%@ page import="com.xxxx.mapper.StuMapper" %> <%@ page import="com.xxxx.entity.Stu" %><%-- Created by IntelliJ IDEA. User: 22466 Date: 2022/11/16 Time: 15:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>stuselect.jsp</title> </head> <%--查询个人信息--%> <body> <table align="center" border="1" width="800"> <tr> <td>学生学号</td><td>学生姓名</td><td>性别</td><td>年龄</td><td>专业</td><td>班级</td><td>已选课程</td> </tr> <% String uname= (String) request.getSession().getAttribute("uname"); // String type= (String) request.getSession().getAttribute("type"); SqlSession sqlSession = GetSqlSession.CreateSqlSession(); StuMapper stuMapper = sqlSession.getMapper(StuMapper.class); Stu stu=stuMapper.selectByName(uname); %> <tr> <td><%=stu.getId()%></td><td><%=stu.getName()%></td><td><%=stu.getSex()%></td><td><%=stu.getAge()%></td><td><%=stu.getPro()%></td><td><%=stu.getCla()%></td> <td><%=stu.getClaid()%></td> </tr> </table> </body> </html>
3.upstu.jsp(学生修改个人信息页面)这个是管理员功能里就有的,直接可以用
<%-- Created by IntelliJ IDEA. User: 22466 Date: 2022/11/13 Time: 1:16 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>upstu.jsp</title> </head> <%--不符合*录入格式*,不可修改,,没完成--%> <body> <% String id=request.getParameter("id");//获取超链接传过来的id request.getSession().setAttribute("id",id);//设置域对象 %> <h2 align="center">修改学生信息</h2> <table align="center" width="800" border="1"> <form method="post" action="upstuServlet"> <tr> <td>学号</td> <td><%=id%></td> </tr> <tr> <td>姓名</td> <td><input type="text" name="name" id="name"></td> </tr> <tr> <td>性别</td> <td> <input type="radio" name="sex" value="男" checked>男 <input type="radio" name="sex" value="女">女 </td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age" id="age"></td> </tr> <tr> <td>专业</td> <td> <input type="radio" name="pro" value="计算机科学与技术" checked>计算机科学与技术<br> <input type="radio" name="pro" value="软件工程">软件工程<br> <input type="radio" name="pro" value="网络工程">网络工程<br> <input type="radio" name="pro" value="数字媒体技术">数字媒体技术<br> </td> </tr> <tr> <td>班级</td> <td><input type="text" name="cla" id="cla"></td> </tr> <tr align="center"> <td colspan="2"><button>修改</button></td> </tr> </form> </table> </body> </html>
4.upstuServlet(修改学生信息Servlet)这个是在管理员那就有的,直接可以用
package com.xxxx.servlet; import com.xxxx.entity.Stu; import com.xxxx.mapper.StuMapper; import com.xxxx.util.GetSqlSession; import org.apache.ibatis.session.SqlSession; 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 java.io.IOException; @WebServlet("/upstuServlet") public class upstuServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); int id= Integer.valueOf((String) request.getSession().getAttribute("id"));//获取要修改的id写int会出错** String name=request.getParameter("name"); //姓名 String sex=request.getParameter("sex"); //性别 int age= Integer.parseInt(request.getParameter("age")); //年龄 String pro=request.getParameter("pro"); //专业 String cla=request.getParameter("cla"); //班级 Stu stu=new Stu(id,name,sex,age,pro,cla); SqlSession sqlSession = GetSqlSession.CreateSqlSession(); StuMapper stuMapper = sqlSession.getMapper(StuMapper.class); stuMapper.update(stu); response.getWriter().write("修改成功"); response.getWriter().close(); sqlSession.close(); } }
5.stupwd.jsp(学生修改密码页面)
<%-- Created by IntelliJ IDEA. User: 22466 Date: 2022/11/16 Time: 15:27 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>stupwd.jsp</title> </head> <%--修改个人密码--%> <body> <% String uname= (String) request.getSession().getAttribute("uname"); request.getSession().setAttribute("uname",uname); %> <form action="stupwdServlet" method="post"> 账户:<%=uname%><br>密码:<input type="password" name="upwd"><br> <span id="msg" style="color: red;font-size: 16px">${msg}</span> <button>修改</button> </form> </body> </html>
6.stupwdServlet(学生修改密码Servlet)
package com.xxxx.servlet; import com.xxxx.entity.Login; import com.xxxx.mapper.LoginMapper; import com.xxxx.util.GetSqlSession; import org.apache.ibatis.session.SqlSession; 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 java.io.IOException; @WebServlet("/stupwdServlet") public class stupwdServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String uname= (String) request.getSession().getAttribute("uname"); String upwd=request.getParameter("upwd"); SqlSession sqlSession= GetSqlSession.CreateSqlSession(); LoginMapper loginMapper=sqlSession.getMapper(LoginMapper.class); Login login=loginMapper.selectByUname(uname); if(login.getUpwd().equals(upwd)){ request.getSession().setAttribute("msg","密码不能和原来的密码一样"); request.getRequestDispatcher("stupwd.jsp").forward(request,response); } else{ loginMapper.updateByUname(uname,upwd); response.getWriter().write("修改密码成功"); response.getWriter().close(); } sqlSession.close(); } }
7.stuallcla.jsp(学生查询学校所有课程信息)
<%@ page import="com.xxxx.entity.Cla" %> <%@ page import="com.xxxx.mapper.ClaMapper" %> <%@ page import="com.xxxx.util.GetSqlSession" %> <%@ page import="org.apache.ibatis.session.SqlSession" %><%-- Created by IntelliJ IDEA. User: 22466 Date: 2022/11/16 Time: 15:27 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>stuallcla.jsp</title> </head> <%--查询 学校所有 课程信息,可以直接复制selectcla,唯一要修改的是学生没有修改课程的功能,把相应的给删了就好了--%> <body> <table align="center" border="1" width="800"> <tr> <td>课程编号</td><td>课程名称</td><td>开课时间</td><td>上课时间</td><td>开课院系</td> <td>任课老师</td><td>老师工号</td><td>限制人数</td><td>已选人数</td> </tr> <% SqlSession sqlSession = GetSqlSession.CreateSqlSession(); ClaMapper claMapper = sqlSession.getMapper(ClaMapper.class); Cla[] clas=claMapper.selectAll(); for (Cla cla:clas) { %> <tr> <td><%=cla.getId()%></td><td><%=cla.getName()%></td><td><%=cla.getTime()%></td><td><%=cla.getCi()%></td> <td><%=cla.getYuan()%></td><td><%=cla.getTea()%></td><td><%=cla.getTeaid()%></td><td><%=cla.getPeople()%></td> <td><%=cla.getXuan()%> </td> </tr> <% } sqlSession.close(); %> </table> </body> </html>
8.stuxuan.jsp(学生选课页面)
<%@ page import="org.apache.ibatis.session.SqlSession" %> <%@ page import="com.xxxx.util.GetSqlSession" %> <%@ page import="com.xxxx.mapper.ClaMapper" %> <%@ page import="com.xxxx.entity.Cla" %><%-- Created by IntelliJ IDEA. User: 22466 Date: 2022/11/16 Time: 15:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>stuxuan.jsp</title> </head> <%--学生自助选课,直接复制stuallcla,即浏览课程信息,唯一要变的是添加选课按钮, 然后到Servlet中判断选课人数是否超标,是则返回提示信息,否则就对学生表和课表进行修改--%> <body> <table align="center" border="1" width="800"> <tr> <td>课程编号</td><td>课程名称</td><td>开课时间</td><td>上课时间</td><td>开课院系</td> <td>任课老师</td><td>老师工号</td><td>限制人数</td><td>已选人数</td><td></td> </tr> <% int stuid=(int) request.getSession().getAttribute("stuid"); request.getSession().setAttribute("stuid",stuid); SqlSession sqlSession = GetSqlSession.CreateSqlSession(); ClaMapper claMapper = sqlSession.getMapper(ClaMapper.class); Cla[] clas=claMapper.selectAll(); for (Cla cla:clas) { %> <tr> <td><%=cla.getId()%></td><td><%=cla.getName()%></td><td><%=cla.getTime()%></td><td><%=cla.getCi()%></td> <td><%=cla.getYuan()%></td><td><%=cla.getTea()%></td><td><%=cla.getTeaid()%></td><td><%=cla.getPeople()%></td> <td><%=cla.getXuan()%></td><td><a href="stuxuanServlet?claid=<%=cla.getId()%>&stuid=<%=stuid%>">选课</a></td> </tr> <% } sqlSession.close(); %> <tr align="center"><td colspan="10"> <span style="font-size: 16px;color: red" id="msg">${msg}</span></td></tr> </body> </html>
9.stuxuanServlet(学生选课Servlet)
package com.xxxx.servlet; import com.xxxx.entity.Cla; import com.xxxx.entity.Stu; import com.xxxx.mapper.ClaMapper; import com.xxxx.mapper.StuMapper; import com.xxxx.util.GetSqlSession; import org.apache.ibatis.session.SqlSession; 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 java.io.IOException; @WebServlet("/stuxuanServlet") public class stuxuanServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); int claid= Integer.parseInt(request.getParameter("claid")); int stuid= Integer.parseInt(request.getParameter("stuid")); SqlSession sqlSession= GetSqlSession.CreateSqlSession(); ClaMapper claMapper=sqlSession.getMapper(ClaMapper.class); Cla cla=claMapper.selectByid(claid);//找到原本的已选人数 if(cla.getXuan()==cla.getPeople()){ request.setAttribute("msg","选课人数已满"); request.getRequestDispatcher("stuxuan.jsp").forward(request,response); } else { StuMapper stuMapper = sqlSession.getMapper(StuMapper.class); stuMapper.updateCla(stuid, claid);//更新学生表中的选课信息 claMapper.updateXuan(cla.getId(), cla.getXuan()+1);//更改已选人数 response.getWriter().write("选课成功"); response.getWriter().close(); sqlSession.close(); } } }
10.stucla.jsp(学生个人课程显示)
<%@ page import="com.xxxx.entity.Stu" %> <%@ page import="com.xxxx.mapper.StuMapper" %> <%@ page import="org.apache.ibatis.session.SqlSession" %> <%@ page import="com.xxxx.util.GetSqlSession" %> <%@ page import="com.xxxx.mapper.ClaMapper" %> <%@ page import="com.xxxx.entity.Cla" %><%-- Created by IntelliJ IDEA. User: 22466 Date: 2022/11/16 Time: 15:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>stucla.jsp</title> </head> <%--查询个人课表--%> <body> <% String uname= (String)request.getSession().getAttribute("uname"); SqlSession sqlSession = GetSqlSession.CreateSqlSession(); StuMapper stuMapper = sqlSession.getMapper(StuMapper.class); Stu stu=stuMapper.selectByName(uname); ClaMapper claMapper=sqlSession.getMapper(ClaMapper.class); Cla cla=claMapper.selectByid(stu.getClaid()); %> <table align="center" border="1" width="800"> <tr> <td>课程编号</td><td>课程名称</td><td>开课时间</td><td>上课时间</td><td>开课院系</td> <td>任课老师</td><td>老师工号</td><td>限制人数</td> </tr> <tr> <td><%=cla.getId()%></td><td><%=cla.getName()%></td><td><%=cla.getTime()%></td><td><%=cla.getCi()%></td> <td><%=cla.getYuan()%></td><td><%=cla.getTea()%></td><td><%=cla.getTeaid()%></td><td><%=cla.getPeople()%></td> </tr> </table> </body> </html>
11.ClaMapper.java
package com.xxxx.mapper; import com.xxxx.entity.Cla; import org.apache.ibatis.annotations.Param; public interface ClaMapper { void add(Cla cla); Cla[] selectAll(); void update(Cla cla); Cla selectByTeaname(String teaname); void deleteById(int id); Cla selectByid(int claid); void updateXuan(@Param("id") int id,@Param("num") int num); }
12.ClaMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!--<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"--> <!-- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"--> <!-- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"--> <!-- version="4.0">--> <!--</web-app>--> <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace:命名空间--> <mapper namespace = "com.xxxx.mapper.ClaMapper"> <insert id="add"> insert into cla (name,time,ci,yuan,tea,teaid,people) values (#{name},#{time},#{ci},#{yuan},#{tea},#{teaid},#{people}); </insert> <update id="update"> update cla set time=#{time},ci=#{ci},yuan=#{yuan},tea=#{tea},teaid=#{teaid},people=#{people} where id=#{id}; </update> <update id="updateXuan"> update cla set xuan=#{num} where id=#{id}; </update> <delete id="deleteById"> delete from cla where id=#{id}; </delete> <select id="selectAll" resultType="com.xxxx.entity.Cla"> select * from cla; </select> <select id="selectByTeaname" resultType="com.xxxx.entity.Cla"> select * from cla where tea=#{teaname}; </select> <select id="selectByid" resultType="com.xxxx.entity.Cla"> select * from cla where id=#{claid}; </select> </mapper>
13.StuMapper.java
package com.xxxx.mapper;
import com.xxxx.entity.Stu;
import org.apache.ibatis.annotations.Param;
import javax.servlet.annotation.WebServlet;
public interface StuMapper {
void add(Stu stu);
void update(Stu stu);
Stu[] selectAll();
Stu selectByid(int id);
void deleteById(String id);
Stu selectByName(String uname);
void updateCla(@Param("stuid") int stuid,@Param("claid") int claid);
}
14.StuMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!--<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"--> <!-- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"--> <!-- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"--> <!-- version="4.0">--> <!--</web-app>--> <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace:命名空间--> <mapper namespace = "com.xxxx.mapper.StuMapper"> <insert id="add"> insert into stu01(name,sex,age,pro,cla) values (#{name},#{sex},#{age},#{pro},#{cla}); </insert> <update id="update"> update stu01 set name=#{name},sex=#{sex},age=#{age},pro=#{pro},cla=#{cla} where id=#{id}; </update> <update id="updateCla"> update stu01 set claid=#{claid} where id=#{stuid}; </update> <delete id="deleteById"> delete from stu01 where id=#{id}; </delete> <select id="selectAll" resultType="com.xxxx.entity.Stu"> select * from stu01; </select> <select id="selectByid" resultType="com.xxxx.entity.Stu"> select * from stu01 where id=#{id}; </select> <select id="selectByName" resultType="com.xxxx.entity.Stu"> select * from stu01 where name=#{uname}; </select> </mapper>