編寫3個JSP頁面:login.jsp、validate.jsp和success.jsp,login.jsp輸入用戶名與密碼信息,提交給validate.jsp進行用戶驗證,如果驗證爲合法用戶(用戶名:tom,密碼:jenny)則轉到(forward動作標記)success.jsp頁面,否則轉到login.jsp頁面重新登錄。
(1) login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="validate.jsp">
用戶名:<input type="text" name="name"><br>
密碼:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
(2) validate.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name=request.getParameter("name");
String password=request.getParameter("password");
if("tom".equals(name)&&"jenny".equals(password)){
%>
<jsp:forward page="success.jsp"/>
<%
}else{
%>
}<jsp:forward page="login.jsp"/>
<%
}
%>
</body>
</html>
(3) success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
登錄成功!
</body>
</html>