session在网罗中被称为会话,由于HTTP协议是一种无状态协议,也就是当一个客户向服务器发出请求时,服务器接受请求并返回响应后,该连接结束,而服务器并不保存相关的信息。所以HTTP提供了session。通过session可以在应用程序的web页间进行跳转,并保存用户的状态,使会话存在下去,直到关闭浏览器。
通过session对象可以存储或读取客户相关的信息,如用户名,购物信息。这些可以通过session对象的setAttribute()和getAttribute()方法实现
setAttribute()方法
该方法用于将信息保存
在session范围内
session.setAttribute(String name,Object obj)
例如,将用户名“路明非”保存到session范围内的username变量中
session.setAttribute(“username”,“路明非”);
getAttribute()方法
该方法用于获取
保存在session范围内的信息
getAttribute(String name)
例如,读取保存到session范围内的username变量的值
session.getAttribute(“username”)
创建index.jsp文件,添加用于收集用户登录信息的表单
<%@ 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>
<form name="form" method="post" action="deal.jsp">
用户名:<input name="name" type="text" id="name" style="width:120px"><br>
密 码<input name="pwd" type="password" id="pwd" style="width:120px"><br>
<br>
<input type="submit" name="Submit" value="提交">
</form>
</body>
</html>
编写deal.jsp文件,模拟用户登录,如登录成功,将用户名保存到session范围内的变量中,并将页面重定向到main.jsp页面,否则,重定向index.jsp页面,重新登录
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<!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[][] userList={{"mr","mrsoft"},{"wgh","111"},{"zj","loe"}}; //定义一个保存用户列表的二维数组
boolean flag=false; //登录状态
request.setCharacterEncoding("UTF-8"); //设置编码
String username=request.getParameter("name"); //获取用户名
String password=request.getParameter("pwd"); //获取密码
for(int i=0;i<userList.length;i++){ //遍历二维数组
if(userList[i][0].equals(username)){ //判断用户名
if(userList[i][1].equals(password)){ //判断密码
flag=true; //登录成功
break; //跳出循环
}
}
}
if(flag){ //如果值为true,表示登录成功
session.setAttribute("username",username); //保存用户名到session范围的变量中
response.sendRedirect("main.jsp"); //跳转到主页
}else{
response.sendRedirect("index.jsp"); //跳转到用户登录页面
}
%>
</body>
</html>
编写main.jsp文件,首先获取并显示保存到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>
<%
String username=(String)session.getAttribute("username"); //获取保存在session范围内的用户名
%>
您好!<%=username %>欢迎访问!!!<br>
<a href="exit.jsp">[退出]</a>
</body>
</html>
编写exit.jsp文件,销毁session,并重定向页面到index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
session.invalidate(); //销毁session
response.sendRedirect("index.jsp"); //重定向页面到index.jsp
%>
<!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>
</body>
</html>