81,091
社区成员
发帖
与我相关
我的任务
分享
request.getSession().setAttribute(arg0, arg1); //放入键值对
request.getSession().getAttribute(arg0); //取出键值对
<%
String msg = "";
if (request.getMethod().equalsIgnoreCase("POST")) {
String un = request.getParameter("un");
String pw = request.getParameter("pw");
if (un.equalsIgnoreCase("用户名") && pw.equalsIgnoreCase("密码")) {
session.setAttribute("Login", un);
response.sendRedirect("index.jsp"); //成功的话转到index.jsp
return;
}
msg = "用户名或密码错误!"; //没成功的话打印错误信息
}
%>
<%@page contentType="text/html" pageEncoding="gb2312"%>
<!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=gb2312">
<title>title</title>
</head>
<body>
<h1>title</h1>
<form id="login" action="login.jsp" method="post">
<p><%=msg%></p>
<input type="text" name="un" />
<input type="password" name="pw" />
<input type="submit" value="LOGIN" />
</form>
</body>
</html>
<%
String un = (String) session.getAttribute("Login"); //直接用String类型就可以了
if (un == null) response.sendRedirect("login.jsp"); //顺便可以检查一下,如果username为null就返回login.jsp
%>