需求
通过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" ; //创建核心xmlhttprequest组件 xmlhttp = createxmlhttprequest(); //设置回调函数 xmlhttp.onreadystatechange = proessrequest; //初始化核心组件 xmlhttp.open( "post" , url, true ); //设置请求头 xmlhttp.setrequestheader( "content-type" , "application/x-www-form-urlencoded;" ); //发送请求 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