博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpSession之学习笔记
阅读量:7010 次
发布时间:2019-06-28

本文共 2610 字,大约阅读时间需要 8 分钟。

hot3.png

注:此篇博文是本人看国外官方文档得来的,建议读者阅读。

1.client-server connection

先上一张图,如下

                                     图1

对图1的说明:

  • client与server建立一个连接,这种连接是底层的
  • client发送request到server,等待server的answer
  • server处理request,将处理结果返还给client,这个结果包括status code、其它data

在HTTP/1.1中,在步骤3执行完成后,connection不再被关闭,在connection有效的前提细,后面client不再需要执行步骤1,直接执行步骤2、3就可以。

为了进一步深入,如下图2,图2是我从国外的网上截下来的,建议读者阅读:

                                                图2 HttpSession生成后会有个sessionID

  • Client第一次发送请求,web container生成唯一的session ID(生成session ID的源码,如有兴趣,可以看下tomcat源码),并将其返回给client(在web container返回给client的response中),web container上的这个HttpSession是临时的。
  • 后面Client在每次发送请求给服务器时,都将session ID发送给web container,这样web container就很容易区分出是哪个client.
  • Web container使用这个session ID,找到对应的HttpSession,并将此次request与这个HttpSession联系起来。  

1.1 web container中如何获得HttpSession

    HttpServletRequest中的方法,如下图3所示:

/**     *     * Returns the current session associated with this request,     * or if the request does not have a session, creates one.     *      * @return		the HttpSession associated     *			with this request     *     * @see	#getSession(boolean)     *     */    public HttpSession getSession();    /**     *     * Returns the current HttpSession     * associated with this request or, if there is no     * current session and create is true, returns      * a new session.     *     * 

If create is false * and the request has no valid HttpSession, * this method returns null. * *

To make sure the session is properly maintained, * you must call this method before * the response is committed. If the container is using cookies * to maintain session integrity and is asked to create a new session * when the response is committed, an IllegalStateException is thrown. * * * * * @param create true to create * a new session for this request if necessary; * false to return null * if there's no current session * * * @return the HttpSession associated * with this request or null if * create is false * and the request has no valid session * * @see #getSession() * * */ public HttpSession getSession(boolean create);

                                                              图3 获取HttpSession的方式

    HttpSession中的方法如下图4所示,销毁HttpSession

/**     * Invalidates this session then unbinds any objects bound     * to it.      *     * @exception IllegalStateException	if this method is called on an     *					already invalidated session     */    public void invalidate();

                                                               图4 销毁HttpSession

2.client-server model缺点

    client-server model,如果client不发送请求,server不允许发送送数据给client。为了克服这个困难,开发者可以使用 请求服务器——即不断轮询服务器,或者WebSocket。

3.Cross-Origin Resource Sharing ()    

    跨域资源共享。英文原版在。

转载于:https://my.oschina.net/u/2518341/blog/1940089

你可能感兴趣的文章
十大编程算法助程序员走上高手之路
查看>>
拼团代付时出现缺少字段问题,添加字段的SQL语句
查看>>
Python 编写知乎爬虫实践
查看>>
新书《案例梳理、真题透解与强化训练(最新2012版)》软考辅导用书即将8月上旬面市...
查看>>
Linux下运行openwebos- -(转)
查看>>
CSharp基础知识2-选择语句
查看>>
关于http请求在ie中F12查看显示已挂起,反应时间缓慢的问题
查看>>
LVM的简单配置
查看>>
【Windows Server 2019】AD批量添加用戶
查看>>
LoadRunner中winsocket协议脚本回放时的mismatch问题处理方法
查看>>
Eclipse常用的优秀插件在线更新地址,包括MyEclipse10.0
查看>>
21个非常有用的.htaccess 提示和技巧
查看>>
6220: Permanent data partition free space insufficient to allocate 33296 bytes of memory
查看>>
Hp Web Jetadmin 8.1的常规使用
查看>>
***笔记(二)
查看>>
awk详解
查看>>
hadoop之CombineFileInputFormat篇
查看>>
希望在软件开发生涯初期就知道的 4 件事
查看>>
java实现交换排序
查看>>
【51CTO学院三周年】我收获许多。
查看>>