用JSP实现WEB页面登录验证

<!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>

 

from: http://blog.sina.com.cn/s/blog_4cc54e95010009vh.html

posted @ 2017-12-02 23:14  ec04  阅读(3004)  评论(0编辑  收藏  举报