HttpSession是如何实现的?
我刚刚完成网络应用程序的期末考试。这是一个相当简单(尽管很长 - 12 页)考试的最后一个问题,要求我们编写会话的实现,类似于 javax.http.HttpSession 所做的。
我不愿意承认,这让我难住了。我使用 HashMap 编写了一个相当 BS 的实现,并使用随机 cookie 字符串映射到服务器上的序列化 HashMap 做了一些疯狂的操作,但我很确定它是假的......现在我很想知道它实际上是如何的完毕。
特别是作为一个广泛使用 PHP 但出于某种原因从未费心去了解其便利性背后的魔力的人,我非常有兴趣了解有关会话的底层实现的更多信息。当然是 J2EE 和 PHP,但任何其他语言/框架也都很棒。谢谢!
I just finished taking a final exam on web applications. Capping off what had been a rather easy (albeit lengthy - 12 pages) exam was a question asking us to code an implementation of sessions, similar to that done by javax.http.HttpSession.
I hate to admit, it stumped me. I cranked out a rather BS implemetation using a HashMap and did some craziness with a random cookie string mapping to a serialized HashMap on the server, but I'm pretty sure it's bogus...and now I'm dying to know how it's actually done.
Particularly as someone who has used PHP extensively but for whatever reason never bothered to learn the magic behind the convenience, I'm very interested to learn more about the underlying implementations of sessions. J2EE and PHP for sure, but any other languages/frameworks are great, too. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解 - 你很接近。
根据我的理解,本质上是 MD5“ID”的 cookie 保存在客户端,并通过 cookie 或修改后的 GET 传递。
在服务器端,具有匹配 sessionID 的“会话”数据保存在临时文件中(在 Linux 上默认为 /tmp)。我相信会话目录可以在 PHP.ini 文件中设置。
From my understanding - you're close.
From my understanding a cookie with what is essentially an MD5 "ID" is saved on the client side and delivered via cookie or modified GET.
On the server side the "session" data with matched sessionID is saved in a temp file (on Linux it is defaulted to /tmp). The session directory I believe can be set in the PHP.ini file.
由于它是一个接口,您可以查看在 Tomcat 等开源 Web 容器中实现它的类,并亲自查看。
As it is an interface, you may look at the class(es) implementing it in an open-source web container like Tomcat, and see for yourself.