JSP 注册页面 如何添加验证用户名是否存在的代码? 20
RT数据库是用SQLserver的数据库名login用户名username密码password下面是注册提交的JSP页面<%@pagelanguage="java"imp...
RT 数据库是用SQL server的 数据库名login 用户名username
密码password 下面是注册提交的JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page contentType="text/html; charset=GBK"%>
<%request.setCharacterEncoding("GBK");%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%@ page language="java" import="java.sql.*"%>
your username is:<%=request.getParameter("username")%>
<br><br>
your password is:<%=request.getParameter("pass")%>
<br><br>
<%
String name=request.getParameter("username");
String pass=request.getParameter("pass");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbcon=DriverManager.getConnection("jdbc:odbc:login","sa","");
PreparedStatement stat=dbcon.prepareStatement(
"insert login values(?,?)");
stat.setString(1,name);
stat.setString(2,pass);
stat.executeUpdate();
out.println("<br><br><B>注册成功</B>");
}
catch(Exception e)
{
out.println(e);
}
%>
<form method=get action="http://localhost:8080/xxx/test2.jsp">
<input type=submit value="查看用户">
</form>
</html>
可以根据我给的JSP进行修改吗? 展开
密码password 下面是注册提交的JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page contentType="text/html; charset=GBK"%>
<%request.setCharacterEncoding("GBK");%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%@ page language="java" import="java.sql.*"%>
your username is:<%=request.getParameter("username")%>
<br><br>
your password is:<%=request.getParameter("pass")%>
<br><br>
<%
String name=request.getParameter("username");
String pass=request.getParameter("pass");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbcon=DriverManager.getConnection("jdbc:odbc:login","sa","");
PreparedStatement stat=dbcon.prepareStatement(
"insert login values(?,?)");
stat.setString(1,name);
stat.setString(2,pass);
stat.executeUpdate();
out.println("<br><br><B>注册成功</B>");
}
catch(Exception e)
{
out.println(e);
}
%>
<form method=get action="http://localhost:8080/xxx/test2.jsp">
<input type=submit value="查看用户">
</form>
</html>
可以根据我给的JSP进行修改吗? 展开
3个回答
展开全部
用ajax
有段代码 你参考下
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>
<script language="javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
}
function checkloginname()
{
var loginname=document.all.personregedit.loginname.value;
if(loginname==""||!checklength(loginname,4,16))
{
document.getElementById("lname").innerHTML=" <img src='../images/31.gif' border='0' /><font color='#FF0000' size='-1'>该用户名不合法,快重新填写!</font>";
document.all.personregedit.loginname.value="";
document.all.personregedit.loginname.focus();
return;
}
createXMLHttpRequest();
xmlHttp.onreadystatechange=checknamers;
xmlHttp.open("post","loginname.checkname?name="+loginname);
xmlHttp.send(null);
}
function checknamers()
{
var responseContext;
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
responseContext=xmlHttp.responseText;
if(responseContext.indexOf("null")==-1)
{
document.getElementById("lname").innerHTML=" <img src='../images/09.gif' border='0' /><font color='#FF0000' size='-1'>Sorry!该用户名已被注册,请重新填写!</font>";
document.all.personregedit.loginname.value="";
document.all.personregedit.loginname.focus();
}else
{
document.getElementById("lname").innerHTML=" <img src='../images/04.gif' border='0' /><font color='#0000FF' size='-1'>恭喜!该用户名可用!</font>";
}
}
}
}
function checklength(thestring,a,b)
{
var string=thestring;
var minl=parseInt(a);
var maxl=parseInt(b);
var thelength=string.length;
if(thelength>=minl&&thelength<=maxl)
{
return true;
}else
{
return false;
}
}
function checkrpsw()
{
var password=document.all.personregedit.password.value;
var repassword=document.all.personregedit.repassword.value;
if(repassword!=password)
{
document.getElementById("ps").innerHTML=" <img src='../images/31.gif' border='0' /><font color='#FF0000' size='-1'>你两次输入的密码不同,重来吧!</font>";
document.all.personregedit.password.value="";
document.all.personregedit.repassword.value="";
document.all.personregedit.password.focus();
}else
{
document.getElementById("ps").innerHTML="";
}
}
function checkemail()
{
var email=document.all.personregedit.email.value;
if((email!=null&&email!="")&&(email.indexOf("@")==-1||email.indexOf("@")==0||email.indexOf("@")==email.length-1))
{
alert("您输入的邮箱格式有误,请重新填写!")
document.all.personregedit.email.value="";
document.all.personregedit.email.focus();
}
}
function submitform()
{
checkrpsw();
if(document.all.personregedit.personname.value==null||document.all.personregedit.personname.value=="")
{
alert("姓名是必填项!");
}else if(document.all.personregedit.loginname.value==null||document.all.personregedit.loginname.value==""){
alert("用户名是必填项!");
}else if(!checklength(document.all.personregedit.loginname.value,4,16)){
alert("用户名的长度必须在4-16个字符之间!");
}else if(document.all.personregedit.password.value==null||document.all.personregedit.password.value==""){
alert("密码不能为空!");
}else if(!checklength(document.all.personregedit.password.value,4,16)){
alert("用户密码的长度必须在4-16个字符之间!");
}else if(document.all.personregedit.email.value==null||document.all.personregedit.email.value==""){
alert("请填写邮箱地址!");
}else{
document.all.personregedit.action="regeditaction.jsp";
document.all.personregedit.submit();
}
}
</script>
<body>
<form name="personregedit" method="post" action="">
<input type="hidden" name="regedittype" value="person" />
<table width="98%" height="98%" cellpadding="1" cellspacing="1">
<tr>
<td>
<table width="100%" height="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center"><font color="#0000FF" size="+2" face="楷体_GB2312">个 人 信 息 注 册</font><br /><br /><br /></td>
</tr>
<tr>
<tr>
<td align="right"><a href="regeditunit.jsp"><font color="#0000FF" size="-1" face="新宋体">团 体 信 息 注 册 </font></a><br /><br /><br /></td>
</tr>
<td><table width="100%" height="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="right" width="19%" valign="top">姓 名:</td>
<td width="42%" align="left" valign="top">
<input name="personname" type="text" size="40" value="" /></td>
<td width="39%" align="left"> <img src="../images/tutu02.gif" border="0" /><font size="2" color="#485EF2">请填写真实姓名</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">用户名:</td>
<td align="left" valign="top">
<input name="loginname" type="text" size="40" value="" onblur="checkloginname()" onkeyup="value=value.replace(/[^a-zA-Z0-9]/g,'')" onmouseover="value=value.replace(/[^a-zA-Z0-9]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-zA-Z0-9]/g,''))"/>
<div id="lname"></div></td>
<td> <img src="../images/tutu02.gif" border="0" /><font size="2" color="#485EF2">即登录名,只能为数字和英文字母的组合(4-16个字符)!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">密 码:</td>
<td align="left" valign="top">
<input name="password" type="password" size="44" value="" onkeyup="value=value.replace(/[^a-zA-Z0-9]/g,'')" onmouseover="value=value.replace(/[^a-zA-Z0-9]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-zA-Z0-9]/g,''))"/></td>
<td> <img src="../images/tutu02.gif" border="0" /><font size="2" color="#485EF2">即登录密码,只能为数字和英文字母的组合(4-16个字符)!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">重复密码:</td>
<td align="left" valign="top">
<input name="repassword" type="password" size="44" value="" onblur="checkrpsw()" onkeyup="value=value.replace(/[^a-zA-Z0-9]/g,'')" onmouseover="value=value.replace(/[^a-zA-Z0-9]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-zA-Z0-9]/g,''))"/>
<div id="ps" ></div></td>
<td> <img src="../images/tutu02.gif" border="0" /><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">学 院:</td>
<td align="left" valign="top">
<input name="colleage" type="text" size="40" value="" /></td>
<td> <font size="2" color="#485EF2">选填内容!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">班 级:</td>
<td align="left" valign="top">
<input name="theclass" type="text" size="40" value=""/></td>
<td> <font size="2" color="#485EF2">选填内容!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">邮 箱:</td>
<td align="left" valign="top">
<input name="email" type="text" size="40" value="" onblur="checkemail()"/></td>
<td> <img src="../images/tutu02.gif" border="0" /><font size="2" color="#485EF2">请确认邮箱可以正常使用,它是您取回密码和接收管理员信息的唯一方式!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">注册协议:</td>
<td align="left" valign="top">
<textarea name="textarea" cols="40" rows="5" readonly="readonly" style="background-color:#CCCCCC">
用户成功注册后... </textarea>
<br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%"></td>
<td align="left"><font color="#FF0000" face="新宋体" size="2">注册用户注册后默认同意以上注册协议,请认真阅读后提交注册信息!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%"></td>
<td align="left">
<input name="button" type="button" onclick="submitform()" value="提 交"/>
<input name="reset" type="reset" value="重 填"/></td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
有段代码 你参考下
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>
<script language="javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
}
function checkloginname()
{
var loginname=document.all.personregedit.loginname.value;
if(loginname==""||!checklength(loginname,4,16))
{
document.getElementById("lname").innerHTML=" <img src='../images/31.gif' border='0' /><font color='#FF0000' size='-1'>该用户名不合法,快重新填写!</font>";
document.all.personregedit.loginname.value="";
document.all.personregedit.loginname.focus();
return;
}
createXMLHttpRequest();
xmlHttp.onreadystatechange=checknamers;
xmlHttp.open("post","loginname.checkname?name="+loginname);
xmlHttp.send(null);
}
function checknamers()
{
var responseContext;
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
responseContext=xmlHttp.responseText;
if(responseContext.indexOf("null")==-1)
{
document.getElementById("lname").innerHTML=" <img src='../images/09.gif' border='0' /><font color='#FF0000' size='-1'>Sorry!该用户名已被注册,请重新填写!</font>";
document.all.personregedit.loginname.value="";
document.all.personregedit.loginname.focus();
}else
{
document.getElementById("lname").innerHTML=" <img src='../images/04.gif' border='0' /><font color='#0000FF' size='-1'>恭喜!该用户名可用!</font>";
}
}
}
}
function checklength(thestring,a,b)
{
var string=thestring;
var minl=parseInt(a);
var maxl=parseInt(b);
var thelength=string.length;
if(thelength>=minl&&thelength<=maxl)
{
return true;
}else
{
return false;
}
}
function checkrpsw()
{
var password=document.all.personregedit.password.value;
var repassword=document.all.personregedit.repassword.value;
if(repassword!=password)
{
document.getElementById("ps").innerHTML=" <img src='../images/31.gif' border='0' /><font color='#FF0000' size='-1'>你两次输入的密码不同,重来吧!</font>";
document.all.personregedit.password.value="";
document.all.personregedit.repassword.value="";
document.all.personregedit.password.focus();
}else
{
document.getElementById("ps").innerHTML="";
}
}
function checkemail()
{
var email=document.all.personregedit.email.value;
if((email!=null&&email!="")&&(email.indexOf("@")==-1||email.indexOf("@")==0||email.indexOf("@")==email.length-1))
{
alert("您输入的邮箱格式有误,请重新填写!")
document.all.personregedit.email.value="";
document.all.personregedit.email.focus();
}
}
function submitform()
{
checkrpsw();
if(document.all.personregedit.personname.value==null||document.all.personregedit.personname.value=="")
{
alert("姓名是必填项!");
}else if(document.all.personregedit.loginname.value==null||document.all.personregedit.loginname.value==""){
alert("用户名是必填项!");
}else if(!checklength(document.all.personregedit.loginname.value,4,16)){
alert("用户名的长度必须在4-16个字符之间!");
}else if(document.all.personregedit.password.value==null||document.all.personregedit.password.value==""){
alert("密码不能为空!");
}else if(!checklength(document.all.personregedit.password.value,4,16)){
alert("用户密码的长度必须在4-16个字符之间!");
}else if(document.all.personregedit.email.value==null||document.all.personregedit.email.value==""){
alert("请填写邮箱地址!");
}else{
document.all.personregedit.action="regeditaction.jsp";
document.all.personregedit.submit();
}
}
</script>
<body>
<form name="personregedit" method="post" action="">
<input type="hidden" name="regedittype" value="person" />
<table width="98%" height="98%" cellpadding="1" cellspacing="1">
<tr>
<td>
<table width="100%" height="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center"><font color="#0000FF" size="+2" face="楷体_GB2312">个 人 信 息 注 册</font><br /><br /><br /></td>
</tr>
<tr>
<tr>
<td align="right"><a href="regeditunit.jsp"><font color="#0000FF" size="-1" face="新宋体">团 体 信 息 注 册 </font></a><br /><br /><br /></td>
</tr>
<td><table width="100%" height="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="right" width="19%" valign="top">姓 名:</td>
<td width="42%" align="left" valign="top">
<input name="personname" type="text" size="40" value="" /></td>
<td width="39%" align="left"> <img src="../images/tutu02.gif" border="0" /><font size="2" color="#485EF2">请填写真实姓名</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">用户名:</td>
<td align="left" valign="top">
<input name="loginname" type="text" size="40" value="" onblur="checkloginname()" onkeyup="value=value.replace(/[^a-zA-Z0-9]/g,'')" onmouseover="value=value.replace(/[^a-zA-Z0-9]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-zA-Z0-9]/g,''))"/>
<div id="lname"></div></td>
<td> <img src="../images/tutu02.gif" border="0" /><font size="2" color="#485EF2">即登录名,只能为数字和英文字母的组合(4-16个字符)!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">密 码:</td>
<td align="left" valign="top">
<input name="password" type="password" size="44" value="" onkeyup="value=value.replace(/[^a-zA-Z0-9]/g,'')" onmouseover="value=value.replace(/[^a-zA-Z0-9]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-zA-Z0-9]/g,''))"/></td>
<td> <img src="../images/tutu02.gif" border="0" /><font size="2" color="#485EF2">即登录密码,只能为数字和英文字母的组合(4-16个字符)!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">重复密码:</td>
<td align="left" valign="top">
<input name="repassword" type="password" size="44" value="" onblur="checkrpsw()" onkeyup="value=value.replace(/[^a-zA-Z0-9]/g,'')" onmouseover="value=value.replace(/[^a-zA-Z0-9]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-zA-Z0-9]/g,''))"/>
<div id="ps" ></div></td>
<td> <img src="../images/tutu02.gif" border="0" /><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">学 院:</td>
<td align="left" valign="top">
<input name="colleage" type="text" size="40" value="" /></td>
<td> <font size="2" color="#485EF2">选填内容!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">班 级:</td>
<td align="left" valign="top">
<input name="theclass" type="text" size="40" value=""/></td>
<td> <font size="2" color="#485EF2">选填内容!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">邮 箱:</td>
<td align="left" valign="top">
<input name="email" type="text" size="40" value="" onblur="checkemail()"/></td>
<td> <img src="../images/tutu02.gif" border="0" /><font size="2" color="#485EF2">请确认邮箱可以正常使用,它是您取回密码和接收管理员信息的唯一方式!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%" valign="top">注册协议:</td>
<td align="left" valign="top">
<textarea name="textarea" cols="40" rows="5" readonly="readonly" style="background-color:#CCCCCC">
用户成功注册后... </textarea>
<br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%"></td>
<td align="left"><font color="#FF0000" face="新宋体" size="2">注册用户注册后默认同意以上注册协议,请认真阅读后提交注册信息!</font><br />
<br /></td>
</tr>
<tr>
<td align="right" width="19%"></td>
<td align="left">
<input name="button" type="button" onclick="submitform()" value="提 交"/>
<input name="reset" type="reset" value="重 填"/></td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
展开全部
在插入数据库前先查询
PreparedStatement stat=dbcon.prepareStatement("查询语句")
查询语句:select 注册的用户名 from 表
如果stat不是空,用户名存在,跳转页面
如果是空则进行插入数据操作,最后提示成功。
PreparedStatement stat=dbcon.prepareStatement("查询语句")
查询语句:select 注册的用户名 from 表
如果stat不是空,用户名存在,跳转页面
如果是空则进行插入数据操作,最后提示成功。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<%@ page language="java" import="java.sql.*"%>
your username is:<%=request.getParameter("username")%>
<br><br>
your password is:<%=request.getParameter("pass")%>
<br><br>
<%
if(username=="") return(false)
else{
String name=request.getParameter("username");
String pass=request.getParameter("pass");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbcon=DriverManager.getConnection("jdbc:odbc:login","sa","");
PreparedStatement stat=dbcon.prepareStatement(
"insert login values(?,?)");
stat.setString(1,name);
stat.setString(2,pass);
stat.executeUpdate();
out.println("<br><br><B>注册成功</B>");
}
}
your username is:<%=request.getParameter("username")%>
<br><br>
your password is:<%=request.getParameter("pass")%>
<br><br>
<%
if(username=="") return(false)
else{
String name=request.getParameter("username");
String pass=request.getParameter("pass");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbcon=DriverManager.getConnection("jdbc:odbc:login","sa","");
PreparedStatement stat=dbcon.prepareStatement(
"insert login values(?,?)");
stat.setString(1,name);
stat.setString(2,pass);
stat.executeUpdate();
out.println("<br><br><B>注册成功</B>");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询