实现java web页面的登录验证

实现 java web 页面的登录验证

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

1.       sql2000 数据库中建立数据库 test..
Image000000.jpg

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /?><shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@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
 Image000001.jpg

3. 在表中增加数据
 Image000002.jpg

3.       建立数据源 test
 Image000003.jpg

 

Eclipse 开发环境

4. 新建项目
 Image000004.jpg

4.       新建 WEB 下面的 HTML 页面 index.html.
Image000005.jpg

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 >

 

界面如右: Image00006.jpg

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 浏览器.

 

Image000007.jpg10.   测试一下 ^_^

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

Image000008.jpg
点击登陆后跳为下页:Image000009.jpg

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值