This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jstl question and page scope ?

 
Sami Sayeed
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
This is the first program..
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%
java.util.Map map = new java.util.HashMap();

//created a map which is basically in the jsp page(page scope??? am i right)
%>
<c:set target="<%=map%>" property="dog" value="tom"/>

<a href="testset.jsp">click</a>
${map.dog}

<%=map.get("dog")%>// this is fine

Everything works fine in the first file...

This is the second jsp

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%
java.util.Map map = new java.util.HashMap();

%>


<c:set target="${map}" property="dog" value="tom"/>// iam trying to use el to refer the map
// throws the following error
javax.servlet.ServletException
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:795)
org.apache.jsp.testsettarget_jsp._jspService(testsettarget_jsp.java:74)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
<a href="testset.jsp">click</a>
${map.dog}



<%=map.get("dog")%>
 
Guido Schoepp
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sami Sayeed:

This is the second jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%
java.util.Map map = new java.util.HashMap();
%>

<c:set target="${map}" property="dog" value="tom"/>// iam trying to use el to refer the map
// throws the following error
javax.servlet.ServletException
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:795)
org.apache.jsp.testsettarget_jsp._jspService(testsettarget_jsp.java:74)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
<a href="testset.jsp">click</a>
${map.dog}

<%=map.get("dog")%>


Mixing of JSP scripting and EL is not a good idea.
<c:set> takes the value out of the PageContext object. But your declaration in the script (java.util.Map map = ...) doesn't make the map accessible for PageContext.

Try to use <jsp:useBean id="map" class="java.util.HashMap"/> instead of <% java.util.Map map = ...%>

Guido
 
Shiang Wang
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use scriptlet in JSP page, it is copied to the _jspService() of the generated servlet as it appear in the JSP. Therefore if you declare anything in the scriptlet, it is not an attribute in any scope, it is just an object that exist in the scriptlet that can only be shared by objects in the scriptlet.

Shiang
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic