session相关知识点与操作

session.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Session</title>
	<base href="http://localhost:8080/cookie_session/">
<style type="text/css">

	ul li {
		list-style: none;
	}
	
</style>
</head>
<body>
	<iframe name="target" width="500" height="500" style="float: left;"></iframe>
	<div style="float: left;">
		<ul>
			<li><a href="http://localhost:8080/cookie_session/session?action=createOrGetSession" target="target">Session的创建和获取(id号、是否为新创建)</a></li>
			<li><a href="session?action=setAttribute" target="target">Session域数据的存储</a></li>
			<li><a href="session?action=getAttribute" target="target">Session域数据的获取</a></li>
			<li>Session的存活</li>
			<li>
				<ul>
					<li><a href="session?action=defaultLife" target="target">Session的默认超时及配置</a></li>
					<li><a href="session?action=life3" target="target">Session3秒超时销毁</a></li>
					<li><a href="session?action=deleteNow" target="target">Session马上销毁</a></li>
				</ul>
			</li>
			<li><a href="" target="target">浏览器和Session绑定的原理</a></li>
		</ul>
	</div>
</body>
</html>

servlet程序

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/*
Session会话
1).什么是session会话
1.Session就是一个接口、(HttpSession)
2.Session就是会话,他是用来维护一个客户端和服务器之间关联的一种技术
3.每个客户端都有自己的一个session会话
4.session中,我们经常用来保存用户登录之后的信息
如何创建和获取session。他们的API是一样的
request.getSession
第一次调用是:创建Session会话
之后调用都是:获取前面创建好的Session会话对象
isNew():判断到底是不是刚创建出来的
true表示刚创建
false表示获取之前创建
每个会话都有一个身份证号,也就是id值,而且这个id是唯一的
getid()得到session的会话id值
 */
public class sessionServlet extends BaseServlet {

        protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //创建和获取session会话对象
            HttpSession session = req.getSession();
            //判断当前session会话,是否是新建出来的
            boolean isnew=session.isNew();
            //获取session会话的唯一标识 id
            String id = session.getId();
            resp.getWriter().write("得到的Session,他的id是"+id+"<br/>");
            resp.getWriter().write("这个Session是否是新创建的:"+isnew);
            System.out.println("hi");
        }


    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //让session会话马上超时
        HttpSession session = req.getSession();
        session.invalidate();
        resp.getWriter().write("session已经设置为超时无效");
    }
    protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //先获取session对象
        HttpSession session = req.getSession();
        session.setMaxInactiveInterval(3);
        resp.getWriter().write("当前 session已经设置为3秒后超时");
    }
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取session的默认超时时长
        int maxInactiveInterval = req.getSession().getMaxInactiveInterval();
        resp.getWriter().write("session的默认时长为:"+maxInactiveInterval+"秒");

    }
    //获取value中的数据
    protected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object key1 = req.getSession().getAttribute("key1");
        resp.getWriter().write("从Session中获取出key1的数据是:"+key1);

    }
    //在session中保存数据
    protected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.getSession().setAttribute("key1","value1");
            resp.getWriter().write("已经存好了数据");
    }
}

配置web.xml

<servlet>
        <servlet-name>sessionServlet</servlet-name>
        <servlet-class>com.atshangqiu.servlet.sessionServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>sessionServlet</servlet-name>
        <url-pattern>/session</url-pattern>
    </servlet-mapping>

baseservlet

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public abstract class BaseServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决post请求中文乱码问题
        // 一定要在获取请求参数之前调用才有效
        req.setCharacterEncoding("UTF-8");
        // 解决响应中文乱码问题
        resp.setContentType("text/html; charset=UTF-8");
        String action = req.getParameter("action");
        try {
            // 获取action业务鉴别字符串,获取相应的业务 方法反射对象
            Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
//            System.out.println(method);
            // 调用目标业务 方法
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值