在jsp中通过Ajax判断注册的用户名是否存在,并且是不用通过跳转页面来判断的代码
1个回答
展开全部
<html>
<head>
<script type="text/javascript">
function $(tagId){
return document.getElementById(tagId);
}
function checkUserName(){
var name = $("nameId").value;
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else if(window.ActiveXObject){
xhr = new ActiveXObject("Microsoft.XMLHttp");
}
xhr.open("POST","/Ajax/userNameCheckServlet");
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send("userNeme="+name);
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var ret = xhr.responseText;
var span = $("msgId");
//innerHTML作用 方便往标签中增加内容
span.innerHTML = "<font color='red'>"+ret+"</font>" ;
}
}
}
</script>
</head>
<body>
<table>
<tr>
<td>用户名:<input type="text" id="nameId" onblur="checkUserName();"/>
<span id="msgId"></span></td>
</tr>
<tr>
<td>密 码:<input type="password"/></td>
</tr>
<tr>
<td align="center"><input type="button" value="确定"/></td>
</tr>
</table>
</body>
</html>
然后在servlet里判断是否用户名存在:
package com.wsw.day01;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserNameCheckServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
String userName = request.getParameter("userNeme");
System.out.println(userName);
PrintWriter out = response.getWriter();
if("wsw".equals(userName)){
out.print("is ok");
}else{
out.print("is false");
}
out.flush();
}
}
这里是简单的判断,实际应用的时候要连数据库,查询判断,反正都是这个原理
<head>
<script type="text/javascript">
function $(tagId){
return document.getElementById(tagId);
}
function checkUserName(){
var name = $("nameId").value;
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else if(window.ActiveXObject){
xhr = new ActiveXObject("Microsoft.XMLHttp");
}
xhr.open("POST","/Ajax/userNameCheckServlet");
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send("userNeme="+name);
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var ret = xhr.responseText;
var span = $("msgId");
//innerHTML作用 方便往标签中增加内容
span.innerHTML = "<font color='red'>"+ret+"</font>" ;
}
}
}
</script>
</head>
<body>
<table>
<tr>
<td>用户名:<input type="text" id="nameId" onblur="checkUserName();"/>
<span id="msgId"></span></td>
</tr>
<tr>
<td>密 码:<input type="password"/></td>
</tr>
<tr>
<td align="center"><input type="button" value="确定"/></td>
</tr>
</table>
</body>
</html>
然后在servlet里判断是否用户名存在:
package com.wsw.day01;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserNameCheckServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
String userName = request.getParameter("userNeme");
System.out.println(userName);
PrintWriter out = response.getWriter();
if("wsw".equals(userName)){
out.print("is ok");
}else{
out.print("is false");
}
out.flush();
}
}
这里是简单的判断,实际应用的时候要连数据库,查询判断,反正都是这个原理
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询