<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
    <form name="formName" action="dologin.jsp">
        用户名:<input type="text" name="uname"><br> 密码:<input
            type="text" name="password"><br>

        <input type="button" value="登录" onclick="toLogin()">
        <input type="button" value="注册" onclick="toReg()">
    </form>
    <script type="text/javascript">
        function toReg() {
            window.location.href = "reg.jsp";
        }
        function toLogin() {
            if(formName.uname.value==""||formName.password.value==""){
                alert("账号或密码不能为空!");
                return;
            }
            formName.submit();
        }
    </script>
    <br>
</body>
</html>
<%@page import="com.dmk.entity.User"%>
<%@page import="com.dmk.dao.UserDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
    <%
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        UserDao userDao = new UserDao();
        User user = userDao.getUserByUname(uname);
        if (password.equals(user.getPassword())) {
            session.setAttribute("uname", uname);
            request.getRequestDispatcher("main.jsp").forward(
                    request, response);
        } else {
    %>
    <script type="text/javascript">
        alert("登录失败");
    </script>
    <%
        request.getRequestDispatcher("login.jsp").forward(request,
                    response);
        }
    %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
    <form name="regForm" action="submit.jsp">
        用户名:<input type="text" name="uname"><br> 密码:<input
            type="password" name="password"><br> 确认密码:<input
            type="password"><br> 年龄:<input type="text" name="age"><br>
    </form>
    <button onclick="reg()">登录</button>
    <script type="text/javascript">
        function reg() {
            regForm.submit();
        }
    </script>
    <br>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
    <%
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        Integer age = Integer.parseInt(request.getParameter("age"));
        User user = new User(uname, password, age);
        UserDao userDao = new UserDao();
        userDao.regUser(user);
        response.sendRedirect("login.jsp");
    %>
</body>
</html>
package com.dmk.entity;

public class User {
    private Integer stuId;
    private String uname;
    private String password;
    private Integer age;

    public User() {
        super();
    }

    public User(String uname, String password, Integer age) {
        super();
        this.uname = uname;
        this.password = password;
        this.age = age;
    }

    public Integer getStuId() {
        return stuId;
    }

    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}
package com.dmk.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbUtil {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Connection getCon() {
        Connection con = null;
        try {
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "123456");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }
}

 

posted @ 2022-05-08 17:49  董梦坤  阅读(66)  评论(0编辑  收藏  举报