大三下jsp作业九

index

<%@ 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>

<script type="text/javascript">

function aaa() {

window.location = "registe.jsp";

}

</script>

<base href="<%=basePath%>">

<title>My JSP 'index.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" type="text/css" href="styles.css">

-->

</head>

<body>

<form action="dologin.jsp" method="post">

用户名:<input type="text" name="uname" value="小白" />

密码 : <input type="password" name="upwd" value="88888" /><br> <br>

<input type="button" value="注册" onclick="aaa()">&nbsp;&nbsp;

<input type="submit" value="登录">

</form>

</body>

</html>

registe

<%@ 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>
<style type="text/css">
a {
    text-decoration: none;
}

img {
    height-line: 20px;
    height: 20px;
    width: 20px;
}
</style>
<script type="text/javascript">
    function a() {
        alert("注册成功,请您登录使用。");
    }
    function b() {
        alert("您是否放弃注册!");
        window.location = "index.jsp";
    }
    function mycheck() {
        //判断用户名是否为空
        if (form.newuser.value == "") {
            alert("用户名不能为空,请输入用户名!");
            form.userName.focus();
            return;
        }
        //判断密码是否为空
        if (form.newpassword.value == "") {
            alert("密码不能为空,请输入密码!");
            form.password.focus();
            return;
        }
        //判断验证码是否为空
        if (form.validationCode.value == "") {
            alert("验证码不能为空,请输入验证码!");
            form.validationCode.focus();
            return;
        }
        //判断验证码是否正确
        if (form.validationCode.value != form.validationCode1.value) {
            alert("请输入正确的验证码!!");
            form.validationCode.focus();
            return;
        }
        form.submit();
    }
</script>
<base href="<%=basePath%>">

<title>My JSP 'registe.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" type="text/css" href="styles.css">
    -->

</head>

<body>
    <form action="doregiste.jsp" method="post">
        <p>
            用户名:<input type="text" name="newuser" placeholder="请设定您的用户名" />
        </p>
        <p>
            密码:<input type="text" name="newpassword" placeholder="请设定您的密码" />
        </p>
        <p>
            邮箱:<input type="text" name="newemail" placeholder="请输入您的可用邮箱邮箱" />
        </p>
        <p>
            验证码:<input type="text" name="validationCode"
                onKeyDown="if(event.keyCode==13){form.submit.focus();}" size="6">
            <%
                int intmethod1 = (int) ((((Math.random()) * 11)) - 1);
                int intmethod2 = (int) ((((Math.random()) * 11)) - 1);
                int intmethod3 = (int) ((((Math.random()) * 11)) - 1);
                int intmethod4 = (int) ((((Math.random()) * 11)) - 1);
                //将得到的随机数进行连接
                String intsum = intmethod1 + "" + intmethod2 + intmethod3
                        + intmethod4;
            %>
            <!-- 设置隐藏域,验证比较时使用-->
            <input type="hidden" name="validationCode1" value="<%=intsum%>">
            <!-- 将图片名称与得到的随机数相同的图片显示在页面上 -->
            <img src="./images/<%=intmethod1%>.jpg"> <img
                src="./images/<%=intmethod2%>.jpg"> <img
                src="./images/<%=intmethod3%>.jpg"> <img
                src="./images/<%=intmethod4%>.jpg"><br>
            <br>

        </p>
        <input type="submit" value="注册" onclick="a()">&nbsp;&nbsp; <input
            type="button" value="返回" onclick="b()">
    </form>


</body>
</html>

doregiste

<%@page import="com.gd.entity.Users"%>
<%@page import="com.gd.dao.UsersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    request.setCharacterEncoding("utf-8");
    String newuser = request.getParameter("newuser");
    String newpassword = request.getParameter("newpassword");
    String newemail = request.getParameter("newemail");
    
    Users u = new Users();
    u.setUsername(newuser);
    u.setPassword(newpassword);
    u.setEmail(newemail);
    UsersDao ud = new UsersDao();
    ud.registea(u);
    request.getRequestDispatcher("index.jsp").forward(request, response);

%>

UsersDao

package com.gd.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gd.entity.Users;

public class UsersDao extends BaseDao {
    
    public boolean login(String uname, String upwd) throws SQLException {
        
        Connection conn = getConnection();
        
        String sql = "select * from users where username=? and password=?";
        
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, uname);
        ps.setString(2, upwd);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            closeAll(conn, ps, rs);
            return true;
        } else {
            closeAll(conn, ps, rs);
            return false;

        }
    }

    public void registe(String uname, String upwd, String email)
            throws SQLException {
       
        Connection conn = getConnection();
        
        String sql = "insert into users (username,PASSWORD,email) value (?,?,?)";

        
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, uname);
        ps.setString(2, upwd);
        ps.setString(3, email);
        int row = ps.executeUpdate();
        if (row == 1) {
            System.out.println("添加成功");
            closeAll(conn, ps, null);
        } else {
            System.out.println("添加失败");
        }
    }

    public void registea(Users users)
            throws SQLException {
     
        Connection conn = getConnection();
     
        String sql = "insert into users (username,PASSWORD,email) value (?,?,?)";

        
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, users.getUsername());
        ps.setString(2, users.getPassword());
        ps.setString(3, users.getEmail());
        ps.executeUpdate();
        closeAll(conn, ps, null);
        
    }

// public static void main(String[] args) {
// UsersDao ud=new UsersDao();
// try {
// System.out.println(ud.login("tom", "456"));
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//    public static void main(String[] args) {
//
//        UsersDao ud = new UsersDao();
//
//        try {
//            ud.registe("yuhang", "456", "1418612405");
//
//        } catch (SQLException e) {
//
//            e.printStackTrace();
//        }
//
//    }
}

 

 

猜你喜欢

转载自www.cnblogs.com/cunhua/p/12931466.html
今日推荐