When to use requestScope in jstl?

半世苍凉 提交于 2019-12-20 12:37:34

问题


A jstl variable is set in request scope in a jsp

<c:set var="name" value="Tiger" scope="request" />

This variable is accessed from a jspf included to this jsp. Now, is there any difference in accessing the variable in these two ways ?

1) <c:out value="${name}" />
2) <c:out value="${requestScope.name}" />

When to use requestScope ?


回答1:


You use requestScope when you absoluetely want your object to come from the request, and not from the page, session or application scope. Inded, using ${name} will search for a name attribute in the page, then in the request, then in the session, then in the application.

Let's say that some other code in the JSP set a name attribute in the page scope. But you want to access the name in the request: you're forced to use requestScope.

Let's say the session might have a name attribute. Not using requestScope.name would return the session-scoped name if the JSP forgot to set the name attribute in the request scope.

If the goal of the JSP fragment is to access something set in the enclosing JSP, maybe this JSP fragment should be a JSP tag, and you should pass the name as an argument to this tag.




回答2:


Within my research (I am also new one for jstl), request scope can set values to request page from response page for example assume that we have a page called index.jsp and its action page is index_action.jsp

if we, set values to the action page

<c:set var="nme" scope="request" value="Janaka aravinda"/>
<%  request.getRequestDispatcher("index.jsp").forward(request, response); %>

(// I created nme variable and set its value as Janaka aravinda. and back to reload request page(index.jsp) )

Now we can call nme in index.jsp nme variable as follow Request value

<c:out value="${nme}"/>


来源:https://stackoverflow.com/questions/19112098/when-to-use-requestscope-in-jstl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!