回 帖 发 新 帖 刷新版面

主题:编写JSP页面实现注册程序~在线等

题目如下:
超女音乐吧需要注册,请为此编写JSP页面来实现注册,注册信息包括用户名,密码,性别,年龄,电话和Email。用户名不能重复,如果用户名已经存在要提示用户 ;
用户名,性别,密码和Email必须输入,密码需要输入两次,并前后一致;Email要求进行合法性检验。
这是我写的:
第一个JSP:
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
 
<html>
<head>
<title>超女音乐吧</title>
<style type="text/css">
<!--
.style1
{
font-size:2ex;
font-weight: bold;
color:#ff0000;
font-style: italic;
}
-->
</style>
</head>
<body>

<form name = myform method = post action = Result.jsp>
    <div align = center><span class=style1><h3>超女音乐吧注册</h3></span></div>
    <table align = center border = 0>
            
            <hr>
            <tr>
                <td>用户名:</td>
                <td>
                    <input type = text name = username >
                </td>
            </tr>
            
            <tr>
                <td>密码</td>
                <td>
                    <input type = password name = password1>
                    
                </td>
            </tr>
            
            <tr>
                <td>确认密码</td>
                <td>
                    <input type = password name = password2>
                    
                </td>
            </tr>
            
            <tr>
                <td>性别</td>
                <td>
                    <input type = radio name = sex checked value = 男>男
                    <input type = radio name = sex value = 女>女
                </td>
            </tr>
            
            <tr>
                <td>年龄</td>
                <td>
                    <input type = text name = age >
                </td>
            </tr>
            
            <tr>
                <td>电话</td>
                <td>
                    <input type = text name = phone maxlength = 20>
                </td>
            </tr>
            
            <tr>
                <td>Email</td>
                <td>
                    <input type = text name = mail maxlength = 50>
                </td>
            </tr>
            
            <tr>
                <td>
                    <input type = submit name = Submit value = 提交 width = 10>
                </td>
                <td>
                    <input type = reset name = Reset value = 重置>
                </td>
            </tr>

    </table>
</form>

</body>
</html>
第二个JSP:
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@ page import = "java.sql.*" %>

<%
request.setCharacterEncoding("gbk");
String name = request.getParameter("username");
String pwd1 = request.getParameter("password1");
String pwd2 = request.getParameter("password2");
String mail = request.getParameter("mail");

if(name.equals(""))
{
    out.println("请输入姓名!");
}
else
{
    if(pwd1!="" && pwd2 != "")
    {
        if(mail != "")
        {
            if(pwd1.equals(pwd2))
            {
                if(mail.indexOf("@",0) != -1)
                {
                    try
                    {
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    Connection con = DriverManager.getConnection("jdbc:odbc:test","sa","");
                    PreparedStatement ps1 = con.prepareStatement("select * from where username = ?");
                    ps1.setString(1,name);
                    ResultSet rs = ps1.executeQuery();
                    if(rs.next())
                    {
                        out.println("该用户名已经存在");
                        return;
                    }
                    PreparedStatement ps2 = con.prepareStatement("insert into register values(?)");
                    ps2.setString(1,name);
                    ResultSet rs2 = ps2.executeQuery();
                    out.println("注册成功");
                    rs.close();
                    ps2.close();
                    con.close();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    out.println("Email格式不正确");
                }
            }else
            {
                out.println("两次密码不一致!");
            }
        }
        else
        {
            out.println("Email不能为空!");
        }
    }
    else
    {
        out.println("密码不能为空!");
    }
}
%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>

</body>
</html>
现在就是连接数据库判断用户是否已经存在无法实现,还有就是好像很繁锁~
有没有简便的方法~请指点~
谢谢











回复列表 (共7个回复)

沙发

将数据库操作写到一个javabean里比较好..

板凳

是不是可以把验证都放倒一个函数里写,然后按下按钮.调用函数判断,不过我没有调用数据库来判断,等我把数据库装好了再看吧

BTW:你的进度还挺快的嘛.看来北大青鸟的效率挺高[em11]

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
 
<html>
<head>
<title>超女音乐吧</title>
<style type="text/css">
<!--
.style1
{
font-size:2ex;
font-weight: bold;
color:#ff0000;
font-style: italic;
}
-->
</style>
</head>
<body>
<script language="javascript" >
    <!--
   function chek()
    {
if(myform.username.value=="")
{
   alert("请输入姓名!");
}
else if (myform.password1.value=="" || myform.password2.value=="")
{   alert("请输入密码!");
 }
else if (myform.password1.value!= myform.password2.value)
{   alert("两次密码不一致!");
 }
else if (myform.mail.value.indexOf("@",0) == -1)
{   alert("mail格式不对!");
 }
else
myform.submit();
}
    -->
</script>


<form name = myform method = post action = Result.jsp>
    <div align = center><span class=style1><h3>超女音乐吧注册</h3></span></div>
    <table align = center border = 0>
            
            <hr>
            <tr>
                <td>用户名:</td>
                <td>
                    <input type = text name = username >
                </td>
            </tr>
            
            <tr>
                <td>密码</td>
                <td>
                    <input type = password name = password1>
                    
                </td>
            </tr>
            
            <tr>
                <td>确认密码</td>
                <td>
                    <input type = password name = password2>
                    
                </td>
            </tr>
            
            <tr>
                <td>性别</td>
                <td>
                    <input type = radio name = sex checked value = 男>男
                    <input type = radio name = sex value = 女>女
                </td>
            </tr>
            
            <tr>
                <td>年龄</td>
                <td>
                    <input type = text name = age >
                </td>
            </tr>
            
            <tr>
                <td>电话</td>
                <td>
                    <input type = text name = phone maxlength = 20>
                </td>
            </tr>
            
            <tr>
                <td>Email</td>
                <td>
                    <input type = text name = mail maxlength = 50>
                </td>
            </tr>
            
            <tr>
                <td>
                    <input type = button name = Submit value = 提交 width = 10 onclick="chek()">
                </td>
                <td>
                    <input type = reset name = Reset value = 重置>
                </td>
            </tr>

    </table>
</form>

</body>
</html>


///////////////
Result.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

    <h1>注册成功!</h1>

    
    </body>
</html>

3 楼

你怎么知道我在北大青鸟上学?

4 楼

[quote]你怎么知道我在北大青鸟上学?[/quote]
因为我搜索了一下关于北大青鸟的帖子,看到了你的回复,呵呵

5 楼

有侦探的眼光~

6 楼

同是北大菜鸟人,同情同情

7 楼

嗯咯,嗯咯,强烈的同情!~

我来回复

您尚未登录,请登录后再回复。点此登录或注册