前端之家收集整理的这篇文章主要介绍了
ajax表单验证后台用户名是否存在,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package ajaxservlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import db.DBconnection;
public class AjaxServlet extends HttpServlet {
//private static final long serialVersionUID = 1L;
public void destroy() {
try {
DBconnection.connectionClose();
} catch (sqlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
response.setContentType("text/html;charset=utf-8");
response.setHeader("Cache-Control","no-cache");
PrintWriter out=response.getWriter();
String name=request.getParameter("username");
String pass=request.getParameter("userpass");
String pass2=request.getParameter("userpass2");
String a=request.getParameter("a");
String b=request.getParameter("b");
System.out.println(name+" "+pass);
System.out.println("a "+a);
System.out.println("b "+b);
request.setAttribute("name",name);
System.out.println(name);
try {
if(checkName(name))
{
System.out.println(checkName(name));
System.out.println("checkname="+name);
out.write("用户名已经存在,请重新输入!");
System.out.println("xxxxxxxxxxxxxxxxx");
request.setAttribute("error.message","用户名已经存在");
}
else
{
out.write("此用户名合法");
}
} catch (sqlException e) {
// TODO Auto-generated catch block
out.close();
e.printStackTrace();
}
}
public boolean checkName(String name) throws sqlException //检查用户名是否存在
{
boolean flag=false;
Connection conn=DBconnection.getConnection();
System.out.println("conn="+conn);
/*String sql="select * from a_user where username='"+name+"'";
System.out.println("name="+name);
Statement state=conn.createStatement();
ResultSet rs=state.executeQuery(sql);*/
String sql="select * from a_user where username=?";
PreparedStatement pstate=conn.prepareStatement(sql);
pstate.setString(1,name);
ResultSet rs=pstate.executeQuery();
//System.out.println(rs.getInt(0));
//System.out.println(rs.getString("username"));
while(rs.next())
{
System.out.println("rs.getString(username)="+rs.getString("username"));
flag=true;
}
return flag;
}
public void doPost(HttpServletRequest request,IOException {
//response.setContentType("text/html");
this.doGet(request,response);
}
public void init() throws ServletException {
}
}
- //以下是ajax部分
- varxmlhttp;
- functionajaxfun()
- {
- if(window.XMLHttpRequest)
- {//codeforIE7+,Firefox,Chrome,Opera,Safari
- xmlhttp=newXMLHttpRequest();
- }
- else
- {//codeforIE6,IE5
- xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
- }
- returnxmlhttp;
- }
- window.onload=function()
- {
- document.getElementById("username1").onblur=function()
- {
- varname=document.getElementsByName("username")[0].value;
- xmlhttp=ajaxfun();
- xmlhttp.onreadystatechange=check;
- //xmlhttp.open("get","http://localhost:8080/ajaxtest/AjaxServlet?username="+name+"&time="+newDate().toTimeString(),true);
- //以下是get方法传值
- /*xmlhttp.open("get","./AjaxServlet?username="+name+"&time="+newDate().toTimeString(),true);
- xmlhttp.send(null);*/
- /*
- *如果需要像HTML表单那样使用POST传递数据,请使用setRequestHeader()来添加HTTP头。
- *然后在send()方法中规定您希望发送的数据:
- */
- xmlhttp.open("post",true);
- xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
- xmlhttp.send("a=100&b=222");
- }
- }
- functioncheck()
- {
- alert(xmlhttp.status);
- if(xmlhttp.readyState==4&&xmlhttp.status==200)
- {
- alert(xmlhttp.status);
- document.getElementById("span").innerHTML=xmlhttp.responseText;
- }
- }
//以下是ajax部分
var xmlhttp;
function ajaxfun()
{
if (window.XMLHttpRequest)
{// code for IE7+,Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6,IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
window.onload=function()
{
document.getElementById("username1").onblur=function()
{
var name=document.getElementsByName("username")[0].value;
xmlhttp=ajaxfun();
xmlhttp.onreadystatechange=check;
//xmlhttp.open("get","http://localhost:8080/ajaxtest/AjaxServlet?username="+name+"&time="+new Date().toTimeString(),true);
//以下是get方法传值
/*xmlhttp.open("get","./AjaxServlet?username="+name+"&time="+new Date().toTimeString(),true);
xmlhttp.send(null);*/
/*
* 如果需要像 HTML 表单那样使用 POST 传递数据,请使用 setRequestHeader() 来添加 HTTP 头。
* 然后在 send() 方法中规定您希望发送的数据:
*/
xmlhttp.open("post",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
xmlhttp.send("a=100&b=222");
}
}
function check()
{
alert(xmlhttp.status);
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.status);
document.getElementById("span").innerHTML=xmlhttp.responseText;
}
}
- //jsp中的body部分
- <body>
- <%--<formaction="/ajaxtest/RegisteServlet"method="get">
- <formaction=""method="get"enctype="application/x-www-form-urlencoded">
- --%>
- <formaction="/ajaxtest/RegisteServlet"method="get">
- 用户名:<inputtype="text"name="username"id="username1"/><spanid="span"></span><br>
- 密码:<inputtype="password"name="userpass"/><br>
- 确认密码:<inputtype="password"name="userpass2"/><br>
- <inputtype="submit"value="提交"/>
- <inputtype="button"name="username"id="username1"value="点击"/>
- </form>
- </body>
原文链接:https://www.f2er.com/ajax/165920.html