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>

posted @ 2016-06-28 19:11  凌零聆  阅读(807)  评论(1编辑  收藏  举报