ch6.JSP内置对象--session对象

session对象

session对象是 javax.servlet.http.HttpSession 类的实例。和Java Servlets中的session对象有一样的行为。

session对象用来跟踪在各个客户端请求间的会话。


主要方法:



每个用户都是一个不同的session,服务器通过session id区分不同的用户,用

<%
	String id = session.getId();
%>
查看Id
在Cookie中自动设置的那个cookie就是用户的session id,所以,session在操作时使用了cookiede 处理机制,如果服务器重启,session id要重新分配

如果要保留,就要使用序列化机制


登陆和注销

这是session用的最多的地方

session 在所有项目中用的最多的地方就是登陆验证以及注销

测试,登陆页面(根据之前的登陆页面改进):

login.jsp

<%@ page contentType = "text/html" pageEncoding = "GBK"%>
<%@ page import = "java.sql.*"%>
<html>
<head><title>www.thystar.com</title></head>
<body>
<center>
		<h1>登陆操作</h1>
		<hr>
		<form action = "login.jsp" method = "post">
			<table border = "1">
			<tr>
				<td colspan="2">用户登录</td>
			</tr>
			<tr>
				<td>登陆ID</td>
				<td><input type = "text" name = "id"></td>
			</tr>
			<tr>
				<td>登陆密码</td>
				<td><input type = "password" name = "password"></td>
			</tr>
			<tr>
				<td colspan = "2">
					<input type = "submit" value = "登陆">
					<input type = "reset" value = "重置">
				</td>
			</tr>
			</table>
		</form>
<%!  //连接数据库
	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
	public static final String DBURL = "jdbc:mysql://localhost:3306/usr";
	public static final String DBUSER = "root";
	public static final String DBPASS = "mysqladmin";
%>
<%  //初始化连接对象
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	boolean flag = false;
	String name = null;         //接受用户姓名
%>
<%  //操作数据库,查找用户名和密码
	try{
		Class.forName(DBDRIVER);
		conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
		String sql = "SELECT name FROM login WHERE userid = ? AND password = ?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, request.getParameter("id"));
		pstmt.setString(2, request.getParameter("password"));
		rs = pstmt.executeQuery();
		if(rs.next()){
			flag = true;
			name = rs.getString(1);	
		}

	}catch(Exception e){
		System.out.println(e);
	}
	finally{
		try{
			rs.close() ;
			pstmt.close() ;
			conn.close() ;
		} catch(Exception e){}
	}
%>
<%  
	if(flag){
		response.setHeader("refresh","2; URL = welcome.jsp"); //定时跳转
		session.setAttribute("userid", name); //将登录的用户名保存
%>
		<h3>用户登录成功,两秒后跳转</h3>
		<h3>如果没有跳转,请按这里</h3>
<%	
	}
	}else{
%>
		<h3>登录错误</h3>
<%	
	}
%>	
</body>
</html>

welcome.jsp

<%@ page contentType = "text/html" pageEncoding = "GBK"%>
<html>
<head><title>www.thystar.com</title></head>
<body>
<%
	if(session.getAttribute("userid")!=null){
%>
		<h3>欢迎<%=session.getAttribute("userid")%>, <a href = "logout.jsp">注销</a></h3>
<%
	}else{
%>
		<h3>请先<a href = "login.jsp">登录</a></h3>
<%
	}
%>
</body>
</html>

logout.jsp

<%@ page contentType = "text/html" pageEncoding = "GBK"%>
<html>
<head><title>www.thystar.com</title></head>
<body>
<%
	response.setHeader("refresh","2; URL=login.jsp");
	session.invalidate();
%>
<h3>已退出本系统,2秒后跳转</h3>
<h3>如果没有跳转,按<a href = "login.jsp">这里</h3>
</body>
</html>

但是这个页面是有问题的,就是“登陆错误”这个信息一直会出现,直到你输入正确的信息跳转。暂时不会解决,再往后学看看。

会话跟踪有一下四种技术:

  1. 通过session提供的方法保存
  2. 通过Cookie
  3. 通过表单的隐藏域完成
  4. 通过地址重写



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值