catalogue
1, Add students / teachers in the administrator interface
1. login.jsp login identity judgment
2. Create two new views corresponding to teachers and students
3. Create two mainform s corresponding to teachers and students
Three, fuzzy search teacher to see all topics.
1. Topic selection data table display (same as that of student and teacher)
2. Fuzzy search (sql: like keyword)
2. Data interface for fuzzy search: SelectTitleServlet
5, The teacher graded the students
1. Click student scoring to display only the students belonging to the teacher
1. A new method is added in teacherdao: edit student grade
Error note:
1. Error: unexpected syntax error: unexpected string
The colon is used to pass values in data in ajax
data:{ 'studentId':studentId, 'name':name, 'sex':sex, 'password':password },
(80 messages) data transmission parameter error in ajax_ m0_50052896 blog - CSDN blog
2. Front end data cannot be transmitted back:
Comment out contentType: / * contentType:'application/json;charset=utf-8', */
3. Error:
java.sql.SQLSyntaxErrorException: Unknown column 'teachertId' in 'field list'
teacherId is careless and wrong!!!
4. The front-end data is OK, but the database does not change
The non automatic submission request is set in the DBUtil tool class, so the modification request needs to be submitted manually when the Dao layer changes the table (add, delete, modify)
//Submit dbUtil.commit();
1, Add students / teachers in the administrator interface
allStudent. HTML - > addstudentservlet - > adminservice - > adminserviceimpl - > admindao - > admindaoimpl - > Return
2, Sub role
1. login.jsp login identity judgment
success:function(res){//Callback function if(res=="success"){ if(entity=="administrators"){ alert("Login succeeded! About to jump to the administrator home page"); //The address of the mainform cannot be written directly in href, because it is protected in the WEB-INF directory, so you can use the window.location.href="/test/ViewMainFormServlet"; }else if(entity=="teacher"){ alert("Login succeeded! About to jump to the teacher's home page"); //The address of the mainform cannot be written directly in href, because it is protected in the WEB-INF directory, so you can use the window.location.href="/test/ViewTeacherFormServlet"; }else if(entity=="student"){ alert("Login succeeded! About to jump to the student home page"); //The address of the mainform cannot be written directly in href, because it is protected in the WEB-INF directory, so you can use the window.location.href="/test/ViewStudentFormServlet"; } }
2. Create two new views corresponding to teachers and students
Jumping to a jsp file requires a view layer
In the dopost method, jump to the student and teacher's MainForm JSP, which is newly created below
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //doGet(request, response); request.getRequestDispatcher("/WEB-INF/jsp/StudentMainForm.jsp").forward(request, response); }
3. Create two mainform s corresponding to teachers and students
Change corresponding operation
<dd><a href="/test/allStudent.html">I want to choose a topic</a></dd> <dd><a href="/test/allTeacher.html">Submit work</a></dd> <dd><a href="/test/addStudent.html">Query results</a></dd>
Three, fuzzy search teacher to see all topics.
1. Topic selection data table display (same as that of student and teacher)
Data interface for displaying tables and pages: SelectAllTitleServlet
2. Fuzzy search (sql: like keyword)
1. allTitle.html
Click the submit button to add the select method
<script type="text/javascript"> function select(){ var select=$('#search').val(); var table = layui.table; table.reload('tableReload',{ /* fuzzy search */ url:'SelectTitleServlet?select='+select, where:{ select:select } }); } </script>
2. Data interface for fuzzy search: SelectTitleServlet
Call dao layer
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //doGet(request, response); String name=request.getParameter("select"); userDao userDao=new userDaoImpl(); try { List<Object> list = userDao.selectTileList2(name); response.setContentType("text/html;charset=utf-8"); //Transfer json data Vo vo=new Vo(); vo.setCode(0); vo.setMsg("success"); //What you need here is the sum of the number in the database vo.setCount(userDao.countTitle()); vo.setData(list); //Convert the json type to a string and return it to the front end response.getWriter().write(JSONObject.toJSONString(vo)); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
3. dao layer
sql statement: where like: Fuzzy Lookup
public List<Object> selectTileList2(String name) throws SQLException { DBUtil dbUtil = new DBUtil(); String sql="select * from titleinfo where name like '%"+name+"%'"; //There is no need for question mark reference and where statement query. You can use statement directly Statement st=(Statement) dbUtil.getStatement(); //Execute the sql statement to get the result set rs ResultSet rs=(st).executeQuery(sql); List<Object> list=new ArrayList<Object>(); while(rs.next()) { Title tile = new Title(); tile.setTeacherId(rs.getString("teacherId")); tile.setName(rs.getString("name")); tile.setType(rs.getString("type")); tile.setTitleId(rs.getInt("titleId")); list.add(tile); } return list; }
4, Teachers upload questions
1. allTitle.html
2. addTitle.jsp
layui multi file upload template
uploadservlet upload interface: modify path
1. Realize the function of read-only job number and corresponding response account when adding a topic
1. Checklogin servlet: put the accounts of administrators, students and teachers in the session domain, so that the teacher information can be in addtitle JSP
HttpSession session=request.getSession();
session.setAttribute("teacher", teacher);
if(entity.equals("teacher")) { Teacher teacher; try { teacher=loginService.selectAllTeacher(name, password); if(teacher!=null) { HttpSession session=request.getSession(); session.setAttribute("teacher", teacher); response.getWriter().write("success"); } else { response.getWriter().write("fail"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
2. dao layer: when logging in, pass the user name, password and corresponding id to the teacher and return to the front end. Then the job number when the teacher adds a topic is the corresponding teacherid when logging in
public Teacher selectAllTeacher(String name, String password) throws SQLException{ // TODO Auto-generated method stub DBUtil dbUtil = new DBUtil(); String sql="select * from s_teacher where name =? and password =?"; PreparedStatement ps=(PreparedStatement)dbUtil.getPreparedStatement(sql); ps.setString(1, name); ps.setString(2, password); ResultSet rs=ps.executeQuery(); Teacher teacher= new Teacher(); while(rs.next()){ teacher.setTeacherId(rs.getString("teacherId")); teacher.setTname(rs.getString("name")); teacher.setPassword(rs.getString("password")); return teacher; } return null; }
3. addTitle.jsp: the value of the teacher ID is the teacher ID corresponding to the user name and password when we log in
//Introduce teachers into <%@ page import="com.sxh.pojo.*" %> <!-- write java The code needs to be enclosed by a percent sign ,obtain teacherId,Automatically fill in the job No. text box--> <%Teacher teacher=(Teacher)session.getAttribute("teacher"); String teacherId=teacher.getTeacherId(); %> <label>Teacher job No.:</label> <input type="text" id="teacherId" value=<%=teacherId%> readonly="readonly"> <br>
2 upload
1. allTitle.html
Add button in file: add topic
<button style="height:30px;width:150px"><a href="/test/addTitle.jsp">Add topic</a></button>
2.addTitle.jsp
Create a file in the href value of the new button: addtitle JSP, download the layui template about file upload
url: UploadServlet, the interface for uploading files
URL in ajax: AddTitleServlet
3. AddTitleServlet
Call the newly created teacherDao in the dao layer
4. teacherDao
public int addTitle(Title title) throws SQLException { DBUtil dbUtil = new DBUtil(); String sql="insert into titleinfo(type,name,teacherId,src) values(?,?,?,?)"; PreparedStatement ps=(PreparedStatement)dbUtil.getPreparedStatement(sql); ps.setString(1,title.getType()); ps.setString(2, title.getName()); ps.setString(3, title.getTeacherId()); ps.setString(4, title.getSrc()); System.out.println(title.getType()+"------"); int rs=ps.executeUpdate(); if(rs==1) { //Manual submission dbUtil.commit(); return 1; }else { dbUtil.connectionRollback(); return 0; } }
Insert the submitted information and return layer by layer
5, The teacher graded the students
1. Click student scoring to display only the students belonging to the teacher
Click student scoring - > mystudent JSP - > GetGradeServlet - > teacherdao - > return layer by layer with paging
1. Two new methods are added in teacherdao: screening the students belonging to the teacher and calculating the number of students of the teacher
public List<Object> selectMyStudentList(String page, String limit,String teacherId) throws SQLException { // TODO Auto-generated method stub DBUtil dbUtil = new DBUtil(); String sql="select * from studentinfo where teacherId=? limit ?,?"; PreparedStatement ps=(PreparedStatement)dbUtil.getPreparedStatement(sql); int page1=Integer.parseInt(page); int limit1=Integer.parseInt(limit); ps.setString(1,teacherId); ps.setInt(2, (page1-1)*limit1); ps.setInt(3, limit1); ResultSet rs=ps.executeQuery(); List<Object> list=new ArrayList<Object>(); while(rs.next()) { Student stu = new Student(); stu.setStudentId(rs.getString("studentId")); stu.setName(rs.getString("name")); stu.setSex(rs.getString("sex")); stu.setAge(rs.getInt("age")); stu.setTeacherId(rs.getString("teacherId")); stu.setTitleId(rs.getInt("titleId")); stu.setGrade(rs.getString("grade")); list.add(stu); } return list; } @Override public int countMyStudent(String teacherId) throws SQLException { DBUtil dbUtil = new DBUtil(); String sql="select count(*) as sum from studentinfo where teacherId=?"; PreparedStatement ps=(PreparedStatement)dbUtil.getPreparedStatement(sql); ps.setString(1, teacherId); //Execute the sql statement to get the result set rs ResultSet rs=ps.executeQuery(); while(rs.next()){ return rs.getInt("sum"); } return 0; }
2. GetGradeServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //doGet(request, response); String teacherId=request.getParameter("teacherId"); String pageStr=request.getParameter("page"); String limitStr=request.getParameter("limit"); try { List<Object> list=teacherDao.selectMyStudentList(pageStr, limitStr, teacherId); response.setContentType("text/html;charset=utf-8"); // Transfer json data Vo vo = new Vo(); vo.setCode(0); vo.setMsg("success"); // What you need here is the sum of the number in the database vo.setCount(teacherDao.countMyStudent(teacherId)); vo.setData(list); // Convert the json type to a string and return it to the front end response.getWriter().write(JSONObject.toJSONString(vo)); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2. Edit results for students
Click student scoring - > mystudent When editing cells in JSP - > EditGradeServlet - > teacherdao - > return layer by layer, with paging
1. A new method is added in teacherdao: edit student grade
public int editGrade(Student student) throws SQLException { DBUtil dbUtil = new DBUtil(); String sql="update studentinfo set grade=? where studentId=?"; PreparedStatement ps=(PreparedStatement)dbUtil.getPreparedStatement(sql); ps.setString(1, student.getGrade()); ps.setString(2, student.getStudentId()); int rs=ps.executeUpdate(); while(rs==1) { dbUtil.commit(); return 1; } return 0; }
2. EditGradeServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String studentId=request.getParameter("studentId"); String grade=request.getParameter("grade"); Student student = new Student(); student.setStudentId(studentId); student.setGrade(grade); try { int a = teacherDao.editGrade(student); if(a==1) { response.getWriter().write("success"); } else { response.getWriter().write("fail"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }