用JSP實現WEB頁面登入驗證

來源:互聯網
上載者:User
1.index.html
<!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>
</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>

2.login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.* "%>
<!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>
</head>
<body>

<%
 String user_name= request.getParameter("uid");
 String pass_word = request.getParameter("upwd");
 if(user_name!=null && !user_name.equals("")){
 
 try{
             Class.forName("org.gjt.mm.mysql.Driver");
             Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/test","test","test");
             Statement stmt=(Statement)conn.createStatement();
             String sql="select * from account where username='"+user_name+"'";
             sql +="and password='"+pass_word+"'";
             ResultSet rs=(ResultSet)stmt.executeQuery(sql);
             if(rs.next())
                  {
                  session.setAttribute("login","ok");
                  session.setAttribute("uname",user_name);
%>
   <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" 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>
</head>
<body>

<%@include file="checkvalid.jsp"%>
歡迎進入本頁面,您已經通過了驗證,你的使用者名稱是:<%=session.getAttribute("uname") %><p>

 
<a href=continue.jsp>您可以跳轉到後續頁面</a>
 

</body>
</html>

4.checkvalid.jsp

<%@ 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>
</head>
<body>

<%
  if(session.getAttribute("login")==null||!session.getAttribute("login").equals("ok"))
  {response.sendRedirect("index.html");}

 %>
</body>
</html>

5.continue.jsp

<%@ 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>
</head>
<body>
<%@include file="checkvalid.jsp"%>
<%=session.getAttribute("uname") %>,歡迎您進入第二個頁面!<p>

</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.