作业

<form action="dologin.jsp" method="post">
 用户名:<input type="text" name="username"/>
    <br>
    密码:<input type="password"  name="password"/>
    <br>
  <input type="submit" onclick="changeActionToLogin()" value="登录">
  <input type="submit" onclick="changeActionToRegister()" value="注册">
  </form>
<%
    Stu s = new Stu();
    int id = Integer.parseInt(request.getParameter("sid"));
    s.setSid(id);
    String uname = request.getParameter("uname");
    s.setUname(uname);
    String upwd = request.getParameter("upwd");
    s.setUpwd(upwd);
     
    StuDao sd=new StuDao();
    if(sd.addStu(s)>0){
        //跳转注册成功页面
    }else{
        //错误页面
    }
     %>


<%
    UserBean userBean = new UserBean();
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    userBean.setUname(username);
    userBean.setUpassword(password);
    Dao dao = new Dao();
    int i = dao.UesrRegister(userBean);
    if(i > 0){
        HttpSession session1 = request.getSession();
        session1.setAttribute("username",username);
        session1.setAttribute("password",password);
        request.getRequestDispatcher("ok.jsp").forward(request,response);
    }else{
        response.sendRedirect("error.jsp");
    }
%>
public int addUser(user u) throws Exception {
        int i = 0;
          Class.forName("com.mysql.jdbc.Driver");
            // 2.提供另外三个连接的基本信息
            String url = "jdbc:mysql://localhost:3306/school";
            String user = "root";
            String password = "root";
            Connection conn = DriverManager.getConnection(url, user, password);
            PreparedStatement ps = conn.prepareStatement("insert into user (uname,upwd)values(?,?)");
            ps.setString(1, u.getUname());
            ps.setString(2, u.getUpwd());
            return i = ps.executeUpdate();
             }
public class Login extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String uusername = req.getParameter("username").trim();
        String ppassword = req.getParameter("password").trim();
        int code1Int= 0;
        int code2Int= 0;
        int yzmInt= 0;
        if (code1Int+code2Int==yzmInt) {
            try {
                String yzm = req.getParameter("verify").trim();
                String code1 = req.getParameter("code1").trim();
                String code2 = req.getParameter("code2").trim();
                code1Int = Integer.parseInt(code1);
                code2Int = Integer.parseInt(code2);
                yzmInt = Integer.parseInt(yzm);
                UserDao userDao = new UserDao();
                boolean b = userDao.QueryUser(uusername, ppassword);
                if (code1Int + code2Int == yzmInt) {
                    if (b == true) {
                        HttpSession session = req.getSession();
                        session.setAttribute("uusername", uusername);
                        req.getRequestDispatcher("index.jsp").forward(req, resp);
                    } else {
                        resp.getWriter().println("<h1>登陆失败<h1>");
                        resp.getWriter().println("<h1>用户名或密码输入错误<h1>");
                        resp.getWriter().println("<h1>5s后页面跳转....</h1>");
                        resp.getWriter().println("<a href=\"login.jsp\">返回至登陆界面</a>");
                        resp.setHeader("Refresh", "5;url=login.jsp");
                    }
                } else {
                    resp.getWriter().println("<h1>验证码输入错误<h1>");
                    resp.getWriter().println("<h1>3s后页面跳转....</h1>");
                    resp.getWriter().println("<a href=\"login.jsp\">返回至登陆界面</a>");
                    resp.setHeader("Refresh", "3;url=login.jsp");
                }
            } catch (NumberFormatException e) {
                resp.getWriter().println("<h1>验证码为空,千万不可以为空哦<h1>");
                resp.getWriter().println("<h1>3s后页面跳转....</h1>");
                resp.getWriter().println("<a href=\"login.jsp\">返回至登陆界面</a>");
                resp.setHeader("Refresh","3;url=login.jsp");
            }
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    public void success(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}

 

posted @ 2021-04-19 17:45  好学生!  阅读(57)  评论(0编辑  收藏  举报