JSP简单登录模块使用Session

JSP Login.jsp
<%@ page contentType="text/html;charset=GB2312" %>

<html>
<head>
<title>CH5 - Login.jsp</title>
</head>
<body>

<h2>javax.servlet.http.HttpSession - session 对象</h2>
<form action=Login.jsp method="POST" >
Login Name: <input type="text" name="Name"><br>
Login Password: <input type="text" name="Password" ><br>
<input type="submit" value="Send"><br>
<form>

<% if (request.getParameter("Name") != null && request.getParameter("Password") != null) {
String Name = request.getParameter("Name");
String Password = request.getParameter("Password");

if (Name.equals("mike") && Password.equals("1234")) {
session.setAttribute("Login", "OK");
response.sendRedirect("Member.jsp");
}
else {
out.println("登录错误,请输入正确名称");
}
}
%>

</body>
</html>
JSP Member.jsp
<%@ page contentType="text/html;charset=GB2312" %>

<html>
<head>
<title>CH5 - Member.jsp</title>
</head>
<body>

<h2>javax.servlet.http.HttpSession - session 对象</h2>
<%
String Login = (String)session.getAttribute("Login");

if (Login != null && Login.equals("OK")) {
out.println("欢迎进入");
session.invalidate();
}
else {
out.println("请先登录,谢谢") ;
out.println("<br>经过五秒之后,网页会自动返回Login.jsp");

response.setHeader("Refresh","5;URL=Login.jsp");
}
%>

</body>
</html>

你可能感兴趣的:(html,jsp,servlet)