关于请求对象行为的混淆 [英] Confusion regarding the behaviour of the request object

查看:110
本文介绍了关于请求对象行为的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搞乱servlet和JSP,我对某些事感到困惑:

I've been messing around with servlets and JSPs and I am a bit confused at something:


  1. 我是制作了一个servlet(控制器),它将请求发送到JSP

  1. I've made a servlet (controller) which would dispatch the request to a JSP

我在$ $中使用setAttribute()方法为请求对象设置了一些属性b $ b servlet。

I've set some attributes to the request object using setAttribute() method in my servlet.

我可以在JSP中访问请求对象的参数和属性,而不会出现任何

问题。

I can access the request object's parameters and attributes within the JSP without any
problem.

现在我使用

session.setAttribute(test,request)将请求对象存储为会话对象中的属性。

Now I've stored the request object as an attribute in the session object using
session.setAttribute("test", request).

我写了第二个JSP(当我点击一个特定的按钮时,从第一个JSP导航到它会通过

Javascript - 通过使用window.location函数和

给出第二个JSP的地址作为值)

I've written a second JSP (navigating to it from the first JSP would be through
Javascript when I click a particular button- by using the window.location function and
giving the address of the second JSP as the value)

我对这些东西很陌生并且对这种行为感到困惑。为什么我的请求对象的属性被擦除而参数保持不变(只要我访问了我的第一个JSP中的参数;这对我来说更令人困惑,因为它没有意义IMO)

I am really new to this stuff and am confused about this behaviour. Why were my request object's attributes being 'erased' while the parameters remain intact (as long I had accessed the parameters in my first JSP; which is even more bewildering to me as it does not make sense IMO)

任何解释都将不胜感激!提前感谢你。

Any explanation would be appreciated! Thanking you in advance.

推荐答案

这只是一个有根据的猜测,但我认为问题是你的容器中的请求对象选择可能是懒惰的参数:当你问它一个参数时,它会伸出一些外部环境并提取所需数据,同时缓存它。

This is just an educated guess, but I think the problem is that request objects in your container of choice might be lazy about its parameters: when you ask it for a parameter it reaches out to some external context and pulls the required data, at the same time caching it.

然而,奇怪行为的原因并不重要。问题应该通过不在会话中保存请求来解决。请求对象只是当前请求的句柄,而不是数据存储本身。它可能正在使用下面的任何机制,因为我们知道所有属性都可以存储在threadlocals中。绝对没有合同可以使请求充当任何类型的存档。例如:如果我问这样存储的安全主体请求,这意味着什么?我的意思是当前的会议主席吗?我的意思是截至创建请求时的委托人吗?

Nevertheless, the reason of the strange behavior is not really important. The problem should be solved by not saving requests in session. A request object is only your handle to the current request, not a data store by itself. It might be using any mechanism underneath, for all we know the attributes may be stored in threadlocals. There is absolutely no contract that would make a request act as an archive of any sort. For example: what would it mean if I asked such a stored request for the security principal? Would I mean "the current principal of the session"? Would I mean "the principal as of the moment when the request was created"?

编辑:

出于纯粹的好奇心,我只看了一眼Tomcat的实现(我不知道你正在使用哪个容器)并发现它支持我的声明:不仅大多数数据是懒惰地收集,而是请求对象被回收!因此,如果您尝试将其存储在会话中然后使用,您可能会发现您正在使用某人的其他请求。

Out of pure curiosity I just took a peek at Tomcat's implementation (I have no idea which container you are using) and found that it supports my claims: not only most of the data is gathered lazily, but the request object is recycled! So if you try store it in a session and then use, you might find that you are using someone's else request.

这篇关于关于请求对象行为的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆