tags:

views:

33

answers:

3

I have a jsp that uses a 'counter' (Integer) object to keep track of the various pieces of the page that are displayed.

There are three major sections of the page, each implemented using a separate jsp that is jsp:included.

So it basically looks like this:

JSP #1

<html>
    <body>
       <jsp:include page="include1.jsp" />
       <jsp:include page="include2.jsp" />
       <jsp:include page="include3.jsp" />
    </body>
</html>

I need the çounter to be passed in to the first jsp:include, updated (addition only, if that matters) and have the updated value handed in to the next jsp:include, and then have the newly updated value haded in to the next jsp:include.

So counter starts at 0. include1.jsp updates this variable to 3. include2.jsp starts with the value 3 and updates to 5. include3.jsp starts with the value 5 and updates to 9.

I have this whole setup working well with all the other necessary data that needs to be handed in to the jsp:includes, but I can not for the life of me figure out how to have the /same/ object be used in all places so that it will be updated by the various jsp:includes.

Is this even possible?

Any other way to achieve the functionality I'm looking for? (use a counter across all jsp:includes)

Any and all help will be greatly appreciated.

A: 

I think this is possible you could pass a parameter like this:

<jsp:include page="include1.jsp" />  
  <jsp:param name="counter" value="<%=counter%>" />  
</jsp:include> 

Plus you have to define the counter as a jsp declaration like this i think:

<%! int counter =0; %>
kukudas
This looks like a very simple solution - I think I was missing JSP's 'static' concept. I will give this a go and report back. Thank you for your response.
Joel
It won't make it static, but it will make it an instance variable instead of a local variable. Instance variables are however shared among **all requests** throughout the entire webapplication. You don't want to have that, it would only lead to unexpected results when multiple users access the same JSP simultaneously.
BalusC
BalusC is right. It's a jsp declaration which will result in an instance variable. I also think the solution suggested by him is more appropriate and easier to implement.
kukudas
I also read some stuff that suggested this might not be a 'modern' approach. I have looked into another solution.
Joel
A: 

Just store it in the request scope. JSTL's c:set is helpful in this.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:set var="counter" value="0" scope="request" />
<jsp:include page="include1.jsp" />
<jsp:include page="include2.jsp" />
<jsp:include page="include3.jsp" />

Then in each of the include files do as follows to increment it:

<c:set var="counter" value="${counter + 1}" scope="request" />

or even statically

<c:set var="counter" value="3" scope="request" />
BalusC
Thank you for the response. I hadn't thought of looking to the JSTL for a solution. I will try this out!
Joel
A: 

I solved this problem by passing my value in the request object.

It looks like this:

My main.jsp says:

<% int counter = 0; 
   request.setAttribute("counter", counter);
%>

<jsp:include page="include1.jsp">
   <jsp:param name="counter" value="<%=request.getAttribute("counter")%> />
</jsp:include>

<jsp:include page="include2.jsp">
   <jsp:param name="counter" value="<%=request.getAttribute("counter")%> />
</jsp:include>

<jsp:include page="include3.jsp">
   <jsp:param name="counter" value="<%=request.getAttribute("counter")%> />
</jsp:include>

and my 'include' jsps look like this:

<%
   Integer counter = 
      Integer.valueOf(request.getParameter("counter"));
%>

   <!-- do my stuff, sometimes manipulating the 'counter' Integer object -->

<%
   request.setAttribute("counter", counter);
%>

You can see how this works. The main page passes the value of the counter object as a param to the first includeX.jsp, which pulls the param out of the request and uses it. Then at the end of the includeX.jsp it sets the current value of the counter in the request attributes. When the main.jsp picks it up again it pulls the value from the request attributes to hand in to the next includeX.jsp as a param again.

At one point I did have it implemented /all/ using request.get/setAttribute calls rather than the stuff, but it seemed to indicate the flow better to pass it as a parameter rather than pull it from the request object. I don't really know the pros/cons of either approach but this is working for me now.

Thanks for the help everyone, even though I implemented in a totally different way than suggested.

Joel