实现java web页面的登录验证

实现 java web 页面的登录验证

本案例中的程序主要通过 java jdbc-odbc 驱动连接 sql2000 数据库 , 并依据数据库中的用户表信息验证客户端登录请求提交的用户名和密码 .

1.       sql2000 数据库中建立数据库 test..
实现java web页面的登录验证

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /?><shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="[email protected]@[email protected]@[email protected]@[email protected]@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><lock aspectratio="t" v:ext="edit"></lock></shapetype>

2.       test 数据库中建表 userid
 实现java web页面的登录验证

3. 在表中增加数据
 实现java web页面的登录验证

3.       建立数据源 test
 实现java web页面的登录验证

 

Eclipse 开发环境

4. 新建项目
 实现java web页面的登录验证

4.       新建 WEB 下面的 HTML 页面 index.html.
实现java web页面的登录验证

5.       写入代码 :

<! DOCTYPE HTML PUBLIC "-//W<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /?><chmetcnv w:st="on" unitname="C" sourcevalue="3" hasspace="False" negative="False" numbertype="1" tcsc="0">3C</chmetcnv>//DTD HTML 4.01 Transitional//EN" >

< html >

< head >

< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >

< title > 系统登录 </ title >

</ head >

< body >

    < center >    

    < h2 > 系统登录 </ h2 >    

    < form action = "login.jsp" method = "post" >

        < Input type = "text" name = "uid" maxlength = 8 style = "width:150" >< br >

        < Input type = "password" name = "upwd" maxlength = 8 style = "width:150" >< br >

        < Input type = "submit" value = " 登陆" >

        < Input type = "reset" value = " 取消" >

    </ form >

    </ center >

</ body >

</ html >

 

界面如右: 实现java web页面的登录验证

6.       新建 jsp 文件 login.jsp.

<%@ page language = "java" contentType = "text/html; charset=UTF-8" pageEncoding = "UTF-8" %>

<%@ page import = "java.sql.*" %>

<! DOCTYPE HTML PUBLIC "-//W<chmetcnv w:st="on" unitname="C" sourcevalue="3" hasspace="False" negative="False" numbertype="1" tcsc="0">3C</chmetcnv>//DTD HTML 4.01 Transitional//EN" >

< html >

< head >

< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >

< title > 验证页面 </ title >

</ head >

< body >

<%

String username = request.getParameter( "uid" );

String password = request.getParameter( "upwd" );

if (username != null && !username.equals( "" )){

try {

    /*

     * 连接数据库

     */

        Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

        Connection con=DriverManager.getConnection( "jdbc:odbc:test" , "" , "" );

        Statement stmt =con.createStatement();

 

 

    String sql = "select * from userid where name='" + username + "'" ;

    sql += " and psw='" + password + "'" ;  // 准备查询语句

    ResultSet rs=stmt.executeQuery( sql );

    if ( rs.next() ){

    session.setAttribute( "login" , "ok" ); // 验证通过之后,跳转到后续页面

    session.setAttribute( "uname" ,username);

%>

        < jsp:forward page = "main.jsp" />

<%

    } else

         out.println( " 错误的用户名和密码" );  // 验证未通过,显示错误信息

    out.println( "<a href=index.html> 返回</a>" );

    } catch (Exception ee){

        ee.printStackTrace();

    }

} else {

    out.println( " 请先登录!" );  // 验证未通过,显示错误信息

    out.println( "<a href=index.html> 返回</a>" );

}

%>

</ body >

</ html >

7.       新建 checkvalid.jsp

<%@ page language = "java" contentType = "text/html; charset=UTF-8" pageEncoding = "UTF-8" %>

<! DOCTYPE HTML PUBLIC "-//W<chmetcnv w:st="on" unitname="C" sourcevalue="3" hasspace="False" negative="False" numbertype="1" tcsc="0">3C</chmetcnv>//DTD HTML 4.01 Transitional//EN" >

< html >

< head >

< title > 验证页面 </ title >

</ head >

< body >

<%

    if (session.getAttribute( "login" )== null || !session.getAttribute( "login" ).equals( "ok" )) {

        response.sendRedirect( "index.html" );  // 验证没有通过

    }      

%>

</ body >

</ html >

 

8.     新建main.jsp

<%@ page language = "java" contentType = "text/html; charset=UTF-8" pageEncoding = "UTF-8" %>

<! DOCTYPE HTML PUBLIC "-//W<chmetcnv w:st="on" unitname="C" sourcevalue="3" hasspace="False" negative="False" numbertype="1" tcsc="0">3C</chmetcnv>//DTD HTML 4.01 Transitional//EN" >

< html >

< head >

< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >

< title > 主页面 </ title >

</ head >

< body >

<%@ include file = "checkvalid.jsp" %>

        欢迎进入本页面,您已经通过验证,你的用户名是 <%= session.getAttribute( "uname" ) %> < p >

        < A HREF = "continue.jsp" > 您可以跳转到后续页面 </ A >  

</ body >

</ html >

9.     新建continue.jsp

 

<%@ page language = "java" contentType = "text/html; charset=UTF-8" pageEncoding = "UTF-8" %>

<! DOCTYPE HTML PUBLIC "-//W<chmetcnv w:st="on" unitname="C" sourcevalue="3" hasspace="False" negative="False" numbertype="1" tcsc="0">3C</chmetcnv>//DTD HTML 4.01 Transitional//EN" >

< html >

< head >

< meta http-equiv = "Content-Type" content = "text/html; charsetUTF-8" >

< title > Insert title here </ title >

</ head >

< body >

<%@ include file = "checkvalid.jsp" %>

        <%= session.getAttribute( "uname" ) %> , 欢迎您进入第二个页面!

</ body >

</ html >

10. 首先在 Eclipse 中启动 Tomcat 应用服务器 , 然后启动 IE 浏览器.

 

实现java web页面的登录验证10.   测试一下 ^_^

先输入用户名,再输入密码,当然只有在 sql2000 中有的用户才是有较用户!

实现java web页面的登录验证
点击登陆后跳为下页:实现java web页面的登录验证