主题:编写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>
现在就是连接数据库判断用户是否已经存在无法实现,还有就是好像很繁锁~
有没有简便的方法~请指点~
谢谢
超女音乐吧需要注册,请为此编写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>
现在就是连接数据库判断用户是否已经存在无法实现,还有就是好像很繁锁~
有没有简便的方法~请指点~
谢谢