Posted on 2008-09-24 15:58
鱼跃于渊 阅读(711)
评论(0) 编辑 收藏 所属分类:
平时小练习
login.html :
<!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>Insert title here</title>
<script type="text/javascript">
function checkname(){
//alert("test") ;
document.getElementById("msg").innerHTML="<img src=\"images/fwf3.JPG\" />" ;
var url = "checklogin.jsp?username=" + document.forms[0].username.value ;
//alert(url) ;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest() ;
}else{
if(window.ActiveXObject){
try{
xhr = new ActiveXObject("Microsoft.XMLHttpRequest") ;
}catch(e){}
}
}
if(xhr){
xhr.onreadystatechange = showContents ;
xhr.open("GET", url, true) ;
xhr.send(null) ;
}else{
document.getElementById("msg").innerHTML = "sorry ! but your pc coundn't create a xhr object !" ;
}
}
function showContents(){
if(xhr.readyState == 4){
if(xhr.status == 200){
//alert(xhr.responseXML.contentType) ;
//var outMsg = xhr.responseText ;
//if(xhr.responseXML && xhr.responseXML.contentType == "text/xml"){
var outMsg = xhr.responseXML.getElementsByTagName("msg")[0].childNodes[0].nodeValue ;
//}else{
//var outMsg = xhr.responseText ;
//}
//alert(outMsg) ;
if(outMsg == "validate"){
document.getElementById("msg").innerHTML = "<font color=green><strong>用户名正确 </strong></font>" ;
}else{
document.getElementById("msg").innerHTML = "<font color=red><strong>用户名不正确 </strong></font>" ;
}
}else{
var outMsg = "there was a problem on your server : status = " + status ;
}
}
}
</script>
</head>
<body>
<center>
<form action="checklogin.jsp">
username : <input type="text" name="username" onblur="checkname() ;" /><span><div id="msg"></div></span><br><br>
password : <input type="password" name="password" /><br><br>
<input type="submit" value="submit" />
</form>
</center>
</body>
</html>
checklogin.jsp
<%@ page import="java.sql.*" %>
<%
response.setContentType("text/xml") ;
response.setCharacterEncoding("GB18030") ;
response.setHeader("catch-control", "no-store") ;
response.setDateHeader("Expires", 0) ;
String username = request.getParameter("username") ;
Connection conn = null ;
String login = "" ;
try{
Class.forName("com.mysql.jdbc.Driver") ;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testuser?user=root&password=110110") ;
Statement stmt = conn.createStatement() ;
String sql = "select count(*) from ruser where username = '" + username + "'" ;
System.out.println(sql) ;
ResultSet rs = stmt.executeQuery(sql) ;
rs.next() ;
int count = rs.getInt(1) ;
if(count > 0){
login = "validate" ;
}else{
login="novalidate" ;
}
}catch(ClassNotFoundException ex){
ex.printStackTrace() ;
}catch(SQLException ex){
ex.printStackTrace() ;
}
//System.out.println(request.getParameter("username")) ;
response.getWriter().write("<msg>" + login + "</msg>") ;
%>