1 序言
学习java web的时候很渴望有一份完整的项目给我阅读,而网上的大部分项目拿过来都无法直接用,好不容易找到了一个学生管理系统也是漏洞百出。在此,我将边修改边学习这份代码,并且加上完全的注释,以便日后查阅。
2 项目说明
该项目为常见的学生管理系统,系统用例就不再说明。
《待补充》
3 代码分析
1 登录功能
1 首页 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% response.sendRedirect("user/index.jsp"); %> <%--script跳转有的浏览器不识别,比如火狐,应该使用response.sendRedict()方式更为正确, 之后好多这种情况这种情况,不再一一说明 --%> </body> </html>
2 user/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>欢迎登陆</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="img/css.css"> </head> <%--调用js进行前台检查,经常用到的这一段代码,复用率相当高 --%> <script language="JavaScript"> function login11() { if (document.form1.name.value == "" ) { alert("请输入用户名!"); document.form1.name.focus(); return false; } if (document.form1.pwd.value == "" ) { alert("请输入密码!"); document.form1.pwd.focus(); return false; } } </script> <STYLE> .input7 { BORDER-BOTTOM-WIDTH: 0px; WIDTH: 120px; COLOR: #000000; HEIGHT: 20px; BORDER-RIGHT-WIDTH: 0px } </STYLE> <%--本页面为真正登陆页面,因此会有两种状态,1.登陆成功跳转到下一个页面,2是登陆失败重新登录,因此要接受登陆状态参数"error" --%> <% String error = (String)request.getAttribute("error"); if(error != null && error.equals("1")){ %><script>alert('用户名或密码错误!')</script><% } %> <body bgcolor="#94E6FF"><%--将表单提交到servlet/user/UserLoginServlet中 --%> <form action="servlet/user/UserLoginServlet" name="form1" method="post" onSubmit="return login11()"> <div align="center"> <p>inxi </p> <p> </p> <p> </p> <table border="0" width="612" background="img/backPic1.gif" height="264" style="border: 3px double #9B9B9B"> <tr> <td colspan="2" align="center"><font face="华文隶书" size="5">研究生工程实践管理系统<br></font><font face="华文隶书" size="3"></font></td> </tr> <tr> <td width="292" align="right">用 户 名:</td> <td width="310"><input type="text" name="name" maxlength="16" class="input7"/></td> </tr> <tr> <td width="292" align="right">登录密码:</td> <td width="310"><input type="password" name="pwd" maxlength="16" class="input7"/></td> </tr> <tr> <td width="292" align="right">请选择登录身份:</td> <td width="310"> <select name= "station"> <option value="学生">研究生</option> <option value="指导老师">企业导师</option> </select> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="登陆"> <input type="Reset" value="重置"></td> </tr> </table> </div> </form> </body> </html>
3 UserLonginBean
这个工程中的Bean总共四个,用到的时候陆续贴出来
package bean; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.*; import util.*; public class UserLoginBean { Connection conn = null; Statement st = null; ResultSet rs = null; ArrayList al = new ArrayList();//容器的方式来接受出巡出来的结果,每个对象是查询结果的每一列,这里都是String对象 public int login(String name,String pwd,String station){//返回值int,代表查询状态,这里也可以自定义异常处理,复用率更高 int temp = 0; conn = DBConn.getConn();//util类bean 这里就不贴了,只有get和close的功能 try { st = conn.createStatement(); if(station.equals("学生")){//若身份为学生,则查询学生表,若是老师查询老师表 rs = st.executeQuery("select * from student where name='"+name+"'"); } if(station.equals("指导老师")){//老师分为指导老师和其他老师,这里只查询指导老师,功能待扩充 rs = st.executeQuery("select * from teacher where name='"+name+"' and station='"+station+"'"); } if(rs.next()){//若查询到有结果,则证明有该用户 String id = rs.getString("id"); String tname = rs.getString("name"); String tpwd = rs.getString("pwd"); String truename = rs.getString("truename"); String stations = rs.getString("station"); if(pwd.equals(tpwd)){//取出密码字段匹配,匹配成功取出信息,放入容器 temp = 1;//1状态代表查询成功 al.add(id); al.add(tname); al.add(tpwd); al.add(stations); al.add(truename); }else{ temp = 2;//2 代表密码不匹配 } }else{ temp = 3;//3代表没有该用户名 } } catch (SQLException e) { e.printStackTrace(); } finally{ DBConn.close(conn,st,rs);//最后关闭打开的对象,利用bean关闭 } return temp;//返回登录状态 } public ArrayList getArrayLst(){ return al;//返回登录查询到的结果 } }
4 servlet/user/UserLoginServlet
至此,登陆功能已经完成,接下来是选课功能package servlet.user; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.RequestDispatcher; 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 util.Validate; import bean.UserLoginBean; public class UserLoginServlet extends HttpServlet { /** * Constructor of the object. */ public UserLoginServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } //由于表单是post过来的,这里要将post转到get public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Validate vd = new Validate();//字符转换类,不再贴了 String name = vd.getUnicode(request.getParameter("name")); String pwd = vd.getUnicode(request.getParameter("pwd")); String station = vd.getUnicode(request.getParameter("station")); UserLoginBean lb = new UserLoginBean();//创建UserLoginBean对象,所有的用户操作都用得上,所以封装为Bean int flag = lb.login(name,pwd,station);//将表单传递过来的参数登录 ArrayList al = lb.getArrayLst();//获取登录结果,若没有登录成功则为null HttpSession session = request.getSession(); session.setAttribute("login", al);//创建session,将login设为登录成功的返回容器 String str = ""; if(flag == 2 || flag == 3){//2 3都代表登录失败 str = "/user/index.jsp"; request.setAttribute("error", "1");//设置error为登录失败状态 }else{ if(al.get(3).equals("学生"))//若是学生返回,转发至/user/student_list_3.jsp,老师则/user/student_list_2.jsp str = "/user/student_list_3.jsp"; else str = "/user/student_list_2.jsp"; } RequestDispatcher rd=request.getRequestDispatcher(str); rd.forward(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } }
2 选课功能
1 SelectBean
package bean; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import util.DBConn; public class SelectBean { Connection conn = null; Statement st = null; ResultSet rs = null; public ArrayList select(String sql,String[] args){ ArrayList al = new ArrayList(); conn = DBConn.getConn(); try { st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ ArrayList alRow = new ArrayList(); for(int i = 0;i < args.length;i++){ alRow.add(rs.getString(args[i])); } al.add(alRow);//按语句查询后,依次将查询到的结果装入容器 } } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } finally{ DBConn.close(conn,st,rs); } return al;//返回查询结果容器 } }
2 ListServlet
将需要列出来的结果都查询出来先
package servlet; import java.io.IOException; 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 util.Validate; import bean.SelectBean; public class ListServlet extends HttpServlet { /** * Constructor of the object. */ public ListServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { SelectBean sb = new SelectBean(); String sql1 = "select * from teacher where station='指导老师'"; String[] args1 = {"id","name","pwd","truename","sex","age","phone","addr","station"}; ArrayList al1 = sb.select(sql1, args1); request.setAttribute("tutor", al1);//将指导老师查询结果装入al1 String sql2 = "select * from teacher where station='指导班主任'"; String[] args2 = {"id","name","pwd","truename","sex","age","phone","addr","station"}; ArrayList al2 = sb.select(sql2, args2); request.setAttribute("mostly", al2);//将班主任查询结果装入al2 String sql3 = "select * from projectbase"; String[] args3 = {"baseid","basename","contacts","baseaddress","basephone","baseemail","baseintroduction"}; ArrayList al3 = sb.select(sql3, args3); request.setAttribute("captain", al3);//将项目信息装入al3 String sql4 = "select * from school"; String[] args4 = {"id","name","pwd","truename","addr","station"}; ArrayList al4 = sb.select(sql4, args4); request.setAttribute("school", al4);//将学校信息装入al4 String sql5 = "select * from student"; String[] args5 = {"id","name","pwd","truename","sex","age","phone","addr","station"}; ArrayList al5 = sb.select(sql5, args5); request.setAttribute("student", al5);//将学生信息装入al5 String sql11 = "select * from project,projectbase where project.baseid = projectbase.baseid"; String[] args11 = {"projectid","projectname","basename","projectinfo","state"}; ArrayList al11 = sb.select(sql11, args11); request.setAttribute("project", al11);//关联两表,查询学生申请的项目信息,装入al11 String sql6 = "select * from course"; String[] args6 = {"id","name","times","student"}; ArrayList al6 = sb.select(sql6, args6); request.setAttribute("course", al6);//成绩信息装入al6 Validate vd = new Validate(); String baseid = vd.getUnicode(request.getParameter("baseid")); String sql7 = "select * from project where baseid="+baseid; String[] args7 = {"projectid","projectname","baseid","projectinfo","state"}; ArrayList al7 = sb.select(sql7, args7); request.setAttribute("studentleave", al7);//根据传递过来的参数,查找学生申请情况,装入al7 String sql12 = "select * from resource where baseid="+baseid; String[] args12 = {"resourcename","resourcenumber","resourceinfor","resourcestate","baseid","teacherid","resourceid"}; ArrayList al12 = sb.select(sql12, args12); request.setAttribute("resources", al12);//根据传递过来的参数,查询与项目相关的指定项目的资源信息,转入al12 /* HttpSession session = request.getSession(); ArrayList adminlogin = (ArrayList)session.getAttribute("adminlogin"); if(!adminlogin.get(3).equals(1)) { ArrayList login = (ArrayList)session.getAttribute("login"); String sql12 = "select project.projectid,projectname,projectinfo,teacherproject.state from teacherproject,project,teacher where project.projectid = teacherproject.projectid and teacherproject.id=teacher.id and teacherproject.id="+login.get(0); String[] args12 = {"projectid","projectname","projectinfo","state"}; ArrayList al12 = sb.select(sql12, args12); request.setAttribute("teacherlookapply", al12); // HttpSession session = request.getSession(); ArrayList login1 = (ArrayList)session.getAttribute("login"); String sql13 = "select project.projectid,projectname,projectinfo,studentproject.state from studentproject,project,student where project.projectid = studentproject.projectid and studentproject.id=student.id and studentproject.id="+login1.get(0); String[] args13 = {"projectid","projectname","projectinfo","state"}; ArrayList al13 = sb.select(sql13, args13); request.setAttribute("studentlookapply", al13); } */ String sql8 = "select * from teacherleave"; String[] args8 = {"id","teacherid","teacher","title","content","times"}; ArrayList al8 = sb.select(sql8, args8); request.setAttribute("teacherleave", al8);//查询老师留言,装入al8 String sql9 = "select * from grade"; String[] args9 = {"id","studentd","coursety","gradentn"}; ArrayList al9 = sb.select(sql9, args9); request.setAttribute("grade", al9);//查询年级信息装入al9 String sql10 = "select * from taste"; String[] args10 = {"id","student","title","content","times"}; ArrayList al10 = sb.select(sql10, args10); request.setAttribute("taste", al10);//查询学生留言 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } }
3 SessLoginServlet
检查session,检查是否登陆成功
package servlet; 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; public class SessLoginServlet extends HttpServlet { /** * Constructor of the object. */ public SessLoginServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); if(request.getSession().getAttribute("login") == null || request.getSession().getAttribute("login").equals("")){ pw.write("<script>alert('您还没有登录!');window.navigate('../user/index.jsp');</script>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } }
3 user/top.jsp
页面的抬头分离写出来,别的页面时用include指令包含进来
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>研究生工程实践管理系统</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="img/css.css"> </head> <jsp:include flush="true" page="/servlet/SessLoginServlet"></jsp:include> <body> <div align="center"> <table style="border-style: dotted; border-width: 1px"> <tr><td width="800" height="100" style="font-family: 华文行楷; font-size: 70px; border-style: ridge; border-width: 1px; color:#332590; font-style:italic" bgcolor="#C0C0C0" align="center"> 研究生工程实践管理系统</td></tr> <tr> <td align="center" valign="top"> <table width="100%"> <tr><td align="center" height="30" background="img/bg222.gif"> <table> <tr> <%-- <td><a href="user/course_list.jsp">课程安排|</a></td> <td><a href="user/studentleave.jsp">学生留言|</a></td> <td><a href="user/teacherleave.jsp">老师留言|</a></td> <td><a href="user/grade_list.jsp">学生成绩发布|</a></td> --%> <td><a href="user/student_list_2.jsp">工程实践项目管理|</a></td> <%//检查login是否登陆成功,显示教学资源管理链接 if(request.getSession().getAttribute("login") != null && !request.getSession().getAttribute("login").equals("")){ ArrayList al = (ArrayList)session.getAttribute("login"); if(al.get(3).equals("指导老师")){ %><td><a href="user/teacher_resource.jsp">实践教学资源管理|</a></td><% }} %> <%-- <td><a href="">实践教学资源管理|</a></td>--%> <td><a href="servlet/UserShowServlet">个人信息管理|</a></td> <td><a href="user/modifypwd.jsp">修改密码|</a></td> <%-- <td><a href="servlet/CopyOfUserShowServlet">个人信息管理|</a></td>--%> <td><a href="servlet/RemoveServlet?login=1">注销退出</a></td> </tr> </table> </td></tr> </table> </td> </tr> </table> </div> </body> </html>
4 StudentSelectServlet
package servlet.user; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.SelectBean; import util.Validate; public class StudentSelectServlet_1 extends HttpServlet { /** * Constructor of the object. */ public StudentSelectServlet_1() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Validate vd = new Validate(); String projectname = vd.getUnicode(request.getParameter("projectname_1")); SelectBean sb = new SelectBean(); String sql = "select * from project,projectbase "; String[] args = {"projectid","projectname","basename","projectinfo","state"}; sql += "where project.baseid = projectbase.baseid and projectname like '%"+projectname+"%'"; ArrayList al = sb.select(sql, args); request.setAttribute("students", al); RequestDispatcher rd=request.getRequestDispatcher("/user/student_list_3.jsp"); rd.forward(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } }
学生管理系统开发代码分析笔记:jsp+java bean+servlet技术
最新推荐文章于 2022-12-22 12:12:47 发布