用户名密码登录并验证
< % @ 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> 登录界面< / title>
< style>
. box{
position: absolute;
top: 50 % ;
left: 50 % ;
transform: translate ( - 50 % , - 50 % ) ;
width: 400 px;
padding: 40 px;
background: rgba ( 0 , 0 , 0 , 0.8 ) ;
box- sizing: border- box;
box- shadow: 0 15 px 25 px rgba ( 0 , 0 , 0 , .5 ) ;
border- radius: 10 px;
}
. box h2{
margin: 0 0 30 px;
padding: 0 ;
color: #ffffff;
text- align: center;
}
. box . inputBox{
position: relative;
}
. box . inputBox input{
width: 100 % ;
padding: 10 px 0 ;
font- size: 16 px;
color: #ffffff;
letter- spacing: 1 px;
margin- bottom: 30 px;
border: none;
border- bottom: 1 px solid #ffffff;
outline: none;
background: transparent;
}
. box . inputBox label{
position: absolute;
top: 0 ;
left: 0 ;
letter- spacing: 1 px;
padding: 10 px 0 ;
font- size: 16 px;
color: #ffffff;
pointer- events: none;
transition: 1 s;
}
. box . inputBox input: focus~ label,
. box . inputBox input: valid~ label
{
top: - 18 px;
left: 0 ;
color: #03 a9f4;
font- size: 12 px;
}
. box input[ type= "submit" ] {
background: transparent;
border: none;
outline: none;
color: #ffffff;
background: #03 a9f4;
margin: 0 px 80 px;
padding: 10 px 30 px;
cursor: pointer;
border- radius: 15 px;
}
< / style>
< / head>
< body>
< div class = "box" >
< h2> Login< / h2>
< ! -- 创建供用户输入的 HTML 表单-- >
< form action= "check.jsp" method= "post" >
< div class = "inputBox" >
< input type= "text" name= "uname" required= "" >
< label> Username< / label>
< / div>
< div class = "inputBox" >
< input type= "password" name= "upwd" required= "" >
< label> Password< / label>
< / div>
< div align= "center" >
< input type= "submit" name= "" value= "提交" >
< / div>
< / form>
< / div>
< / body>
< / html>
显示 数据库端 check.jsp
< % @page import = "java.sql.PreparedStatement" % >
< % @page import = "java.sql.ResultSet" % >
< % @page import = "java.sql.SQLException" % >
< % @page import = "java.sql.DriverManager" % >
< % @page import = "java.sql.Connection" % >
< % @ 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>
< body>
< %
Connection con= null ;
PreparedStatement pstmt= null ;
ResultSet rs= null ;
try {
Class. forName ( "com.microsoft.sqlserver.jdbc.SQLServerDriver" ) ;
System. out. println ( "数据库驱动成功!" ) ;
String url= "jdbc:sqlserver://localhost:1433;databaseName=用户注册库" ;
String username= "sa" ;
String password= "1234567" ;
con= DriverManager. getConnection ( url, username, password) ;
if ( con!= null ) {
System. out. println ( "已创建连接!" ) ;
request. setCharacterEncoding ( "utf-8" ) ;
String name= request. getParameter ( "uname" ) ;
String pwd= request. getParameter ( "upwd" ) ;
String sql= "select count(*) from tb_user where username=? and password=?" ;
pstmt= con. prepareStatement ( sql) ;
pstmt. setString ( 1 , name) ;
pstmt. setString ( 2 , pwd) ;
rs= pstmt. executeQuery ( ) ;
int count= - 1 ;
if ( rs. next ( ) ) {
count= rs. getInt ( 1 ) ;
}
if ( count> 0 ) {
out. println ( "登陆成功!" ) ;
} else {
out. println ( "登陆失败!" ) ;
}
} else {
System. out. println ( "数据库连接失败!" ) ;
}
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} finally {
if ( rs!= null ) {
rs. close ( ) ;
}
if ( pstmt!= null ) {
pstmt. close ( ) ;
}
if ( con!= null ) {
con. close ( ) ;
}
}
% >
< / body>
< / html>
显示