选课系统中用到了4个表,分别是classs、yonghu、teacher、student。在用户中存放管理员的信息name和password以及id,在另三个表中存放对应的数据如图:
calss:
teacher:
student:
yonghu:
首先root用户提前定义好名字以及密码,老师和学生可以由root进行增加
登录时根据选择的用户类型将输入的用户名和密码与数据库中对应的进行判断,根据用户的不同类型跳转到不同的界面。登陆成功后将登录用户的名字存放到session中,之后进行一系列例如更改个人信息、添加课程时直接调用session中的当前用户的名字进行更改。
下面是代码:
DAO:
1 package Dao; 2 3 import java.sql.Connection; 4 import java.sql.Statement; 5 import java.util.ArrayList; 6 import java.util.List; 7 import java.sql.ResultSet; 8 import DBUtil.DBUtil; 9 import Entity.teacher; 10 import Entity.student; 11 import Entity.Classs; 12 public class Dao { 13 public String dopost(String username,String password,String leibie) { 14 String i="-1"; 15 String sql=null; 16 if(leibie.equals("老师")) 17 { 18 sql="select * from teacher where xingming = '"+username+"'"; 19 } 20 else if(leibie.equals("学生")) 21 { 22 sql="select * from student where xingming = '"+username+"'"; 23 } 24 else if(leibie.equals("管理员")) 25 { 26 sql="select * from yonghu where name = '"+username+"'"; 27 } 28 Connection conn = DBUtil.getConn(); 29 Statement state = null; 30 ResultSet rs = null; 31 try { 32 state = conn.createStatement(); 33 rs = state.executeQuery(sql); 34 while(rs.next()) { 35 String password1 = rs.getString("password"); 36 if(password.equals(password1)) { 37 i=rs.getString("id"); 38 } 39 break; 40 } 41 }catch (Exception e) { 42 e.printStackTrace(); 43 } finally { 44 DBUtil.close(rs,state, conn); 45 } 46 return i; 47 } 48 public boolean teadd(teacher tea) { 49 50 String sql = "insert into teacher(gonghao,xingbie,xingming,xuexiao,zhicheng,password,id) values('"+ tea.getGonghao() + "','"+ tea.getXingbie() +"','"+ tea.getXingming() +"','" + tea.getXuexiao() +"','"+ tea.getZhicheng() +"' , '"+tea.getPassword()+"' , '"+tea.getId()+"')"; 51 Connection conn = DBUtil.getConn(); 52 Statement state = null; 53 boolean f = false; 54 int a = 0; 55 56 try { 57 state = conn.createStatement(); 58 a=state.executeUpdate(sql); 59 } catch (Exception e) { 60 e.printStackTrace(); 61 } finally { 62 63 DBUtil.close(state, conn); 64 } 65 66 if (a > 0) { 67 f = true; 68 } 69 return f; 70 71 } 72 public boolean stadd(student stu) { 73 74 String sql = "insert into student(xuehao,xingming,xingbie,banji,zhuanye,password,id) values('"+ stu.getXuehao() + "','"+ stu.getXingming() +"','"+ stu.getXingbie() +"','" + stu.getBanji() +"','"+ stu.getZhuanye() +"' , '"+stu.getPassword()+"' , '"+stu.getId()+"')"; 75 Connection conn = DBUtil.getConn(); 76 Statement state = null; 77 boolean f = false; 78 int a = 0; 79 80 try { 81 state = conn.createStatement(); 82 a=state.executeUpdate(sql); 83 } catch (Exception e) { 84 e.printStackTrace(); 85 } finally { 86 87 DBUtil.close(state, conn); 88 } 89 90 if (a > 0) { 91 f = true; 92 } 93 return f; 94 95 } 96 public boolean claadd(String bianhao,String name,String number,String prename) { 97 98 99 String sql = "insert into classs(clahao,claname,number,tea,num) values('"+ bianhao + "','"+ name +"','"+ number +"','"+prename+"', '0')"; 100 Connection conn = DBUtil.getConn(); 101 Statement state = null; 102 boolean f = false; 103 int a = 0; 104 105 try { 106 state = conn.createStatement(); 107 a=state.executeUpdate(sql); 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } finally { 111 112 DBUtil.close(state, conn); 113 } 114 115 if (a > 0) { 116 f = true; 117 } 118 return f; 119 120 } 121 public boolean teagai(String leibie,String neirong,String prename) { 122 123 String sql = "update teacher set "+ leibie+" = '"+ neirong+"' "+"where xingming = '"+prename+"'"; 124 Connection conn = DBUtil.getConn(); 125 Statement state = null; 126 boolean f = false; 127 int a = 0; 128 129 try { 130 state = conn.createStatement(); 131 a=state.executeUpdate(sql); 132 } catch (Exception e) { 133 e.printStackTrace(); 134 } finally { 135 136 DBUtil.close(state, conn); 137 } 138 139 if (a > 0) { 140 f = true; 141 } 142 return f; 143 144 } 145 public boolean stugai(String leibie,String neirong,String prename) { 146 147 String sql = "update student set "+ leibie+" = '"+ neirong+"' "+"where xingming = '"+prename+"'"; 148 Connection conn = DBUtil.getConn(); 149 Statement state = null; 150 boolean f = false; 151 int a = 0; 152 153 try { 154 state = conn.createStatement(); 155 a=state.executeUpdate(sql); 156 } catch (Exception e) { 157 e.printStackTrace(); 158 } finally { 159 160 DBUtil.close(state, conn); 161 } 162 163 if (a > 0) { 164 f = true; 165 } 166 return f; 167 168 } 169 public List<Classs> list(){ 170 String sql="select * from classs"; 171 Connection conn = DBUtil.getConn(); 172 Statement state =null; 173 ResultSet rs = null; 174 List<Classs> list = new ArrayList<>(); 175 try { 176 state = conn.createStatement(); 177 rs = state.executeQuery(sql); 178 Classs bean = null; 179 while (rs.next()) { 180 String claname1=rs.getString("claname"); 181 String clahao1=rs.getString("clahao"); 182 String number1=rs.getString("number"); 183 String tea1=rs.getString("tea"); 184 String num1=rs.getString("num"); 185 bean = new Classs(clahao1,claname1,number1,tea1,num1); 186 list.add(bean); 187 188 } 189 190 }catch (Exception e) { 191 e.printStackTrace(); 192 } finally { 193 DBUtil.close(rs,state, conn); 194 } 195 return list; 196 } 197 public boolean jia(String num,String number,String clahao) { 198 boolean f=false; 199 int n1=Integer.parseInt(num); 200 int n2=Integer.parseInt(number); 201 if(n1<n2) { 202 n1++; 203 String num1=null; 204 num1 = String.valueOf(n1); 205 String sql = "update classs set num = '"+num1+"' where clahao = '"+clahao+"'"; 206 Connection conn = DBUtil.getConn(); 207 Statement state = null; 208 int a = 0; 209 try { 210 state = conn.createStatement(); 211 a=state.executeUpdate(sql); 212 } catch (Exception e) { 213 e.printStackTrace(); 214 } finally { 215 216 DBUtil.close(state, conn); 217 } 218 219 if (a > 0) { 220 f = true; 221 } 222 223 } 224 return f; 225 } 226 }
DBUtil:
1 package DBUtil; 2 3 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 11 12 public class DBUtil { 13 14 public static String db_url = "**********"; 15 public static String db_user = "****"; 16 public static String db_pass = "******"; 17 18 public static Connection getConn () { 19 Connection conn = null; 20 21 try { 22 Class.forName("com.mysql.jdbc.Driver"); 23 conn = DriverManager.getConnection(db_url, db_user, db_pass); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 28 return conn; 29 }//end getConn 30 31 public static void close (Statement state, Connection conn) { 32 if (state != null) { 33 try { 34 state.close(); 35 } catch (SQLException e) { 36 e.printStackTrace(); 37 } 38 } 39 40 if (conn != null) { 41 try { 42 conn.close(); 43 } catch (SQLException e) { 44 e.printStackTrace(); 45 } 46 } 47 } 48 49 public static void close (ResultSet rs, Statement state, Connection conn) { 50 if (rs != null) { 51 try { 52 rs.close(); 53 } catch (SQLException e) { 54 e.printStackTrace(); 55 } 56 } 57 58 if (state != null) { 59 try { 60 state.close(); 61 } catch (SQLException e) { 62 e.printStackTrace(); 63 } 64 } 65 66 if (conn != null) { 67 try { 68 conn.close(); 69 } catch (SQLException e) { 70 e.printStackTrace(); 71 } 72 } 73 } 74 75 public static void main(String[] args) throws SQLException { 76 Connection conn = getConn(); 77 PreparedStatement pstmt = null; 78 ResultSet rs = null; 79 String sql ="select * from yonghu"; 80 pstmt = conn.prepareStatement(sql); 81 rs = pstmt.executeQuery(); 82 if(rs.next()){ 83 System.out.println("连接成功"); 84 }else{ 85 System.out.println("连接失败"); 86 } 87 } 88 }
Entity中定义了4个类:
classs:
1 package Entity; 2 3 public class Classs { 4 private String clahao; 5 private String claname; 6 private String number; 7 private String tea; 8 private String num; 9 public String getClahao() { 10 return clahao; 11 } 12 public void setClahao(String clahao) { 13 this.clahao = clahao; 14 } 15 public String getClaname() { 16 return claname; 17 } 18 public void setClaname(String claname) { 19 this.claname = claname; 20 } 21 public String getNumber() { 22 return number; 23 } 24 public void setNumber(String number) { 25 this.number = number; 26 } 27 public String getTea() { 28 return tea; 29 } 30 public void setTea(String tea) { 31 this.tea = tea; 32 } 33 public String getNum() { 34 return num; 35 } 36 public void setNum(String num) { 37 this.num = num; 38 } 39 public Classs(String clahao,String claname,String number, String tea,String num) { 40 super(); 41 this.clahao=clahao; 42 this.claname=claname; 43 this.number=number; 44 this.tea=tea; 45 this.num=num; 46 } 47 48 }
另外三个是user、teacher、student结构和这个一样就不上了。
Servlet:
1 package Servlet; 2 import java.io.IOException; 3 import java.util.List; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11 import Entity.teacher; 12 import Entity.User; 13 import Entity.student; 14 import Entity.Classs; 15 import Dao.Dao; 16 17 18 19 20 @WebServlet("/Servlet") 21 public class Servlet extends HttpServlet { 22 private static final long serialVersionUID = 1L; 23 24 25 public Servlet() { 26 super(); 27 28 } 29 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 30 req.setCharacterEncoding("utf-8"); 31 String method = req.getParameter("method"); 32 if ("dopost".equals(method)) { 33 dopost(req,resp); 34 } 35 if ("tiao".equals(method)) { 36 tiao(req,resp); 37 } 38 if ("teadd".equals(method)) { 39 teadd(req,resp); 40 } 41 if ("stadd".equals(method)) { 42 stadd(req,resp); 43 } 44 if ("claadd".equals(method)) { 45 claadd(req,resp); 46 } 47 if ("teagai".equals(method)) { 48 teagai(req,resp); 49 } 50 if ("stugai".equals(method)) { 51 stugai(req,resp); 52 } 53 if("list".equals(method)){ 54 list(req,resp); 55 } 56 if("jia".equals(method)) { 57 jia(req,resp); 58 } 59 60 } 61 62 63 private void dopost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 64 65 req.setCharacterEncoding("utf-8"); 66 String username = req.getParameter("username"); 67 String password = req.getParameter("password"); 68 String leibie=req.getParameter("leibie"); 69 HttpSession session = req.getSession(); 70 session.setAttribute("prename",username); 71 Dao dao=new Dao(); 72 String id=dao.dopost(username, password,leibie); 73 if(id.equals("-1")) { 74 req.setAttribute("message", "登录失败!"); 75 req.getRequestDispatcher("index.jsp").forward(req,resp); 76 } 77 else if(id.equals("0")) { 78 req.setAttribute("message", "登陆成功!"); 79 req.getRequestDispatcher("allteacher.jsp").forward(req,resp); 80 } 81 else if(id.equals("1")) { 82 req.setAttribute("message", "登陆成功!"); 83 req.getRequestDispatcher("allstudent.jsp").forward(req,resp); 84 } 85 else if(id.equals("2")) { 86 req.setAttribute("message", "登陆成功!"); 87 req.getRequestDispatcher("root.jsp").forward(req,resp); 88 } 89 } 90 private void tiao(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 91 req.setCharacterEncoding("utf-8"); 92 String leibie=req.getParameter("leibie"); 93 if(leibie.equals("学生")) { 94 req.setAttribute("message", "请开始添加学生信息!"); 95 req.getRequestDispatcher("student.jsp").forward(req,resp); 96 } 97 if(leibie.equals("老师")) { 98 req.setAttribute("message", "请添加老师信息!"); 99 req.getRequestDispatcher("teacher.jsp").forward(req,resp); 100 } 101 } 102 private void teadd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 103 req.setCharacterEncoding("utf-8"); 104 String gonghao=req.getParameter("gonghao"); 105 String xingming=req.getParameter("xingming"); 106 String xingbie=req.getParameter("xingbei"); 107 String xuexiao=req.getParameter("xuexiao"); 108 String zhicheng=req.getParameter("zhicheng"); 109 String password=req.getParameter("password"); 110 String id="0"; 111 teacher tea=new teacher(gonghao,xingbie,xingming,xuexiao,zhicheng,password,id); 112 Dao dao =new Dao(); 113 boolean f=dao.teadd(tea); 114 if(f) { 115 req.setAttribute("message", "添加成功!"); 116 req.getRequestDispatcher("root.jsp").forward(req,resp); 117 } else { 118 req.setAttribute("message", "添加失败!"); 119 req.getRequestDispatcher("teacher.jsp").forward(req,resp); 120 } 121 } 122 private void stadd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 123 req.setCharacterEncoding("utf-8"); 124 String xuehao=req.getParameter("xuehao"); 125 String xingming=req.getParameter("xingming"); 126 String xingbie=req.getParameter("xingbei"); 127 String banji=req.getParameter("banji"); 128 String zhuanye=req.getParameter("zhuanye"); 129 String password=req.getParameter("password"); 130 String id="1"; 131 student stu=new student(xuehao,xingming,xingbie,banji,zhuanye,password,id); 132 Dao dao =new Dao(); 133 boolean f=dao.stadd(stu); 134 if(f) { 135 req.setAttribute("message", "添加成功!"); 136 req.getRequestDispatcher("root.jsp").forward(req,resp); 137 } else { 138 req.setAttribute("message", "添加失败!"); 139 req.getRequestDispatcher("teacher.jsp").forward(req,resp); 140 } 141 } 142 private void claadd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 143 req.setCharacterEncoding("utf-8"); 144 HttpSession session = req.getSession(); 145 String prename=null; 146 prename=(String)session.getAttribute("prename"); 147 String bianhao=req.getParameter("hao"); 148 String name=req.getParameter("name"); 149 String number=req.getParameter("number"); 150 Dao dao =new Dao(); 151 boolean f=dao.claadd(bianhao,name,number,prename); 152 if(f) { 153 req.setAttribute("message", "添加成功!"); 154 req.getRequestDispatcher("allteacher.jsp").forward(req,resp); 155 } else { 156 req.setAttribute("message", "添加失败!"); 157 req.getRequestDispatcher("addclass.jsp").forward(req,resp); 158 } 159 } 160 private void teagai(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 161 req.setCharacterEncoding("utf-8"); 162 HttpSession session = req.getSession(); 163 String prename=null; 164 prename=(String)session.getAttribute("prename"); 165 String leibie=req.getParameter("leibie"); 166 String neirong=req.getParameter("neirong"); 167 168 Dao dao =new Dao(); 169 boolean f=dao.teagai(leibie,neirong,prename); 170 if(f) { 171 req.setAttribute("message", "修改成功!"); 172 req.getRequestDispatcher("allteacher.jsp").forward(req,resp); 173 } else { 174 req.setAttribute("message", "修改失败!"); 175 req.getRequestDispatcher("updatetea.jsp").forward(req,resp); 176 } 177 } 178 private void stugai(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 179 req.setCharacterEncoding("utf-8"); 180 HttpSession session = req.getSession(); 181 String prename=null; 182 prename=(String)session.getAttribute("prename"); 183 String leibie=req.getParameter("leibie"); 184 String neirong=req.getParameter("neirong"); 185 Dao dao =new Dao(); 186 boolean f=dao.stugai(leibie,neirong,prename); 187 if(f) { 188 req.setAttribute("message", "修改成功!"); 189 req.getRequestDispatcher("allstudent.jsp").forward(req,resp); 190 } else { 191 req.setAttribute("message", "修改失败!"); 192 req.getRequestDispatcher("updatestu.jsp").forward(req,resp); 193 } 194 } 195 private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 196 197 Dao dao=new Dao(); 198 List<Classs> holds = dao.list(); 199 req.setAttribute("holds", holds); 200 req.getRequestDispatcher("xuan.jsp").forward(req,resp); 201 } 202 private void jia(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 203 req.setCharacterEncoding("UTF-8"); 204 String num=req.getParameter("num"); 205 String number=req.getParameter("number"); 206 String clahao=req.getParameter("clahao"); 207 Dao dao=new Dao(); 208 boolean f=dao.jia(num, number, clahao); 209 if(f) { 210 req.setAttribute("message", "选课成功!"); 211 req.getRequestDispatcher("allstudent.jsp").forward(req,resp); 212 } else { 213 req.setAttribute("message", "选课失败!"); 214 req.getRequestDispatcher("xuan.jsp").forward(req,resp); 215 } 216 } 217 }
然后就是各个jsp界面了
index:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>登录</title> 8 </head> 9 <body> 10 <% 11 Object message = request.getAttribute("message"); 12 if (message != null && !"".equals(message)) { 13 %> 14 <script type="text/javascript"> 15 alert("<%=request.getAttribute("message")%>"); 16 var asd=request.getAttribute("username"); 17 </script> 18 <% 19 } 20 %> 21 <form action="Servlet?method=dopost" method="post"> 22 <div> 23 用户名<input type="text" name="username" /> 24 </div> 25 <div> 26 密码<input type="password" name="password" /> 27 </div> 28 <div> 29 <th>类别</th> <select name="leibie"> 30 <option>学生</option> 31 <option>老师</option> 32 <option>管理员</option> 33 </select> 34 </div> 35 <div> 36 <input type="submit" value="登录" /> 37 </div> 38 </form> 39 </body> 40 </html>
root:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>当前位置:添加信息</title> 8 </head> 9 <body> 10 <% 11 Object message = request.getAttribute("message"); 12 if (message != null && !"".equals(message)) { 13 %> 14 <script type="text/javascript"> 15 alert("<%=request.getAttribute("message")%>"); 16 var asd=request.getAttribute("username"); 17 </script> 18 <% 19 } 20 %> 21 <form action="Servlet?method=tiao" method="post"> 22 <div> 23 <th>类别</th> <select name="leibie"> 24 <option>学生</option> 25 <option>老师</option> 26 </select> 27 </div> 28 <div> 29 <input type="submit" value="提交" /> 30 </div> 31 32 33 </form> 34 35 36 </body> 37 38 </html>
teacher:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加老师信息</title> </head> <body> <% Object message = request.getAttribute("message"); if (message != null && !"".equals(message)) { %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); var asd=request.getAttribute("name"); </script> <% } %> <form action="Servlet?method=teadd" method="post" > <table > <tr> <th>工号:</th> <td> <input name="gonghao" type="text" /> </td> </tr> <tr> <th>姓名:</th> <td> <input name="xingming" type="text" /> </td> </tr> <tr> <th>性别:</th> <td> <input name="xingbei" type="radio" value="男" />男 <input name="xingbei" type="radio" value="女" />女 </td> </tr> <tr> <th>学校:</th> <td> <input name="xuexiao" type="text"> </td> </tr> <tr> <th>职称:</th> <td> <input name="zhicheng" type="text"> </td> </tr> <tr> <th>密码:</th> <td> <input name="password" type="text"> </td> </tr> <tr> <td > <input type="submit" value="提交" /> </td> </tr> </table> </form> </body> </html>
student:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加学生信息</title> </head> <body> <% Object message = request.getAttribute("message"); if (message != null && !"".equals(message)) { %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); var asd=request.getAttribute("name"); </script> <% } %> <form action="Servlet?method=stadd" method="post"> <table> <tr> <th>学号:</th> <td><input name="xuehao" type="text" /></td> </tr> <tr> <th>姓名:</th> <td><input name="xingming" type="text" /></td> </tr> <tr> <th>性别:</th> <td><input name="xingbei" type="radio" value="男" />男 <input name="xingbei" type="radio" value="女" />女</td> </tr> <tr> <th>班级:</th> <td><input name="banji" type="text"></td> </tr> <tr> <th>专业:</th> <td><input name="zhuanye" type="text"></td> </tr> <tr> <th>密码:</th> <td><input name="password" type="text"></td> </tr> <tr> <td><input type="submit" value="提交" /></td> </tr> </table> </form> </body> </html>
allteacher:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>当前位置:主页</title> 8 </head> 9 <body> 10 <% 11 Object message = request.getAttribute("message"); 12 if (message != null && !"".equals(message)) { 13 %> 14 <script type="text/javascript"> 15 alert("<%=request.getAttribute("message")%>"); 16 var asd=request.getAttribute("username"); 17 </script> 18 <% 19 } 20 %> 21 <% String prename=null; 22 prename=request.getParameter(prename); 23 %> 24 25 26 <div align="center"> 27 <div>当前用户:${prename }</div> 28 <div> 29 30 <a href="updatetea.jsp">修改个人信息</a> 31 </div> 32 33 <div> 34 <a href="addclass.jsp">添加课程信息</a> 35 36 </div> 37 38 </div> 39 40 41 42 43 44 </body> 45 46 </html>
allstudent:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>当前位置:主页</title> 9 </head> 10 <body> 11 <% 12 Object message = request.getAttribute("message"); 13 if (message != null && !"".equals(message)) { 14 %> 15 <script type="text/javascript"> 16 alert("<%=request.getAttribute("message")%>"); 17 var asd=request.getAttribute("username"); 18 </script> 19 <% 20 } 21 %> 22 <% String prename=null; 23 prename=request.getParameter(prename); 24 %> 25 26 27 <div align="center"> 28 <div>当前用户:${prename }</div> 29 <div> 30 31 <a href="updatestu.jsp">修改个人信息</a> 32 </div> 33 34 <div> 35 <a href="Servlet?method=list">选课</a> 36 37 </div> 38 39 </div> 40 41 42 43 44 </body> 45 46 </html>
addclass:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>添加老师信息</title> 8 </head> 9 <body> 10 <% 11 Object message = request.getAttribute("message"); 12 if (message != null && !"".equals(message)) { 13 %> 14 <script type="text/javascript"> 15 alert("<%=request.getAttribute("message")%>"); 16 var asd=request.getAttribute("name"); 17 </script> 18 <% 19 } 20 %> 21 <form action="Servlet?method=claadd" method="post"> 22 <table> 23 24 <tr> 25 <th>课程编号:</th> 26 <td><input name="hao" type="text" /></td> 27 </tr> 28 <tr> 29 <th>课程名称:</th> 30 <td><input name="name" type="text" /></td> 31 </tr> 32 <tr> 33 <th>选课人数:</th> 34 <td><input name="number" type="text" /></td> 35 </tr> 36 <tr> 37 <td><input type="submit" value="提交" /></td> 38 </tr> 39 40 </table> 41 42 43 44 45 </form> 46 </body> 47 </html>
updatestu:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 Object message = request.getAttribute("message"); 13 if(message!=null && !"".equals(message)){ 14 %> 15 <script type="text/javascript"> 16 alert("<%=request.getAttribute("message")%>"); 17 </script> 18 <%} %> 19 <% 20 String prename=null; 21 prename=request.getParameter(prename); 22 %> 23 24 25 <div align="center"> 26 <div>${prename }</div> 27 <form action="Servlet?method=stugai" method="post" > 28 29 30 <div> 31 <th>类别</th> 32 <select name="leibie"> 33 <option>xuehao</option> 34 <option>xingming</option> 35 <option>xingbie</option> 36 <option>banji</option> 37 <option>zhuanye</option> 38 </select> 39 </div> 40 <div> 41 <input type="text" name="neirong" /> 42 </div> 43 44 <div> 45 <input type="submit" value="提交" /> 46 </div> 47 </form> 48 </div> 49 </body> 50 </html>
updatetea:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 Object message = request.getAttribute("message"); 12 if(message!=null && !"".equals(message)){ 13 %> 14 <script type="text/javascript"> 15 alert("<%=request.getAttribute("message")%>"); 16 </script> 17 <%} %> 18 <% 19 String prename=null; 20 prename=request.getParameter(prename); 21 %> 22 <div align="center"> 23 <div>当前用户:${prename }</div> 24 <form action="Servlet?method=teagai" method="post"> 25 26 <div> 27 <th>类别</th> <select name="leibie"> 28 <option>gonghao</option> 29 <option>xingming</option> 30 <option>xingbie</option> 31 <option>xuexiao</option> 32 <option>zhicheng</option> 33 </select> 34 </div> 35 <div> 36 <input type="text" name="neirong" /> 37 </div> 38 39 <div> 40 <input type="submit" value="提交" /> 41 </div> 42 </form> 43 </div> 44 </body> 45 </html>
xuan:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 5 <!DOCTYPE html> 6 <html> 7 <head> 8 <meta charset="UTF-8"> 9 <title>课程信息</title> 10 </head> 11 <body> 12 <% 13 Object message = request.getAttribute("message"); 14 if (message != null && !"".equals(message)) { 15 %> 16 <script type="text/javascript"> 17 alert("<%=request.getAttribute("message")%>"); 18 var asd=request.getAttribute("username"); 19 </script> 20 <% 21 } 22 %> 23 <table > 24 <tr> 25 <td>课程编号</td> 26 <td>课程名称</td> 27 <td>任课老师</td> 28 <td>已选人数</td> 29 <td>课程人数</td> 30 </tr> 31 <!-- forEach遍历出adminBeans --> 32 <c:forEach items="${holds}" var="item" varStatus="status"> 33 <tr> 34 <td>${item.clahao}</td> 35 <td>${item.claname}</td> 36 <td>${item.tea}</td> 37 <td>${item.num}</td> 38 <td>${item.number}</td> 39 <td><a href="Servlet?method=jia&num=${item.num }&number=${item.number}&clahao=${item.clahao}" >选课</a> </td> 40 </tr> 41 </c:forEach> 42 </table> 43 44 45 46 </body> 47 </html>
这就是全部的代码了,做这个系统的过程中我遇到的难点是如何保持一个用户的登录状态,可以再之后这个用户进行操作时简单便捷的获取当前用户的信息,找了资料之后发现session对象符合我的要求,便开始在我原来的代码上进行更改。最终实现了登录状态的保持。