1.登录页面
userLogin.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>登录界面
2、注册页面
userRegister.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>注册页面
3、Servlet层
UserLoginServlet.java
获取前端的信息
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 //获取前端的信息 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", "登录失败,账号或密码错误"); 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 { //处理注册页面的请求 req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); Mapmap = 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("注册成功"); req.setAttribute("msg", "注册成功!"); req.getRequestDispatcher("/userLogin.jsp").forward(req, resp); } else { req.setAttribute("msg", "注册失败!"); req.getRequestDispatcher("userRegister.jsp").forward(req, resp); } } catch (SQLException e) { req.setAttribute("msg", "注册失败,账号已存在"); req.getRequestDispatcher("userRegister.jsp").forward(req, resp); } catch (NullPointerException e) { System.out.println("注册失败"); req.setAttribute("msg", "注册失败!信息必须填写完整"); 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.连接数据库
参考我的博客“jdbc连接数据库的文章”
5、实体类
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.创建数据库
7.效果图
8、体会
以上都是参考网上的视频教学来做,一边做一边学,只展示了一部分功能,其他页面没有展示出来,仅供大家参考。
还没有评论,来说两句吧...