session保存用户登录

Posted 凌零聆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了session保存用户登录相关的知识,希望对你有一定的参考价值。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
session.invalidate();
%>
<form action="yanzheng.jsp" method="post">
用户名:<input type="text" name="yonghu"> 
密码:<input type="password" name="mima">
<input type="submit" value="登录">

</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
try{
String yonghu=new String(request.getParameter("yonghu").getBytes("ISO-8859-1"),"UTF-8");
String mima=request.getParameter("mima");


    if(yonghu.equals("小小")&& mima.equals("123"))
    {
        session.setAttribute("yonghu", yonghu);
        response.sendRedirect("zhuye.jsp");
        
    }
    else
    {
        
        out.print("用户名或密码错误,请重新输入");
        response.setHeader("refresh", "3;URL=denglu.jsp");
    }


}
catch(Exception e)
{
out.print("请从登录界面登录");
response.setHeader("refresh", "3;URL=denglu.jsp");
}
%>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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-80">
<title>Insert title here</title>
</head>
<body>
<%

Object obj=session.getAttribute("yonghu");
if(obj==null)
{
out.print("请先登录");
response.setHeader("refresh","3;URL=denglu.jsp");
}
else
{
    String a=new String(obj.toString());
    out.print("欢迎&nbsp;"+a+"&nbsp;登录"); 
%>
<br>
<a href="denglu.jsp"><input type="button" value="退出登录"></a>
<%
} %> 

</body> </html>

以上是关于session保存用户登录的主要内容,如果未能解决你的问题,请参考以下文章

在c#的mvc4中,用httpcontext.user还是session保存用户登录信息?

django使用session来保存用户登录状态

SSH框架z中登录功能如何将用户保存在session

session保存用户登录

session保存用户登录

django session为啥不会重新登录