The full code has been uploaded to Github: https://github.com/Shadow1300/JavaEE
It will also be listed at the end of the article.
1, First of all, understand the operation requirements
We first need to analyze the requirements:
1) Users: teachers and students
2) Functions: add homework, add students, query student homework, submit homework
3) Function expansion and improvement:
① Login: because of the function of adding students, I think there should be a login function. Students who are not authorized cannot enter the submission interface, or the function of adding students will lose its meaning;
② Query homework requirements: because students need to submit homework, there should be a way to view homework requirements;
③ View students: since teachers can add students, you should also be able to view the student table to know which students have been added.
2, Table building analysis
It is easy to analyze that this system needs three tables:
1. Student table;
① Student id (primary attribute)
② Student name
③ Auto increment id sequence number (main attribute)
2. Operation requirements form;
① Auto increment id sequence number (main attribute)
② Job No. (primary attribute)
③ Job requirements
④ Job deadline
3. Submit the operation form;
① Auto increment id sequence number (main attribute)
② Student i ((primary attribute) (foreign key refers to student's ID in student table)
③ Job ID (primary attribute) (foreign key refers to homework'id in homework table)
④ Job title
⑤ Job content
⑥ Submission time
⑦ Deadline of homework (this attribute is for teachers to refer to whether students are late or not, and points are deducted for reference)
After analyzing the requirements and designing the tables, let's not worry about writing the code, but design the whole project as a whole. In addition, you should also know how the web works in Java EE and how the front and back end pass parameters. In this part, I spent more than half of my time to understand. If I didn't lay a good foundation, I couldn't understand the code.
3, System design and basic knowledge mastery
1. System design
The design of this system should be composed of four parts:
① Entity class: get and set for entity property value
② JDBC * *: specially used to interact with database information
③ Servlet: used for front and back end information interaction
④ Jsp: front end interface design (I learned that although. JSP can also write logic and write data processing, I personally think it is better to separate the front and back ends, which is also the main way of project cooperation now, so in this project. jps file is only used as the front-end design)
2. Basic knowledge mastery
First, understand the operation mode:
1. When you want to query: you should request data from the front-end to the back-end database, the back-end sends data to the front-end, and the front-end receives the data and displays it in the page. One way to show data clearly in this scenario is to use tables.
2. Publish information and add information: the front end should actively send the information filled in the page to the back end, and the back end will process the data after receiving the front end information, so as to add, delete and modify the database.
3. Fault tolerance hint: because the front end does not know the database situation of the back end, it may pass in error data to the back end, which is that the back end should conduct data analysis and prepare for fault tolerance.
And Servlet and jsp files are the most important part of this communication!
3. What is Servlet?
Java Servlet is a program running on the Web server or application server. It is the middle layer between the request from the Web browser or other HTTP clients and the database or application program on the HTTP server. Servlet can collect user input from Web forms, present records from databases or other sources, and create Web pages dynamically.
Its structure is:
Servlet s perform the following main tasks:
1. Read the explicit data sent by the client (browser). This includes HTML forms on Web pages, or forms from applet s or custom HTTP clients.
2. Read the implicit HTTP request data sent by the client (browser). This includes cookies, media types, and compressed formats that browsers can understand, and so on.
3. Process data and generate results. This process may need to access the database, perform RMI or CORBA calls, call Web services, or directly calculate the corresponding response.
4. Send explicit data (that is, documents) to the client (browser). The format of this document can be various, including text file (HTML or XML), binary file (GIF image), Excel, etc.
5. Sends an implicit HTTP response to the client (browser). This includes telling a browser or other client what type of document (such as HTML) is returned, setting cookies and cache parameters, and other similar tasks.
4. What is jsp?
Its full name is: Java Server Pages, which is a dynamic web page development technology. It uses JSP tags to insert java code into HTML pages. Labels usually begin with <% and end with% >.
JSP is a Java servlet, which is mainly used to implement the user interface of Java web applications. Web developers write JSPS by combining HTML code, XHTML code, XML elements, and embedded JSP operations and commands.
JSP obtains user input data through web forms, accesses database and other data sources, and then creates web pages dynamically.
JSP tag has many functions, such as accessing database, recording user selection information, accessing JavaBeans components, etc. it can also transfer control information and share information in different web pages.
4, Start writing code
1. First, build three tables in the database, and pay attention to the constraint relationship
2. Entity class
Such as Student:
package com.java.code.model; public class Student { private int id; private String student_id; private String student_name; public String getStudent_name() { return student_name; } public void setStudent_name(String student_name) { this.student_name = student_name; } public String getStudent_id() { return student_id; } public void setStudent_id(String student_id) { this.student_id = student_id; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
3. JDBC: perform various operations on the database
How to add students
//Add student public static void addStudent(Student student) throws SQLException, ClassNotFoundException{ String url="jdbc:mysql://localhost:3306/zy?useSSL=false&allowPublicKeyRetrieval=true"; String driverName="com.mysql.jdbc.Driver"; //Database execution statement String sqlString="insert into zy.student(student_id,student_name) values (\""+student.getStudent_id()+"\",\""+student.getStudent_name()+"\")"; //Load driver Class.forName(driverName); //Create links Connection connection= DriverManager.getConnection(url,"root","zhangying"); //Get statement through link Statement statement=connection.createStatement(); //Add, delete, modify and check statement int resultSet=statement.executeUpdate(sqlString); }
4. Servlet of query: that is to call the query method in JDBC, assign the value to the entity class, pass the entity class to the front end, and then jump to the page displaying the query results
try { //Call query method List<Student> list= StudentHomeworkJdbc.selectStudent(); //Send the result to the attribute, and the front end gets the value of the entity class according to the attribute name req.setAttribute("list",list); //Pass the value to the interface and jump to the. jsp interface req.getRequestDispatcher("selectStudent.jsp").forward(req,resp); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }
5. Front end receiving: the entity class is received first, and the values are extracted and put into the table by using the table method, so as to achieve the query purpose
<body> <% List<Student> list=(List <Student>)request.getAttribute("list"); if(null==list||list.size()<=0){ out.println("No data"); }else { %> <table align="center" width="960" border="0.5" bgcolor="black" cellspacing="1"> <tr align="center" height="40" bgcolor="#b3ffc2"> <td>Serial number</td> <td>Student ID</td> <td>Student name</td> </tr> <% for(Student student:list){ %> <tr align="center" bgcolor="white"> <td><%=student.getId()%></td> <td><%=student.getStudent_id()%></td> <td><%=student.getStudent_name()%></td> </tr> <% } } %> </table> </body>
6. Servlet added to the database: first, after filling out the form in the front interface, submit the data to the back end, so as to add the database.
First, a form is designed at the front end: note that action represents the Servlet to which the data will be transferred after submission
<body> <form action="addhomework" id="form"> <h2>Publishing operation</h2> //Job ID:<input type="text" name="homework_id"><br> //Operational requirements:<input type="text" name="homework_requirement"><br> //Job due date:<input type="datetime-local" name="homework_endtime"><br> <input type="submit" id="submit" value="Submission"><input type="reset" value="Reset"> </form> </body>
7. Back end receiving: the servlet corresponding to the front end needs @ WebServlet operation. After naming, the front and back ends can find each other.
And receive data by
Homework homework=new Homework(); homework.setHomework_id(req.getParameter("homework_id")); homework.setHomework_requirement(req.getParameter("homework_requirement"));
Then call JDBC's corresponding operation function to determine whether the data is added successfully or not, and then jump to the fault tolerant page.
//Begin to transmit try { StudentHomeworkJdbc.addHomework(homework); resp.sendRedirect("addHomeworkSuccess.jsp"); } catch (SQLException e) { resp.sendRedirect("addHomeworkFault.jsp"); e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }
8. Fault tolerance page: no need to be too complex
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Failed to publish job</title> </head> <body> <% out.println("The job No. already exists and cannot be published repeatedly"); %> </body> </html>
9. For convenience, I wrote a separate directory interface
<table border="1"> <tr> <td>Please select an action item</td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/addStudent.jsp" target="_blank">Add student</a></td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/selectstudent" target="_blank">Checking students</a></td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/addHomework.jsp" target="_blank">Publishing operation</a></td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/selectsubmit" target="_blank">View job submission</a></td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/selecthomework" target="_blank">View job requirements</a></td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/submitHomework.jsp" target="_blank">Submit homework</a></td> </tr> </table>
5, Partial rendering
(I'm not familiar with the layout of the interface, so it's a bit ugly.)
6, Full code
The full code has been uploaded to Github: https://github.com/Shadow1300/JavaEE
1,StudentHomeworkJdbc.java
package com.java.code.jdbc; import com.java.code.model.Homework; import com.java.code.model.Student; import com.java.code.model.SubmitHomework; import java.sql.*; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; public class StudentHomeworkJdbc { //Check whether the account name corresponds to public static String selectStudent(Student student) throws SQLException, ClassNotFoundException { String url="jdbc:mysql://localhost:3306/zy?useSSL=false&allowPublicKeyRetrieval=true"; String driverName="com.mysql.jdbc.Driver"; //Database execution statement String sqlString="select * from student"+" where student_id =\""+student.getStudent_id()+"\" and student_name=\""+student.getStudent_name()+"\""; System.out.println("aaa:"+sqlString); //Load driver Class.forName(driverName); List<Student> list=new ArrayList<>(); //Create links Connection connection= DriverManager.getConnection(url,"root","zhangying"); //Get statement through link Statement statement=connection.createStatement(); //Add, delete, modify and check statement ResultSet resultSet=statement.executeQuery(sqlString); //Get execution results if(resultSet.next()){ //Access page String check="success"; System.out.println(1111); return check; } else { String check="wrong"; System.out.println("2222"); return check; } } //Add student public static void addStudent(Student student) throws SQLException, ClassNotFoundException{ String url="jdbc:mysql://localhost:3306/zy?useSSL=false&allowPublicKeyRetrieval=true"; String driverName="com.mysql.jdbc.Driver"; //Database execution statement String sqlString="insert into zy.student(student_id,student_name) values (\""+student.getStudent_id()+"\",\""+student.getStudent_name()+"\")"; //Load driver Class.forName(driverName); //Create links Connection connection= DriverManager.getConnection(url,"root","zhangying"); //Get statement through link Statement statement=connection.createStatement(); //Add, delete, modify and check statement int resultSet=statement.executeUpdate(sqlString); } //Publishing operation public static void addHomework(Homework homework) throws SQLException, ClassNotFoundException{ String url="jdbc:mysql://localhost:3306/zy?useSSL=false&allowPublicKeyRetrieval=true"; String driverName="com.mysql.jdbc.Driver"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//Format date String endtime=df.format(homework.getHomework_endtime()); //Database execution statement String sqlString="insert into zy.homework(homework_id,homework_requirement,homework_endtime) values (\""+homework.getHomework_id()+"\",\""+homework.getHomework_requirement()+"\",\""+endtime+"\")"; System.out.println(sqlString); //Load driver Class.forName(driverName); //Create links Connection connection= DriverManager.getConnection(url,"root","zhangying"); //Get statement through link Statement statement=connection.createStatement(); //Add, delete, modify and check statement int resultSet=statement.executeUpdate(sqlString); } //Submit homework public static void submitHomework(SubmitHomework submitHomework) throws SQLException, ClassNotFoundException{ String url="jdbc:mysql://localhost:3306/zy?useSSL=false&allowPublicKeyRetrieval=true"; String driverName="com.mysql.jdbc.Driver"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//Format date String submit_time=df.format(submitHomework.getSubmit_time()); //Database execution statement String sqlString="insert into zy.submit_homework(student_id,homework_id,homework_title,homework_content,submit_time,homework_endtime) values (\""+submitHomework.getStudent_id()+"\",\""+submitHomework.getHomework_id()+"\",\""+submitHomework.getHomework_title()+"\",\""+submitHomework.getHomework_content()+"\",\""+submit_time+"\",\""+StudentHomeworkJdbc.selectEndtime(submitHomework.getHomework_id())+"\")"; System.out.println(sqlString); //Load driver Class.forName(driverName); //Create links Connection connection= DriverManager.getConnection(url,"root","zhangying"); //Get statement through link Statement statement=connection.createStatement(); //Add, delete, modify and check statement int resultSet=statement.executeUpdate(sqlString); } //Query deadline public static String selectEndtime(String homework_id) throws SQLException, ClassNotFoundException{ String url="jdbc:mysql://localhost:3306/zy?useSSL=false&allowPublicKeyRetrieval=true"; String driverName="com.mysql.jdbc.Driver"; //Database execution statement String sqlString="select homework_endtime from zy.homework where homework_id=\""+homework_id+"\""; //Load driver Class.forName(driverName); //Create links Connection connection= DriverManager.getConnection(url,"root","zhangying"); //Get statement through link Statement statement=connection.createStatement(); //Add, delete, modify and check statement ResultSet resultSet=statement.executeQuery(sqlString); String out=""; //Get execution results while(resultSet.next()){ out=resultSet.getString("homework_endtime"); } return out; } //Query job submission public static List<SubmitHomework> selectSubmit() throws SQLException, ClassNotFoundException { String url="jdbc:mysql://localhost:3306/zy?useSSL=false&allowPublicKeyRetrieval=true"; String driverName="com.mysql.jdbc.Driver"; //Database execution statement String sqlString="select * from submit_homework"; //Load driver Class.forName(driverName); List<SubmitHomework> list=new ArrayList<>(); //Create links Connection connection= DriverManager.getConnection(url,"root","zhangying"); //Get statement through link Statement statement=connection.createStatement(); //Add, delete, modify and check statement ResultSet resultSet=statement.executeQuery(sqlString); //Get execution results while(resultSet.next()){ SubmitHomework submitHomework=new SubmitHomework(); submitHomework.setId(resultSet.getInt("id")); submitHomework.setStudent_id(resultSet.getString("student_id")); submitHomework.setHomework_id(resultSet.getString("homework_id")); submitHomework.setHomework_title(resultSet.getString("homework_title")); submitHomework.setHomework_content(resultSet.getString("homework_content")); submitHomework.setSubmit_time(resultSet.getTimestamp("submit_time")); submitHomework.setHomework_endtime(resultSet.getTimestamp("homework_endtime")); list.add(submitHomework); } return list; } //Checking students public static List<Student> selectStudent() throws SQLException, ClassNotFoundException { String url="jdbc:mysql://localhost:3306/zy?useSSL=false&allowPublicKeyRetrieval=true"; String driverName="com.mysql.jdbc.Driver"; //Database execution statement String sqlString="select * from student"; //Load driver Class.forName(driverName); List<Student> list=new ArrayList<>(); //Create links Connection connection= DriverManager.getConnection(url,"root","zhangying"); //Get statement through link Statement statement=connection.createStatement(); //Add, delete, modify and check statement ResultSet resultSet=statement.executeQuery(sqlString); //Get execution results while(resultSet.next()){ Student student=new Student(); student.setId(resultSet.getInt("id")); student.setStudent_id(resultSet.getString("student_id")); student.setStudent_name(resultSet.getString("student_name")); list.add(student); } return list; } //View job requirements public static List<Homework> selectHomework() throws SQLException, ClassNotFoundException { String url="jdbc:mysql://localhost:3306/zy?useSSL=false&allowPublicKeyRetrieval=true"; String driverName="com.mysql.jdbc.Driver"; //Database execution statement String sqlString="select * from Homework"; //Load driver Class.forName(driverName); List<Homework> list=new ArrayList<>(); //Create links Connection connection= DriverManager.getConnection(url,"root","zhangying"); //Get statement through link Statement statement=connection.createStatement(); //Add, delete, modify and check statement ResultSet resultSet=statement.executeQuery(sqlString); //Get execution results while(resultSet.next()){ Homework homework=new Homework(); homework.setId(resultSet.getInt("id")); homework.setHomework_id(resultSet.getString("homework_id")); homework.setHomework_requirement(resultSet.getString("homework_requirement")); homework.setHomework_endtime(resultSet.getTimestamp("homework_endtime")); list.add(homework); } return list; } }
2. Homework.java entity class
package com.java.code.model; import java.util.Date; public class Homework { private int id; private String homework_id; private String homework_requirement; private Date homework_endtime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getHomework_id() { return homework_id; } public void setHomework_id(String homework_id) { this.homework_id = homework_id; } public String getHomework_requirement() { return homework_requirement; } public void setHomework_requirement(String homework_requirement) { this.homework_requirement = homework_requirement; } public Date getHomework_endtime() { return homework_endtime; } public void setHomework_endtime(Date homework_endtime) { this.homework_endtime = homework_endtime; } }
3. Student.java entity class
package com.java.code.model; public class Student { private int id; private String student_id; private String student_name; public String getStudent_name() { return student_name; } public void setStudent_name(String student_name) { this.student_name = student_name; } public String getStudent_id() { return student_id; } public void setStudent_id(String student_id) { this.student_id = student_id; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
4. SubmitHomework.java entity class
package com.java.code.model; import java.util.Date; public class SubmitHomework { private int id; private String student_id; private String homework_id; private String homework_title; private String homework_content; private Date submit_time; private Date homework_endtime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStudent_id() { return student_id; } public void setStudent_id(String student_id) { this.student_id = student_id; } public String getHomework_id() { return homework_id; } public void setHomework_id(String homework_id) { this.homework_id = homework_id; } public String getHomework_title() { return homework_title; } public void setHomework_title(String homework_title) { this.homework_title = homework_title; } public String getHomework_content() { return homework_content; } public void setHomework_content(String homework_content) { this.homework_content = homework_content; } public Date getSubmit_time() { return submit_time; } public void setSubmit_time(Date submit_time) { this.submit_time = submit_time; } public Date getHomework_endtime() { return homework_endtime; } public void setHomework_endtime(Date homework_endtime) { this.homework_endtime = homework_endtime; } }
5,AddHomeworkServlet.java
package com.java.code.servlet; import com.java.code.jdbc.StudentHomeworkJdbc; import com.java.code.model.Homework; import com.java.code.model.Student; 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; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @WebServlet("/addhomework") public class AddHomeworkServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8");//Set uniform character encoding Homework homework=new Homework(); homework.setHomework_id(req.getParameter("homework_id")); homework.setHomework_requirement(req.getParameter("homework_requirement")); //Can not be empty if(req.getParameter("homework_id").equals("")||req.getParameter("homework_requirement").equals("")||req.getParameter("homework_endtime").equals("")){ resp.sendRedirect("check.jsp"); } else { //Change String to date String nowtime=req.getParameter("homework_endtime")+":00"; nowtime=nowtime.substring(0,10)+" "+nowtime.substring(11); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d2 = null; try { d2 = sdf2.parse(nowtime); } catch (ParseException e) { e.printStackTrace(); } homework.setHomework_endtime(d2); //Begin to transmit try { StudentHomeworkJdbc.addHomework(homework); resp.sendRedirect("addHomeworkSuccess.jsp"); } catch (SQLException e) { resp.sendRedirect("addHomeworkFault.jsp"); e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } }
6,AddStudentServlet.java
package com.java.code.servlet; import com.java.code.jdbc.StudentHomeworkJdbc; import com.java.code.model.Student; 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; import java.sql.SQLException; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; @WebServlet("/addStudentServlet") public class AddStudentServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8");//Set uniform character encoding Student student=new Student(); student.setStudent_id(req.getParameter("student_id")); student.setStudent_name(req.getParameter("student_name")); //Can not be empty if(req.getParameter("student_id").equals("")||req.getParameter("student_name").equals("")){ resp.sendRedirect("check.jsp"); } else { try { StudentHomeworkJdbc.addStudent(student); resp.sendRedirect("AddStudentSuccess.jsp"); } catch (SQLException e) { resp.sendRedirect("AddStudentFault.jsp"); e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } }
7,LoadServlet.java
package com.java.code.servlet; import com.java.code.jdbc.StudentHomeworkJdbc; import com.java.code.model.Student; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLException; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; public class LoadServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8");//Set uniform character encoding Student student=new Student(); student.setStudent_id(req.getParameter("student_id")); student.setStudent_name(req.getParameter("student_name")); //Can not be empty if(req.getParameter("student_id").equals("")||req.getParameter("student_name").equals("")){ resp.sendRedirect("check.jsp"); } else{ try { String answer= StudentHomeworkJdbc.selectStudent(student); if(answer.equals("success")){ resp.sendRedirect("Main.jsp"); } else { resp.sendRedirect("checkwrong.jsp"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } } }
8,SelectHomework.java
package com.java.code.servlet; import com.java.code.jdbc.StudentHomeworkJdbc; import com.java.code.model.Homework; import com.java.code.model.Student; import com.java.code.model.SubmitHomework; 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; import java.sql.SQLException; import java.util.List; @WebServlet("/selecthomework") public class SelectHomework extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { List<Homework> list= StudentHomeworkJdbc.selectHomework(); req.setAttribute("list",list); req.getRequestDispatcher("selectHomework.jsp").forward(req,resp); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
9,SelectStudent.java
package com.java.code.servlet; import com.java.code.jdbc.StudentHomeworkJdbc; import com.java.code.model.Homework; import com.java.code.model.Student; 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; import java.sql.SQLException; import java.util.List; @WebServlet("/selectstudent") public class SelectStudent extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { List<Student> list= StudentHomeworkJdbc.selectStudent(); req.setAttribute("list",list); req.getRequestDispatcher("selectStudent.jsp").forward(req,resp); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
10,SelectSubmitHomeworkServlet.java
package com.java.code.servlet; import com.java.code.jdbc.StudentHomeworkJdbc; import com.java.code.model.SubmitHomework; 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; import java.sql.SQLException; import java.util.List; @WebServlet("/selectsubmit") public class SelectSubmitHomeworkServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { List<SubmitHomework> list= StudentHomeworkJdbc.selectSubmit(); req.setAttribute("list",list); req.getRequestDispatcher("selectSubmit.jsp").forward(req,resp); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
11,SubmitHomeworkServlet.java
package com.java.code.servlet; import com.java.code.jdbc.StudentHomeworkJdbc; import com.java.code.model.SubmitHomework; 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; import java.sql.SQLException; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; @WebServlet("/submit") public class SubmitHomeworkServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8");//Set uniform character encoding SubmitHomework submitHomework=new SubmitHomework(); //Get current time SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//Format date String nowtime=df.format(new Date()); submitHomework.setStudent_id(req.getParameter("student_id")); submitHomework.setHomework_id(req.getParameter("homework_id")); submitHomework.setHomework_title(req.getParameter("homework_title")); submitHomework.setHomework_content(req.getParameter("homework_content")); //Given pattern (the pattern given here must match the format of the given Date String), convert String to Date SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d2 = null; try { d2 = sdf2.parse(nowtime); } catch (ParseException e) { e.printStackTrace(); } //Assignment time submitHomework.setSubmit_time(d2); //Can not be empty if(req.getParameter("student_id").equals("")||req.getParameter("homework_id").equals("")){ resp.sendRedirect("check.jsp"); } else { try { StudentHomeworkJdbc.submitHomework(submitHomework); resp.sendRedirect("submitHomeworkSuccess.jsp"); } catch (SQLException e) { resp.sendRedirect("submitHomeworkFault.jsp"); e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } }
12,web.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"> <servlet> <servlet-name>load</servlet-name> <servlet-class>com.java.code.servlet.LoadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>load</servlet-name> <url-pattern>/load</url-pattern> </servlet-mapping> </web-app>
13,addHomework.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 14:23 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Publish new job</title> </head> <body> <form action="addhomework" id="form"> <h2>Publishing operation</h2> task ID:<input type="text" name="homework_id"><br> Operational requirements:<input type="text" name="homework_requirement"><br> Job due date:<input type="datetime-local" name="homework_endtime"><br> <input type="submit" id="submit" value="Submission" onclick="a()"><input type="reset" value="Reset"> </form> </body> </html>
14,addHomeworkFault.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 19:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Failed to publish job</title> </head> <body> <% out.println("The job No. already exists and cannot be published repeatedly"); %> </body> </html>
15,addHomeworkSuccess.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 19:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Released successfully</title> </head> <body> <% out.println("Job published successfully!"); %> </body> </html>
16,addStudent.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 13:30 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Add student</title> </head> <body> <form action="addStudentServlet" id="form" method="post"> <h2>Add student</h2> Student ID:<input type="text" name="student_id"><br> Student name:<input type="text" name="student_name"><br> <input type="submit" id="submit" value="Submission" onclick="a()"><input type="reset" value="Reset"> </form> </body> </html>
17,AddStudentFault.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 18:07 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Failed to add students</title> </head> <body> <% out.println("The student ID already exists, cannot be added repeatedly"); %> </body> </html>
18,AddStudentSuccess.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 17:57 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Add student success</title> </head> <body> <% out.println("Add student successfully!"); %> </body> </html>
19,check.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/10 Time: 22:39 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>remind</title> </head> <body> <% out.println("Please fill in completely, can't be blank"); %> </body> </html>
20,checksuccess.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 11:33 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login successfully</title> </head> <body> <% out.println("Login successfully"); %> </body> </html>
21,checkwrong.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 11:33 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login failed</title> </head> <body> <% out.println("Login failed because student ID and name do not correspond or the student does not exist"); %> </body> </html>
22,index.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/10 Time: 21:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Student assignment management system login</title> <STYLE type="text/css"> body{ background-repeat: repeat; } body{ background-image: url(images/timg.jpg); } #form{ width:250px; height:160px; position:relative; left:50%; top:50%; margin-left:-150px; margin-top:-80px; } </STYLE> </head> <body> <form action="load" id="form" margin="100px" action="check.jsp" > //Student ID: <input type="text" name="student_id" value="" align="center"><br> //Student name: <input type="text" name="student_name" align="center"><br> <br> <input type="submit" id="submit" value="Sign in" onclick="a()" align="center"> <input type="reset" value="Reset"> <p align="center"></p> </form> </body> </html>
23,Main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table border="1"> <tr> <td>Please select an action item</td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/addStudent.jsp" target="_blank">Add student</a></td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/selectstudent" target="_blank">Checking students</a></td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/addHomework.jsp" target="_blank">Publishing operation</a></td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/selectsubmit" target="_blank">View job submission</a></td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/selecthomework" target="_blank">View job requirements</a></td> </tr> <tr> <td><a href="<%=request.getContextPath() %>/submitHomework.jsp" target="_blank">Submit homework</a></td> </tr> </table> </body> </html>
24,selectHomework.jsp
<%@ page import="com.java.code.model.Homework" %> <%@ page import="java.util.List" %><%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 17:48 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Query job publishing status</title> </head> <body> <% List<Homework> list=(List <Homework>)request.getAttribute("list"); if(null==list||list.size()<=0){ out.println("No data"); }else { %> <table align="center" width="960" border="0.5" bgcolor="black" cellspacing="1"> <tr align="center" height="40" bgcolor="#b3ffc2"> <td>Serial number</td> <td>task ID</td> <td>Operational requirements</td> <td>Job deadline</td> </tr> <% for(Homework homework:list){ %> <tr align="center" bgcolor="white"> <td><%=homework.getId()%></td> <td><%=homework.getHomework_id()%></td> <td><%=homework.getHomework_requirement()%></td> <td><%=homework.getHomework_endtime()%></td> </tr> <% } } %> </table> </body> </html>
25,selectStudent.jsp
<%@ page import="com.java.code.model.Student" %> <%@ page import="java.util.List" %><%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 17:44 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Checking students</title> </head> <body> <% List<Student> list=(List <Student>)request.getAttribute("list"); if(null==list||list.size()<=0){ out.println("No data"); }else { %> <table align="center" width="960" border="0.5" bgcolor="black" cellspacing="1"> <tr align="center" height="40" bgcolor="#b3ffc2"> <td>Serial number</td> <td>Student ID</td> <td>Student name</td> </tr> <% for(Student student:list){ %> <tr align="center" bgcolor="white"> <td><%=student.getId()%></td> <td><%=student.getStudent_id()%></td> <td><%=student.getStudent_name()%></td> </tr> <% } } %> </table> </body> </html>
26,selectSubmit.jsp
<%@ page import="com.java.code.model.SubmitHomework" %> <%@ page import="java.util.List" %><%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 16:05 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>View job submission</title> </head> <body> <% List<SubmitHomework> list=(List <SubmitHomework>)request.getAttribute("list"); if(null==list||list.size()<=0){ out.println("No data"); }else { %> <table align="center" width="960" border="0.5" bgcolor="black" cellspacing="1"> <tr align="center" height="40" bgcolor="#b3ffc2"> <td>Serial number</td> <td>Student ID</td> <td>Job number</td> <td>Job title</td> <td>Job content</td> <td>Submission time</td> <td>Deadline</td> </tr> <% for(SubmitHomework submitHomework:list){ %> <tr align="center" bgcolor="white"> <td><%=submitHomework.getId()%></td> <td><%=submitHomework.getStudent_id()%></td> <td><%=submitHomework.getHomework_id()%></td> <td><%=submitHomework.getHomework_title()%></td> <td><%=submitHomework.getHomework_content()%></td> <td><%=submitHomework.getSubmit_time()%></td> <td><%=submitHomework.getHomework_endtime()%></td> </tr> <% } } %> </table> </body> </html>
27,submitHomework.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 14:50 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Job submission</title> </head> <body> <form action="submit" id="form" method="post"> <h2>Submit homework</h2> Student ID:<input type="text" name="student_id" value="" id="123"><br> task ID:<input type="text" name="homework_id"><br> Job title:<input type="text" name="homework_title"><br> Job content:<input type="text" name="homework_content"><br> <input type="submit" id="submit" value="Submission" onclick="a()"><input type="reset" value="Reset"> </form> </body> </html>
28,submitHomeworkFault.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 20:11 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Job submission failed</title> </head> <body> <% out.println("Job submission failed,Student number does not exist or operation number does not exist"); %> </body> </html>
29,submitHomeworkSuccess.jsp
<%-- Created by IntelliJ IDEA. User: Zhang Ying Date: 2020/3/11 Time: 20:18 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Job submitted successfully</title> </head> <body> <% out.print("Job submitted successfully!"); %> </body> </html>