----------------------------------------------------------------------------------index.jsp----------------------------------------------------------------------------------
<body>
<form action="check.jsp" method="get">
<p>账户:<input type="text" name="userName"/></p>
<p>密码:<input type="password" name="userPwd"/></p>
<p><input type="submit" value="登录"></p>
</form>
</body>
----------------------------------------------------------------------------------check.jsp----------------------------------------------------------------------------------
<body>
<%
request.setCharacterEncoding("UTF-8");
String userName = request.getParameter("userName");
session.setAttribute("userName", userName);
if(userName == null || userName.equals("")){userName = (String)session.getAttribute("userName");}
if(userName == null || userName.equals("")) {
%>
<script type="text/javascript">
window.onload = function () {
document.querySelector("span").innerText = "您尚未登录!";
function backIndex() {
window.location = "index.jsp";
}
setTimeout(backIndex, 2000);
}
</script>
<%
}
%>
<h1>hello,<span><%=userName %></span></h1>
<p><a href="blog.jsp">我的博客</a></p>
<p><a href="logout.jsp">退出登录</a></p>
</body>
----------------------------------------------------------------------------------blog.jsp----------------------------------------------------------------------------------
<body>
<%
request.setCharacterEncoding("UTF-8");
String userName = (String)session.getAttribute("userName");
if(userName == null || userName.equals("")) {
%>
<script type="text/javascript">
window.onload = function () {
document.querySelector("span").innerText = "您尚未登录!";
function backIndex() {
window.location = "index.jsp";
}
setTimeout(backIndex, 2000);
}
</script>
<%
}
%>
<h1>hello,<span><%=userName %></span></h1>
<p><a href="check.jsp">返回主页面</a></p>
</body>
----------------------------------------------------------------------------------logout.jsp----------------------------------------------------------------------------------
<body>
<%
session.removeAttribute("userName");
%>
<jsp:forward page="index.jsp"></jsp:forward>
</body>