How to Pass Parameter to Jsp:Include via C:Set? What Are the Scopes of the Variables in Jsp

How to pass parameter to jsp:include via c:set? What are the scopes of the variables in JSP?

This is because the pgTitle variable is set in page scope. Check it here(sorry I can't get an official documentation for this).

If you want to make this work, you have to set the variable in request scope at least. To set your variable in request scope, use the scope attribute on <c:set>:

<c:set var="pgTitle" value="Welcome" scope="request" />

Per your comment, in web development, the scope of the variables matter because it defines where the variable can be used (similar to a variable declared as field in a class and a variable declared locally in a method). There are four scopes in JSP known as context:

  • Page scope (handled by PageContext). The variables can only be reached if set as attributes in the current page. This means, only current page can access these attributes, included pages are different pages, so they can't access these attributes.
  • Request scope (handled by ServletRequest). The variables can only be reached if set as attributes in the current request. This means, every page handled in the same request can access to these attributes. Important Note: A redirect implies a new request/response process. This means, if you set attributes on the request and execute a redirect, these attributes won't be set as attributes on the new request.
  • Session scope (handled by HttpSession). The variables can only be reached if set as attributes in the current user session. This means, every page used in the same user session can use these attributes until they are removed or the session expires.
  • Application scope (handled by ServletContext). The variables can only be reached if set as attributes in the current context. This means, every page on every session attribute can access to these variables until they are removed from SessionContext or the web application is undeployed.

More info:

  • What are the different scopes in JSP?

Is this the right way to accomplish what I am trying to do?

If there's a Servlet or another controller that handles the attributes to be set in the request (e.g. @Controller from Spring MVC or JSF managed bean), then set the attribute there and not in your page directly.

Personally, it takes some time to earn experience and define the best scope of the variables when used on web applications. Basic examples:

  • The split of a String by comma for presentation purposes will affect only to current view, so this can be set in page scope.
  • Error and successful messages are best suited in request scope. If user updates the page, he/she probably must not see the same messages unless the data is re-processed.
  • User info as name, nickname and preferences can be set in session scope.
  • If you have to display a list of Countries (that should not change in few days), you can store this list in application scope.

Passing parameters to another JSP file using jsp:include tag

<c:forEach var="instanceVar" items="${instanceList}">
<jsp:include page="instance.jsp">
<jsp:param name="myVar" value="${instanceVar}"/>
</jsp:include>
</c:forEach>

In the instance.jsp

<c:out value="${param.myVar}"/>

Java JSP Using Include with Variable Parameters

This way of getting around it is probably awful but it's the only way I could find to get around the issue.

I used a different kind of include in my main JSP

public static String myArg01 = "Argument01";
public static String myArg02 = "Argument02";
<%@ include file="include/other.jsp" %>

Then I could reference the variables directly inside the included JSP file

<p><%= myArg01 %></p>
<p><%= myArg02 %></p>

Passing a set of parameters to jsp include using jstl

I don't know how your blockJSPs look like, but i suggest this workaround :

<c:forEach var="block" items="${blocks}">
<c:set var="blockParams" value="${block.blockParameters}" scope="request" />
<jsp:include page="${block.blockJSP}" />
</c:forEach>

This will set the collection blockParams available in all the requestScope, therefore it will be accessible within the .jsp corresponding to the block - treat the collection inside this jsp, as a variable instead of a parameter.

Source : Passing c:forEach variable to a jsp:include

Passing c:forEach variable to a jsp:include

ANSWER: I ended up just having to do this to get it to work.

<c:forEach items="${cart.entries}" var="entry">
<c:set var="entryFC" value="${entry}" scope="request"></c:set>
<jsp:include page="helper.jsp"></jsp:include>
</c:forEach>

Then i referenced the entryFC in my include. Not very elegant at all but its working so i guess ill go with it.

How to pass Object using jsp:include param tag into another jsp

I don't think you really want tag files here. That's way overkill and too confusing for what you want to accomplish. You need to spend time understanding "scope". Instead of tag files, I would:

1) Change your attribute to be in the "request" scope instead of the default "page" scope by changing this line:

<c:forEach items="${attributeDTOList}" var="attribute" varStatus="status">

to this

<c:forEach items="${attributeDTOList}" var="attribute" varStatus="status">
<c:set var="attribute" value="${attribute}" scope="request"/>

That will make "attribute" a "requestScope" variable that can be used in other JSP files that are c:imported. (Note: forEach doesn't support scope attribute so use c:set to scope it inside each iteration.)

2) Change your original jsp:include to c:import. So change it from:

<jsp:include page="attributeSubFeatureRemove.jsp" >
<jsp:param name="attribute" value="${attribute}" />
</jsp:include>

to this

<c:import url="attributeSubFeatureRemove.jsp"/>

Note that we don't explicitly try to pass the attribute as a parameter, because we have already made it available to all c:imported pages in the "requestScope".

3) Modify your c:imported JSP to reference the attribute using the requestScope by changing this:

<c:set value="${param.attribute}" var="attribute" />
<c:forEach items="${attribute.subFeatures}" var="subAttribute">

to this

<c:forEach items="${requestScope.attribute.subFeatures}" var="subAttribute">

Here we no longer need the c:set because you already have the attribute available. We just need to make sure we look in the requestScope for that variable, instead of in the default pageScope or as a parameter (because we are no longer passing it as a parameter).

This technique will be a lot easier for you to manage.

How to pass java variables from scriptlets to c:when expression in jstl?

Variables in scriptlets cannot be seen in JSTL because Expression Language, the stuff between ${} used in JSTL, will look for attributes in page, request, session or application. You have to at least store the variable from scriptlet in one of these scopes, then use it.

This is an example:

<%
boolean a = true;
request.setAttribute("a", a);
%>

<c:if test="${a}">
<c:out value="a was found and it's true." />
</c:if>

More info:

  • Expression Language StackOverflow wiki
  • JSTL StackOverflow wiki

As a recommendation, stop using scriptlets. Move the business logic in your JSP to controller and the view logic into EL, JSTL and other tags like <display>. More info: How to avoid Java code in JSP files?



Related Topics



Leave a reply



Submit