需求
通過ajax異步刷新頁面驗證用戶輸入的賬號密碼是否在數據庫中存在。
技術棧
jsp+servlet+oracle
具體代碼
jsp部分:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <!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> </head> <script> function createxmlhttprequest() { try { xmlhttp = new xmlhttprequest(); //除了ie之外的其他瀏覽器使用ajax } catch (tryms) { try { xmlhttp = new activexobject( "msxml2.xmlhttp" ); //ie瀏覽器適配 } catch (otherms) { try { xmlhttp = new activexobject( "microsoft.xmlhttp" ); //ie瀏覽器適配 } catch (failed) { xmlhttp = null ; } } } return xmlhttp; } //提交請求 var xmlhttp; function checkuserexists() { var u = document.getelementbyid( "uname" ); var username = u.value; if (username == "" ) { alert( "請輸入用戶名" ); u.focus(); return false ; } //訪問字符串 var url = "loginservlet" ; //創(chuàng)建核心xmlhttprequest組件 xmlhttp = createxmlhttprequest(); //設置回調函數 xmlhttp.onreadystatechange = proessrequest; //初始化核心組件 xmlhttp.open( "post" , url, true ); //設置請求頭 xmlhttp.setrequestheader( "content-type" , "application/x-www-form-urlencoded;" ); //發(fā)送請求 xmlhttp.send( "uname=" +username); } //回調函數 function proessrequest() { if (xmlhttp.status== 200 && xmlhttp.readystate == 4 ) { var b = xmlhttp.responsetext; //得到服務端的輸出結果 if (b== "true" ) { document.getelementbyid( "alert" ).innerhtml = "<font color='red'>用戶名已經存在!</font>" ; } else { document.getelementbyid( "alert" ).innerhtml = "<font color='blue'>用戶名可以使用!</font>" ; } } } </script> <body> 請輸入用戶名: <input id= "uname" name= "uname" type= "text" onblur= "checkuserexists()" /><div id= "alert" style= "display:inline" ></div> </body> </html> |
這里沒有用dao層,直接用servlet和service層進行驗證。
下面是service下jdbc查詢的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import java.sql.connection; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import com.stx.service.user; import com.stx.service.connectionmanager; public class ajaxservice { public boolean searchuser (string uname) { //jdbc查詢用戶名是否存在 boolean isfalse = false ; connection connection = null ; statement stmt = null ; resultset rs = null ; connection = connectionmanager.getconnection(); try { stmt = connection.createstatement(); string sql = "select * from user_b where uname='" +uname+ "'" ; //sql語句 rs = stmt.executequery(sql); isfalse=rs.next(); } catch (sqlexception e) { e.printstacktrace(); } finally { connectionmanager.closeresultset(rs); connectionmanager.closestatement(stmt); connectionmanager.closeconnection(connection); } return isfalse; } } |
jdbc連接代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public class connectionmanager { private final static string driver_class = "oracle.jdbc.oracledriver" ; private final static string url = "jdbc:oracle:thin:@localhost:1521:orcl" ; private final static string dbname = "ibook" ; private final static string password = "qwer" ; public static connection getconnection() { connection connection = null ; try { class .forname(driver_class); connection = drivermanager.getconnection(url, dbname, password); } catch (classnotfoundexception e) { e.printstacktrace(); } catch (sqlexception e) { e.printstacktrace(); } return connection; } public static void closeresultset(resultset rs) { try { if (rs != null ) rs.close(); } catch (sqlexception e) { e.printstacktrace(); } } public static void closeconnection(connection connection) { try { if (connection != null && !connection.isclosed()) connection.close(); } catch (sqlexception e) { e.printstacktrace(); } } public static void closestatement(statement stmt) { try { if (stmt != null ) stmt.close(); } catch (sqlexception e) { e.printstacktrace(); } } } |
關于user類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class user { private string uname; public user() { super (); } public user(string uname) { super (); this .uname = uname; } public string getuname() { return uname; } public void setuname(string uname) { this .uname = uname; } |
關于控制層servlet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
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; import com.stx.service.ajaxservice; /** * servlet implementation class loginservlet */ public class loginservlet extends httpservlet { private static final long serialversionuid = 1l; private ajaxservice ajaxservice = new ajaxservice(); /** * @see httpservlet#httpservlet() */ public loginservlet() { super (); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.setcharacterencoding( "utf-8" ); string uname = request.getparameter( "uname" ); //獲取到輸入的用戶名 boolean isuname = ajaxservice.searchuser(uname); //調用service中的查詢方法 response.setcharacterencoding( "utf-8" ); //設置字符編碼 printwriter out = response.getwriter(); out.print(isuname); out.flush(); out.close(); //關閉資源 } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000015150083