骑猪闯天下

J2ME随笔,记录成长的脚步

统计

留言簿(3)

阅读排行榜

评论排行榜

[HTML-原创] Web页面实现登录验证 范例

Web页面实现登录验证

HTML入门的范例,一句一句敲入的代码,特此保留,以备查阅。
骑猪闯天下。

1. index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
  
<head>
    
<title>系统登录</title>
    
    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    
<meta http-equiv="description" content="this is my page">
    
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  
</head>
  
  
<body>
    版权所有:杜长风 
<br>
    发布时间:20080919
<br>
    
    
    
<center>
    
<h2>系统登录</h2>
    
<form action="login.jsp" method="post">
        
<input type="text" name="uid" maxlength=18 style="width:150"> <br>
        
<input tpye="password" name="upwd" maxlength=18 style="width:150"> <br>
        
<input type="submit" value="登录">
        
<input type="reset" value="取消">
    
</form>
    
</center>
  
</body>
  
</html>

2. login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>    


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
    
<title>验证页面</title>
    
    
<meta http-equiv="pragma" content="no-cache">
    
<meta http-equiv="cache-control" content="no-cache">
    
<meta http-equiv="expires" content="0">    
    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    
<meta http-equiv="description" content="This is my page">
    
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    
-->

  
</head>
  
  
<body> 
    
<%
    
String username = request.getParameter("uid");
    
String password = request.getParameter("upwd");
    
if (username != null && !username.equals(""))
    {
        try{
            
//连接数据库
            Class.forName(
"com.mysql.jdbc.Driver"); //注意这里的连接可能有问题
            Connection conn 
= DriverManager.getConnection("jdbc:mysql://localhost/test""root""admin");
            Statement stmt 
= conn.createStatement();
            
            
String sql = "select * from user where username='" + username + "'";
            sql 
+= "and password='" + password + "'";
            
            ResultSet result 
= stmt.executeQuery(sql);
            
if(result.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>

3. main.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
    
<title>主页面</title>
    
    
<meta http-equiv="pragma" content="no-cache">
    
<meta http-equiv="cache-control" content="no-cache">
    
<meta http-equiv="expires" content="0">    
    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    
<meta http-equiv="description" content="This is my page">
    
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    
-->

  
</head>
  
  
<body>
    
<!-- 包含近来验证身份页面的源代码 -->
    
<%@include file="checkvalid.jsp"%>
        欢迎进入本页面,您已经通过验证,你的用户名是:
<%=session.getAttribute("uname")%><p>
            
<href="continue.jsp"> 你可以跳转到后续界面</a>
  
</body>
</html>

 

4. checkvalid.jsp

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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
    
<title>验证页面</title>
    
    
<meta http-equiv="pragma" content="no-cache">
    
<meta http-equiv="cache-control" content="no-cache">
    
<meta http-equiv="expires" content="0">    
    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    
<meta http-equiv="description" content="This is my page">
    
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    
-->

  
</head>
  
  
<body>
    This is my JSP page. 
<br>
    
<%
    
if(session.getAttribute("login"== null || !session.getAttribute("login").equals("ok"))
    {
        
//检查未通过,跳转回登录界面
        response.sendRedirect(
"index.html");
    }
     
%>
  
</body>
</html>

5. continue.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
    
<title>Insert title here</title>
    
    
<meta http-equiv="pragma" content="no-cache">
    
<meta http-equiv="cache-control" content="no-cache">
    
<meta http-equiv="expires" content="0">    
    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    
<meta http-equiv="description" content="This is my page">
    
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    
-->

  
</head>
  
  
<body>
      
<center>
    
<%@include file="checkvalid.jsp" %>
        
<%=session.getAttribute("uname"%>,欢迎您进入第二个页面!<br>
        
<% out.println("<a href=index.html>返回</a>"); %>
    
</center>
  
</body>
</html>

posted on 2008-09-20 14:23 骑猪闯天下 阅读(2267) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: