jsp内置对象2

四、session

应用session实现用户登录:

login.jsp

<body>
<form action="index.jsp" name="form1" method="post">
用户名:<input type="text" name="user" id="user" style="width: 120px"><br>
密码:<input type="password" name="pwd" id="pwd" style="width:120px;"><br>
<input type="submit" name="submit" value="登录">
</form>
</body>
</html>

index.jsp

<body>
<%
String[][] users={{"zhangsan","123"},{"lisi","123"},{"wanger","123"}};
String user=request.getParameter("user");
String pwd=request.getParameter("pwd");
boolean flag=false;
for(int i=0;i<users.length;i++)
{
    if(users[i][0].equalsIgnoreCase(user))
    {
        if(users[i][1].equalsIgnoreCase(pwd))
        {
        flag=true;
        break;
        }
    }
    }
if(flag)
{
    session.setAttribute("username", user);
    response.sendRedirect("main.jsp");
    }
else
{
    response.sendRedirect("login.jsp");
}
%>
</body>

main.jsp

<body>
<%
String name=(String)session.getAttribute("username");
%>
欢迎光临:<%=name %>
<a href="exit.jsp">[退出]</a>

 exit.jsp

<%
session.invalidate();
response.sendRedirect("login.jsp");
%>

  

posted @ 2017-02-26 11:39  叮咚叮咚126  阅读(191)  评论(0编辑  收藏  举报