jsp实现简单的session 跟踪

来源:互联网 发布:ipad淘宝怎么退出登录 编辑:程序博客网 时间:2024/05/18 23:12
在用户身份验证通过后设定一个session属性,然后调用hello.jsp网页,在hello.jsp中验证session属性,如果该属性不存在或不正确,则给出非法用户提示并链接返回主页。包含index.html  aaa.jsp   hello.jsp Errorpage.jsp
//index.html  参见jsp的index.html
//aaa.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="Errorpage.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

 

<%
request.setCharacterEncoding("GBK");
String Username="";
String Userpwd="";
Username =(String) request.getParameter("userid");
Userpwd =(String) request.getParameter("userpwd");
String id[]=new String[5],pwd[]=new String[5];
id[1]="llx";       pwd[1]="1234567890";
id[2]="kf";   pwd[2]="0123456789";
id[3]="lb";    pwd[3]="0987654321";
id[4]="laoshi";    pwd[4]="9876543210";
id[0]="dd";  pwd[0]="1231231231";
int i;
session.setAttribute("login","false");
for(i=0;i<5;i++)
 {
  if(Username.trim().equals(id[i])&&Userpwd.trim().equals(pwd[i]))
   //response.sendRedirect("http://www.baidu.com");
  { session.setAttribute("login","true");
    session.setAttribute("Username",Username);
  break;}
 } %>
 <%
 if(i>=5) response.sendRedirect("Errorpage.jsp");
 else{ %>
用户登录成功!<br  />
<a href="hello.jsp"> 会话验证页面</a><br />
<% } %>
</html>
 

原创粉丝点击