首页 > 代码库 > JSP:注册&登录

JSP:注册&登录

数据库:Mysql

技术分享

  除了_id自动增长,其余全是varchar

注册:register.jsp

技术分享

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://"            + request.getServerName() + ":" + request.getServerPort()            + path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP ‘Register.jsp‘ starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><link rel="stylesheet" href="css/ah.css" /><script type="text/javascript" src="js/jquery.min.js"></script><script>    function chkUname() {        var v = $("input[name=‘uname‘]").val();        if (v.trim() == "") {            alert("用户名不能为空");            return false;        }        return true;    }    function chkPwd() {        var v = $("input[name=‘pwd‘]").val();        if (v.trim() == "") {            alert("密码不能为空");            return false;        }        return true;    }    function chkPwd2() {        var pwd1 = $("input[name=‘pwd‘]").val();        var pwd2 = $("input[name=‘pwd2‘]").val();        if (pwd1 != pwd2) {            alert("两次输入的密码不一致");            return false;        }        return true;    }    function chkEmail() {        var v = $("input[name=‘email‘]").val();        if (v.trim() == "") {            alert("邮箱不能为空");            return false;        }        // 格式匹配        var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;        if (!reg.test(v)) {            alert("邮箱的格式不正确");            return false;        }        return true;    }    function check() {        if (chkUname() && chkPwd() && chkPwd2() && chkEmail()) {            return true;        }        return false;    }</script></head><body>    <form action="S003User?approach=register" method="post"        onsubmit="return check();">        <table align="center" border="0" class="padding_top">            <tr>                <td colspan="3" class="title_1">用户注册</td>            </tr>            <tr>                <td>用户名:</td>                <td><input type="text" name="uname" onblur="chkUname()"></td>                <td><span class=‘must_w‘>*</span></td>            </tr>            <tr>                <td>密码:</td>                <td><input type="password" name="pwd" onblur="chkPwd()"></td>                <td><span class=‘must_w‘>*</span></td>            </tr>            <tr>                <td>密码确认:</td>                <td><input type="password" name="pwd2" onblur="chkPwd2()"></td>                <td><span class=‘must_w‘>*</span></td>            </tr>            <tr>                <td>邮箱:</td>                <td><input type="text" name="email" onblur="chkEmail()"></td>                <td><span class=‘must_w‘>*</span></td>            </tr>            <tr>                <td>手机:</td>                <td><input type="text" name="tel"></td>            </tr>            <tr>                <td colspan="3" align="center"><input type="submit" value="提交" />                    &nbsp;&nbsp;<input type="reset" value="重置" /> <a href="login.jsp">去注册</a>                </td>            </tr>        </table>    </form></body></html>

登录:login.jsp

技术分享

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://"            + request.getServerName() + ":" + request.getServerPort()            + path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP ‘login.jsp‘ starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><link rel="stylesheet" href="css/ah.css" /><script type="text/javascript" src="js/jquery.min.js"></script></head><body>    <!-- <form action="S003User?t=login" method="post"> -->    <form action="S003User?approach=login" method="post"        onsubmit="return check();">        <table align="center" border="0" class="padding_top">            <tr>                <td colspan="3" class="title_1">用户登录</td>            </tr>            <tr>                <td>用户名:</td>                <td><input type="text" name="uname" onblur="chkUname()"></td>            </tr>            <tr>                <td>密码:</td>                <td><input type="password" name="pwd" onblur="chkPwd()"></td>            </tr>            <tr>                <td colspan="3" align="center"><input type="submit" value="提交" />                    &nbsp;&nbsp;<input type="reset" value="重置" /> <a                    href="register.jsp">直接登录</a></td>            </tr>        </table>    </form></body></html>

CSS:ah.css

#tag {    color: red;    background-color: #00FFFF;    border-radius: 3px;    padding: 5px;}/* 必须输入 */.must_w {    color: red;}/* 上隔离 */.padding_top {    padding-top: 50px;}/* 页面标题1 */.title_1 {    font-family: "微软雅黑";    font-size: larger;    font-weight: bold;}

 

Servlet:S003User.java

package org.ah.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.ah.bean.UserBean;import org.ah.dao.UserDao;public class S003User extends HttpServlet {    private static final long serialVersionUID = 1L;    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        int ret = 0;        String uName = request.getParameter("uname");        String uPwd = request.getParameter("pwd");        UserDao dao = new UserDao();        String approach = request.getParameter("approach");        if ("register".equals(approach)) {            // 注册            String eMail = request.getParameter("email");            String tel = request.getParameter("tel");            UserBean u = new UserBean(uName, uPwd, eMail, tel);            ret = dao.register(u);            if (ret > 0) {                response.sendRedirect("login.jsp");            } else {                response.sendRedirect("register.jsp");            }        } else if ("login".equals(approach)) {            // 登录            UserBean u = new UserBean(uName, uPwd, "", "");            ret = dao.login(u);            if (ret > 0) {                response.sendRedirect("ArticleListServlet");            } else {                response.sendRedirect("login.jsp");            }        }    }}

数据库连接:

package org.ah.dao;import java.sql.*;public class BaseDao {    private static final String DRIVER = "com.mysql.jdbc.Driver";    private static final String URL = "jdbc:mysql://localhost:3306/ahrecommend";    public Connection conn;    public PreparedStatement ps;    public ResultSet rs;    public void getConn() {        try {            Class.forName(DRIVER);            conn = (Connection) DriverManager                    .getConnection(URL, "root", "root");        } catch (SQLException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }    public void closeConn() {        try {            if (conn != null) {                conn.close();                conn = null;            }            if (ps != null) {                ps.close();                ps = null;            }            if (rs != null) {                rs.close();                rs = null;            }        } catch (SQLException e) {            e.printStackTrace();        }    }}
package org.ah.dao;import java.sql.ResultSet;import java.sql.SQLException;import org.ah.bean.UserBean;public class UserDao extends BaseDao {    public int register(UserBean u) {        int ret = 0;        super.getConn();        try {            StringBuffer sb = new StringBuffer();            sb.append(" INSERT INTO");            sb.append("    usermst");            sb.append("    VALUES");            sb.append("    (");            sb.append("     0,");// 自增            sb.append("     ?,");            sb.append("     ?,");            sb.append("     ?,");            sb.append("     ?");            sb.append("    )");            String strSQL = sb.toString();            ps = conn.prepareStatement(strSQL);            ps.setString(1, u.getuName());            ps.setString(2, u.getuPwd());            ps.setString(3, u.geteMail());            ps.setString(4, u.getTel());            ret = ps.executeUpdate();        } catch (SQLException e) {            e.printStackTrace();        } finally {            super.closeConn();        }        return ret;    }    public int login(UserBean u) {        int ret = 0;        super.getConn();        try {            StringBuffer sb = new StringBuffer();            sb.append(" SELECT");            sb.append("    uName");            sb.append(" FROM");            sb.append("    usermst");            sb.append(" WHERE");            sb.append("    uName = ?");            sb.append("     AND");            sb.append("    uPwd = ?");            String strSQL = sb.toString();            ps = conn.prepareStatement(strSQL);            ps.setString(1, u.getuName());            ps.setString(2, u.getuPwd());            super.rs = ps.executeQuery();            if (rs.next()) {                // 查询到(一条)数据                ret = 1;            }        } catch (SQLException e) {            e.printStackTrace();        } finally {            super.closeConn();        }        return ret;    }}

 

JSP:注册&登录