編寫JSP頁面login1.jsp、server.jsp 和loginSuccessjsp.在頁面login 1.jsp 中輸入使用者名稱和密碼,單擊「提交」按鈕將輸入的資訊提交給頁面server.jsp。在server.jsp頁面中進行登入驗證:如果輸入正確(使用者名稱為「zhangsan",密碼為「123」, 提示「成功登入,3秒鐘後進入loginSuccess.jsp頁面」;如果輸入不正確,重新定向到login 1.jsp 頁面。執行login 1.jsp 頁面。
頁面執行效果如圖所示:
登入頁面login1.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>
<form action="server.jsp">
姓名:<input type="text" name="userName" value="">
<br>
密碼:<input type="password" name="pwd" value="">
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
登入中……server.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>
<%
String username=request.getParameter("userName");
String password=request.getParameter("pwd");
if("zhangsan".equals(username)&&"123".equals(password)){
out.print("成功登陸,3秒後進入loginSuccess.jsp頁面");
response.setHeader("refresh", "3;url=loginSuccess.jsp");
}else{
out.print("賬號或密碼錯誤,2秒後回到login1.jsp頁面");
response.setHeader("refresh", "2;url=login_1.jsp");
}
%>
</body>
</html>
loginSuccess.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>
<font color="green" align="center">歡迎張三登入成功!</font>
</body>
</html>