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>