login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=gbk" %>
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
<FORM action="test.jsp" method="post">
姓名:<INPUT type="text" name="username"><br>
密码:<INPUT type="password" name="password"><br>
<INPUT type="submit" value="提交"><br>
<INPUT type="reset" value="重置">
</FORM>
</body>
</html>
test.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=gbk"%>
<html>
<head>
<title>My JSP 'test.jsp' starting page</title>
</head>
<body>
<%
String name=request.getParameter("username");
String password=request.getParameter("password");
out.println(name);
out.println(password);
if("eclipse".equals(name) && "myeclipse".equals(password))
{
session.setAttribute("XXX","YYY");
response.sendRedirect("welcome.jsp");
}
else
{
%>
<h1>登录失败,请返回重新登陆</h1>
<a href="login.jsp">登陆</a>
<%
}
%>
</body>
</html>
welcome.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=gbk"%>
<html>
<body>
<%
if(session.getAttribute("XXX")!=null)
{
%>
<h1>欢迎光临本站!</h1>
<h2><A href="logout.jsp">注销</A></h2>
<%
}
else
{
%>
<h1>你还未登陆,请先登陆</h1>
<h2><a href="login.jsp">登陆 </a></h2>
<%
}
%>
</body>
</html>
logout.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=gbk" %>
<html>
<head>
<title>My JSP 'logout.jsp' starting page</title>
</head>
<body>
<%
session.invalidate();
%>
<a href="welcome.jsp">welcome.jsp</a>
</body>
</html>