包括操作,包括请求对象的指令和哈希码?

时间:2014-06-15 11:50:38

标签: java jsp web hash include

所以这是我的小代码:

的index.jsp:

Hashcode of request object in main page: <%= request.hashCode() %>
<%@ include file="includedirective.jsp"%>
<jsp:include page="includeaction.jsp" />

includeDirective.jsp:

Hashcode of request object in include directive: <%= request.hashCode() %>

includeAction.jsp:

Hashcode of request object in include action: <%= request.hashCode() %>

所以在页面刷新3次之后,输出:

Hashcode of request object in main page: 1646052972
Hashcode of request object in include directive: 1646052972
Hashcode of request object in include action: 318408832

Hashcode of request object in main page: 1646052972
Hashcode of request object in include directive: 1646052972
Hashcode of request object in include action: 618720209

Hashcode of request object in main page: 1646052972
Hashcode of request object in include directive: 1646052972
Hashcode of request object in include action: 412698049

以下是我的以下问题:

1)为什么index.jsp中请求对象的哈希码不随每次刷新而改变?不应该为每个请求创建一个新的请求对象吗?

2)为什么每次刷新页面时,include动作中请求对象的哈希码都在变化?

3)我理解当我使用动作包含页面时有2个不同的请求对象。如果我在includeAction.jsp中添加它:

<%
request.setAttribute("actionAttribute", "Attribute set in includeAction");
%>

然后在index.jsp中我尝试:

<%
    out.print(request.getAttribute("actionAttribute"));
%>

我期待不要看到&#34; actionAttribute&#34;的价值但它在那里?怎么会?

3 个答案:

答案 0 :(得分:1)

Include指令在容器进行&#34;翻译&#34;之前将代码插入到main.jsp中。生成的servlet包含main.jsp和includeDirective.jsp文件的代码。

当您使用include动作时,includeAction.jsp的响应将插入到main.jsp中,但不包括在翻译中。 includeDirective.jsp的代码不是生成的servlet。

1)JSP在第一个请求中转换为Sevlet。

2)包含操作在每个请求中进行新的翻译。

3)请求对象对于main.jsp和includeAction.jsp是相同的。如果在includeAction运行时设置属性,则可以看到includeAction&#34;响应&#34;到main.jsp。

<jsp:include page="includeaction.jsp" />

之前尝试打印属性

答案 1 :(得分:1)

  1. 您假设hashCode表示内存地址,因此相同 对象。
  2. 但是,hashCode可能会针对同一个对象进行更改。
  3. HashCode方法经常被覆盖,通常是从中删除的 对象中的数据。
  4. 因此,如果请求对象中的任何数据发生更改,则hashCode可以 变化。
  5. 因此,如果服务器更改了某些请求对象,则会传递该请求对象 在请求对象中的数据,hashCode可以随时改变 时间。
  6. 尝试以相反的顺序打印相同的操作并告诉我们什么 发生的情况。
  7. 参考 - http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()

答案 2 :(得分:-1)

你需要这个的任何特殊原因吗?

  

1)为什么index.jsp中请求对象的哈希码不是   每次刷新都会改变?

很难说,取决于hashCode方法的实现。你应该知道,servlet容器中的哪个类实现请求,反编译它并分析hashcode方法。

  

2)为什么包含动作的请求对象的哈希码是   每次刷新页面时都会更改?

与上述相同 - 依赖于实现。你应该知道每个容器可能以不同的方式实现它,因为规范中没有定义hashCode方法契约

  

3)我知道有2个不同的请求对象。 [..] 我是   期望不要看到&#34; actionAttribute&#34;但它在那里?   怎么样?

它们不完全是两个不同的对象。如果你后退一步并查看RequestDispatcher接口,那里有一个include(ServletRequest request, ServletResponse response)方法。 jsp:include被转换为该方法调用,所以实际上你的第一个请求是第二次调用的参数。无论是相同的请求还是包含在其他类中,都依赖于容器。但是,您在请求对象中设置的任何属性都将在两个位置都可见,从逻辑角度来看,它是相同的请求。

<强>更新 为了确认,我之前写的内容,我已经在我的网络容器上进行了测试。结果如下。在我的Web容器中,哈希码在每个请求的所有三个位置都是相同的:

Hashcode of request object in main page: -798420760 
Hashcode of request object in include directive: -798420760 
Hashcode of request object in include action: -798420760

Hashcode of request object in main page: -798420760 
Hashcode of request object in include directive: -798420760 
Hashcode of request object in include action: -798420760  

在我的容器中,SAME请求对象用于索引和包含操作。这是确认其实现依赖性和对您的问题的答案取决于平台的另一点。