If i declare a variable in my A.jsp and i am trying to include A.jsp to B.jsp.So my question stands here whether the variable declared in A.jsp is acessable in B.jsp?
Please explain me for both the cases Dynamic include and static include.
When you include a jsp template using <%#page include=""> the source will actually be inserted and compiled into the including file. This is what makes you able to use variables declared in the parent file.
When doing a "dynamic" include it will use RequestDispatcher.include which will invoke the calling page as a new Servlet. This makes you unable to use declared variables.
I would recommend you to pass variables on the request scope using request.setAttribute("name", obj); when doing this
You can't pass server-side parameters using the <%# include %> directive. This directive does a static include. The contents of the included file are statically inserted into the including page. That is, during translation time from jsp to servlet.
Use the <jsp:include> tag instead, it is processed at runtime, and with it you can pass parameters using <jsp:param>.
For instance, if you have a.jsp with
<jsp:include page="b.jsp" />
<jsp:param name="param1" value="value1" />
<jsp:param name="param2" value="value2" />
</jsp:include>
You can get those parameters as request parameters in b.jsp
<% String v = request.getParameter("param1"); %>
Take into account you can still get request parameters available on a.jsp in b.jsp.
Related
I'm trying to reuse a variable that's been set in a jsp and used in a Stripes layout-render.
Main page: /index.jsp
Layout Render page: /jsp/widgets/signin/oAuthUrl.jsp
My /jsp/widgets/signin/oAuthUrl.jsp has a c:set variable called ${oauth_url}. But when I call the layout-render page via s:layout-render name="/jsp/widgets/signin/oAuthUrl.jsp", I would expect to be able to use any variables that are in there, as if I was including that script. But I can't use that ${oauth_url} in index.jsp.
I've even tried doing this, which unsurprisingly doesn't work:
<c:set var="oauth_url" value="<s:layout-render name="/jsp/widgets/signin/oAuthUrl.jsp"
buttonType="signup"/>"/>
<s:link href="${oauth_url}">
How can I reuse a variable that's being set in /jsp/widgets/signin/oAuthUrl.jsp? Basically, how can I make it global to the index.jsp page so it can be used in other places?
If I want to just use c:set values declared in /jsp/widgets/signin/oAuthUrl.jsp, should I import with a different tag instead of s:layout-render? I still want to pass it some attributes - like buttonType.
By default variables set with <c:set ... /> have the page scope and will not be available outside that page.
To make a variable available to other templates higher up the nesting hierarchy for a request, use the request scope:
<c:set var="…" value="…" scope="request" />
I'm trying to pass a parameter to the jsp file that I am including in my main jsp. From what I've seen online the way to do this using c:set
approot/index.jsp
<c:set var="Arg01" value="Argument01"/>
<jsp:include page="include/other.jsp">
<jsp:param name="myArg01" value="${Arg01}"/>
<jsp:param name="myArg02" value="Argument02"/>
</jsp:include>
Although when I try to use the variables in the included jsp page only the one argument seems to be coming through (the second one which is not using c:set)
approot/include/other.jsp
<!-- this doesn't work -->
<p>${param.myArg01}</p>
<!-- this does -->
<p>${param.myArg02}</p>
Nothing crashes but I can see that myArg01 is blank
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>
I am including a JSP page using the s:include tag:
<s:include value="/WEB-INF/jsp/centers/tpa/admin/users/UserEmployerAccessRow.jsp" />
I have several objects that I want to make available to this include and I am trying to store them into the request before the include happens. I am using the s:set tag to store to the request object:
<s:set var="employer_tmp" value="employer" scope="request" />
Everything in the jsp works as expected up to this point. The included jsp is not able to access objects in the request from a s:property tag. Here is what I have inside of the UserEmployerAccessRow.jsp:
<s:property value="#request[employer_tmp]" />
I have also tried it this way:
<s:property value="#employer_tmp" />
I have verified that the object is in the request by doing this:
<% out.println(request.getAttribute("employer_tmp")); %>
My guess is that the s:property is looking for the internal map that Struts sets up for the request and not looking at the actual request object. Does anyone know any markup to force the s:property to grab something out of the request object? It seems like overkill to have to run another action in the loop that I have this include in. I cannot use s:param to hand parameters to the include because it only handles simple http parameters and not objects. Thanks in advance for any direction you guys can provide!
I ended up finding the problem. The attribute name needs to be quoted in the s:property tag:
<s:property value="#request['employer_tmp']" />
I have this on welcome.jsp
<c:set var="pgTitle" value="Welcome"/>
<jsp:include page="/jsp/inc/head.jsp" />
And this in head.jsp:
<title>Site Name - ${pgTitle}</title>
But the variable is blank, and the output is merely
Site Name -
I have read many articles, and I cannot figure out what the problem is. If I echo ${pgTitle} elsewhere within the same welcome.jsp, then it outputs fine.
I am including the core tag library on both pages.
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.
One way is to pass variables to an include via query params:
<jsp:include page="/WEB-INF/views/partial.jsp?foo=${bar}" />
<jsp:include page="/WEB-INF/views/partial.jsp">
<jsp:param name="foo" value="${bar}" />
<jsp:param name="foo2" value="${bar2}" />
</jsp:include>
You can then access those params with ${param.foo}
Another would be to use custom tags:
/WEB-INF/tags/head.tag
<%# attribute name="title" %>
<head>
<title>${title}</title>
</head>
somePage.jsp
<%# taglib prefix="layout" tagdir="/WEB-INF/tags" %>
<html>
<layout:head title="myRadTitle" />
<body></body>
<html/>
You have to set the variable to be (at least) request scoped. You can do id as follows:
<c:set var="pgTitle" value="Welcome" scope="request"/>
<jsp:include page="/jsp/inc/head.jsp" />
And then, in the head.jsp, you can output the variable like this:
<c:out value="${requestScope.pgTitle}" />
You can accomplish this by using a combination of <c:import> and <c:param>.
somePage.jsp
<c:import url="header.jsp" >
<c:param name="pageTitle" value="Whatever the title of the page is"/>
</c:import>
header.jsp
<html>
<head>
<title>
<c:out value="${param.pageTitle}" default="Default is optional" />
</title>
</head>
</html>
I want to include html pages dynamically in a JSP page. I'm fetching the html url from HTML forder and using struts2 to pass the value to JSP page but I'm unable to do this on JSP using either jsp:include or #include tags.
For Example,
I have variable html Url like /somepath/variablehtmlname.html in my struts action property. I want to use this path to include the actual html files located at /somepath location.
<%# include ... %> is evaluated when your JSP pages are compiled and have no access to request variables (like Struts 2 action properties.) Use <c:import /> or <s:include /> instead, which include content on a per-request basis. <jsp:include /> should also work, but (as #BalusC requested) without the code, we can't tell why it doesn't.
Reusing Content in JSP Pages
I agree with the first answer (BobG). You can also simply have the JSP page directly serve up an http forwardTo using the refresh tag, where the servlet writes the new url location to a session variable : <meta http-equiv="refresh" content="0; URL=<%=htmlSessionLink>" />**