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>

登录页面
<br>
<%
//销毁
session.invalidate();

%>

<form action="testPW.jsp" method="post">

用户名:<input type="text" name="username"> 
密码: <input type="password" name="password">
<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>
<%

//检查session,   取得session信息

Object obj = session.getAttribute("username");
if(obj!=null)
{
    out.print("欢迎登录"+obj.toString());
}
else
{
    out.print("会话超时,请重新登录系统");
    
    response.setHeader("refresh", "3;URL=login.jsp ");
}
%>

主页面

<br>
<a href="login.jsp">退出登录</a>
</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>

<%

//验证用户登录信息是否正确

String un = request.getParameter("username");
String pw = request.getParameter("password");

if(un!=null&&pw!=null)
{
    //如果正确,就创建session,并跳转到main.jsp
    if(un.equals("tom")&&pw.equals("123"))
    {
        //记录用户信息
        session.setAttribute("username", un);
        //跳转到系统主页面
        response.sendRedirect("main.jsp");
    }
    else
    {
        //否则就提示密码错误
        out.print("用户名或密码错误");
    }
    
}
else
{
    out.print("请以正常的方式访问系统");
}








%>

</body>
</html>

 

 

账户或者密码错误的时候

 

跳转别的页面,并且回到主页面,销毁原来的数据

posted @ 2016-06-29 11:07  ╄承诺、带给的伤痛—  阅读(189)  评论(0编辑  收藏  举报