1. Login page
userLogin.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Login interface</title> <style> html{ height :100%; background: linear-gradient(to bottom, #9da2bb, #0ee1bd); } .text1{ text-align: center; font-size: 28px; margin-left: 70px; font-weight: bold; letter-spacing: 20px; } .form { width: 400px; height: 300px; border-radius: 20px; margin: 50px auto; padding: 25px; text-align: center; font-family: "Microsoft Yahei"; box-shadow: 0px 0px 13px #8c8c8c; background: rgba(241, 242, 246, 0.4); display: flex; } input { border-radius: 10px; width: 180px; height: 30px; margin-bottom: 5px; border: 1px solid #9b9b9b; outline: none;} input:hover { box-shadow: 0px 0px 5px #020d18; } .input1{ padding-left: 10px; } .input2 { margin-top: 20px; padding-left: 10px; } .submit { background: #7e8acb; margin-top: 20px; width: 80px; height: 21px; font-size: 12px; } .submit:hover,.reg:hover { background-color: #b3d57a; box-shadow: 0px 0px 5px #1c8fff; } .box{ display: flex; flex-direction:row; justify-content: space-around; } .reg{ margin-top: 19px; margin-left: -150px; border-radius: 10px; border: 1px solid #9b9b9b; outline: none; background: #7e8acb; width: 80px; height: 20px; line-height: 21px; font-size: 13px; } .reg a{ display:block; width: 100%; height: 100%; text-decoration: none; color: #000000; } .msg{ display:inline-block; color: #d90d24; height: 30px; line-height: 40px; font-weight: bold; } </style> </head> <body> <form class="form" action="UserLoginServlet" method="post"> <div> <p class="text1">Student Management System</p><br> Account number:<input class="input1" type="text" name="userName" /><br> Password:<input class="input2" type="password" name="userPassword" /><br> <div class="box"> <div> <input class="submit" type="submit" value="Login" /> </div> <div class="reg"> <a href="userRegister.jsp">Register</a> </div> </div> <span class="msg">${msg}</span> </div> </form> <script type="text/javascript"> </script> </body> </html>
2. Registration page
userRegister.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Registration page</title> <style> <%--All elements on the page are styled --%> *{ margin: 0; padding: 0; outline: none; box-sizing: border-box; } html{ height :100%; background: linear-gradient(to bottom,#acb6e5,#86fde8); display: flex; justify-content: center; align-items:center; } p{ text-align: center; font-size: 25px; margin-bottom: 20px; font-weight: bold; letter-spacing: 10px; } form{ width:420px; font-size: 15px; color: #434343; font-family: "Microsoft Yahei"; font-weight: 400; margin-left: 30px; background: rgba(230,255,251,0.4); padding: 10px 100px; border-radius: 30px; box-shadow: 0 0 10px #d9d9d9; } label{ height: 35px; line-height: 30px; display: block; } span{ display: inline-block; width: 20px; text-align: center; } input,select{ height: 20px; width: 150px; border-radius: 10px; padding-left: 10px; border: 1px solid #ffffff; color: #434343; } <%--Set the style when the mouse hovers over the input box (input) or select box (select)--%> input:hover,select:hover{ border: 1px; box-shadow: 0 0 5px #021221; } input[type="radio"]{ width: 20px; height:13px; vertical-align: center; margin-left: 10px; } .submit{ border: 1px solid #adb6bd; background: #4d4e8f; text-align: center; width: 100px; height: 28px; margin-left: 50px; letter-spacing: 10px; color: #ffffff; } h5{ line-height: 30px; color: #f5222d; text-align: center; } </style> </head> <body> <form class=".form" action="RegisterServlet"method="post"> <p>registration page</p> <label> Account Account:<input type ="text" name="userName"> </label> <label> Password:<input type ="password" name="userPassword"> </label> <label> Learn No.:<input type="text" name="sno"> </label> <label> Last name First name:<input type="text" name="snoName"> </label> <label> Sex Gender: Male:<input type="radio" name="sex"value="Male"> Female:<input type="radio" name ="sex" value="Female"> </label> <label> Year Age:<input type="text" name="age"> </label> <label> Occupation Occupation: Students Students:<input type="radio" name="userId" value="1"> Old teacher:<input type="radio" name="userId" value="2"> </label> <label> School College:<input type="text" name="userClass"> </label> <h5>${msg}</h5> <label> <input class="submit" type="submit" value="Register"> </label> </form> </body> </html>
3. Servlet layer
UserLoginServlet.java
Get front-end information
package servlet1; import bean1.DBUtil; import bean1.User; 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.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @WebServlet("/UserLoginServlet") public class UserLoginServlet extends HttpServlet { @Override //Get front-end information protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = DBUtil.getConnection(); String sql = "select*from user where userName=? and userPassword=? "; ps = conn.prepareStatement(sql); String userName = req.getParameter("userName"); String userPassword = req.getParameter("userPassword"); ps.setString(1, userName); ps.setString(2, userPassword); rs = ps.executeQuery(); User user = new User(); if (rs.next()) { do { user.setUserName(rs.getString("userName")); user.setUserPassword(rs.getString("userPassword")); user.setSnoName(rs.getString("snoName")); user.setSex(rs.getString("sex")); user.setAge(rs.getInt("age")); user.setSno(rs.getInt("sno")); user.setUserClass(rs.getString("userClass")); user.setUserId(rs.getInt("userId")); } while (rs.next()); req.getSession().setAttribute("user",user); req.setAttribute("mainR","blank.jsp"); req.getRequestDispatcher("/main.jsp").forward(req, resp); } else { req.setAttribute("msg", "Login failed, wrong account or password"); req.getRequestDispatcher("userLogin.jsp").forward(req, resp); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs,ps,conn); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
UserRegister.java
package servlet1; import bean1.DBUtil; 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.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Map; @WebServlet("/RegisterServlet") public class RegisterServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Handle the request for the registration page req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); Map<String, String[]> map = req.getParameterMap(); PreparedStatement ps = null; Connection conn = null; try { conn = DBUtil.getConnection(); String sql = "insert into user(userName,userPassword,snoName,sex,age,sno,userClass,userId) values(?,?,?,?,?,?,?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, map.get("userName")[0]); ps.setString(2, map.get("userPassword")[0]); ps.setString(3, map.get("snoName")[0]); ps.setString(4, map.get("sex")[0]); ps.setInt(5, Integer.valueOf(map.get("age")[0])); ps.setString(6, map.get("sno")[0]); ps.setString(7, map.get("userClass")[0]); ps.setInt(8, Integer.valueOf(map.get("userId")[0])); int count = ps.executeUpdate(); if (count > 0) { System.out.println("Registration successful"); req.setAttribute("msg", "Registration successful!"); req.getRequestDispatcher("/userLogin.jsp").forward(req, resp); } else { req.setAttribute("msg", "Registration failed!"); req.getRequestDispatcher("userRegister.jsp").forward(req, resp); } } catch (SQLException e) { req.setAttribute("msg", "Registration failed, account already exists"); req.getRequestDispatcher("userRegister.jsp").forward(req, resp); } catch (NullPointerException e) { System.out.println("Registration failed"); req.setAttribute("msg", "Registration failed! Information must be filled in completely"); req.getRequestDispatcher("userRegister.jsp").forward(req, resp); } finally { DBUtil.close(null,ps,conn); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
4. Connect to database
Refer to my blog “Article on jdbc connection to database”
5. Entity class
user.java
package bean1; public class User { private String userName; private String userPassword; private String snoName; private String sex; private int age; private int sno; private String userClass; private int userId; public String getUserName(){ return userName; } public void setUserName(String userName){ this.userName=userName; } public String getUserPassword(){ return userPassword; } public void setUserPassword(String userPassword){ this.userPassword=userPassword; } public String getSnoName(){ return snoName; } public void setSnoName(String snoName){ this.snoName= snoName; } public String getSex(){ return sex; } public void setSex(String sex){ this.sex= sex; } public int getAge(){ return age; } public void setAge(int age){ this.age=age; } public int getSno(){ return sno; } public void setSno(int sno){ this.sno=sno; } public String getUserClass(){ return userClass; } public void setUserClass(String userClass){ this.userClass=userClass; } public int getUserId(){return userId;} public void setUserId(int userId){this.userId=userId;} @Override public String toString(){ return "userName:" + userName + "userPassword:" + userPassword + "snoName:" + snoName + "sex" + sex + "age" + age + "sno" + sno + "userClass" + userClass + "userId" + userId; } }
6. Create database
7. Renderings
8. Experience
The above are all done with reference to online video tutorials. You can learn while doing it. Only part of the functions are shown, and other pages are not shown. They are for your reference only.