<%--
Created by IntelliJ IDEA.
User: 何
Date: 2023/6/9
Time: 9:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>管理员首页</title>
</head>
<body>
<%
String admin = request.getParameter("admin"); //获取页面的数据
String pswd = request.getParameter("pswd");
if ("admin".equals(admin) && "admin".equals(pswd)) {
session.setAttribute("admin", admin);
session.setAttribute("pswd", pswd);
session.setMaxInactiveInterval(10*60); //设置有效时间
%>
<h1>管理员首页</h1>
<%
}else {
out.print("用户名或密码错误!");
response.setHeader("refresh","3;index.jsp");
}
%>
<%
String userName = (String) session.getAttribute("admin");
String mima = (String) session.getAttribute("pswd");
if (null==userName){
response.sendRedirect("index.jsp");
return;
}else {
out.print("欢迎你"+userName+"密码"+mima);
}
%>
</body>
</html>
用户登录
<%--
Created by IntelliJ IDEA.
User: 何
Date: 2023/6/9
Time: 9:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<script type="text/javascript">
function x() {
var ad = document.getElementById('num');
var el = document.getElementById('num2');
if (el){
el.value = '';
ad.value= '';
}
}
</script>
<body>
<form id="fromA" method="post" action="Admin.jsp">
用户名:<input type="text" name="admin" id="num"><br>
密码:<input type="password" name="pswd" id="num2"><br>
<input type="submit" value="登录">
<input type="button" value="取消" onclick="x()">
</form>
</body>
</html>
本文介绍如何在Java Servlet应用中利用Session技术来管理登录用户的会话状态,确保用户在登录后的操作安全性。

5233

被折叠的 条评论
为什么被折叠?



