call something (set array and get its length ) from include file in jsp
Hello
im kinda newbee to jsp , and Im trying to figure out how to accomplish this :
I have simple page that include second page the main page looks like this :
set.jsp :
<%@ include file="get.jsp" %>
<% Object[] params = new Object[3];%>
<% params[0] = "fo1" ;%>
<% params[1] = "gla" ;%>
<% params[2] = "gg" ;%>
<% request.setAttribute("genericTemplatesOpenDocumentHeadComponentParams",params); %>
<html>
<body>
<% out.println(parm_len+"\n"); %>
</body>
</html>
and the include file looks like this :
get.jsp:
<% Object[] paramsHead = (Object[])request.getAttribute("genericTemplatesOpenDocumentHeadComponentParams"); %>
<% int j = paramsHead.length; %>
<% String parm_len = Integer.toString(j);%>
its not variable problem when i changed the code in the include file to
<% int j = 77; %>
<% String parm_len = Integer.toString(j);%>
the main page did print the value 77 to the page , this mean i have problm
white the :
request.getAttribute or request.getAttribute
what can it be ? is there any thing wrong there i can't see?
as you can see im trying to print the length of the array but witout any luck
thanks
[1392 byte] By [
Meirya] at [2007-10-3 1:49:13]

ok this didnt worked i tryied this code that working but again i have problem
i added :
<% RequestDispatcher rd = request.getRequestDispatcher("get.jsp"); %>
<% rd.include(request,response); %>
and it does set the variable but when i try to read the it can be readed only from the the included page (gst.jsp) when i try to put this variable on the main page it does not recognaze it , why ?
and it does set the variable but when i try to read the it can be readied only from the the included page (gst.jsp) when i try to put this variable on the main page it does not recognize it , why ?
Meirya at 2007-7-14 18:47:34 >

Hi,
the directive <%@ include file="..." %> is called a "static include". It includes the text of the given file BEFORE the whole JSP is generated into a Java file and then compiled. Nevertheless the position of the include is very important.
So your file should look like the following:
<%
Object[] params = new Object[3];
params[0] = "fo1" ;
params[1] = "gla" ;
params[2] = "gg" ;
%>
<html>
<body>
<%@ include file="get.jsp" %>
<% out.println(parm_len+"\n"); %>
</body>
</html>
Since you use a static include, you do not have to put the params array into the request scope - it is a local variable to the JSPs service method and can be used directly in the static include file. Reverse - the parm_len variable defined in the static include can be used directly in the including file to print out the length of the array. So you get.jsp would look like the following:
<%
int j = params.length;
String parm_len = Integer.toString(j);
%>
Anyway - this is not a good coding style.
> <% RequestDispatcher rd =
> request.getRequestDispatcher("get.jsp"); %>
> <% rd.include(request,response); %>
The alternative you used here is identical with using the JSP directive <jsp:include page="..." /> which defines a DYNAMIC include. The difference is that each of the JSPs is generated into a Java file and compiled separately (and has to be compilable!). The included file is executed separately at runtime and it's result is inserted in the response stream at the position of the include tag. In this case you have to put the params array into the request as well as the "return value" from get.jsp.
Your JSP has to look like this:
<%
Object[] params = new Object[3];
params[0] = "fo1" ;
params[1] = "gla" ;
params[2] = "gg" ;
request.setAttribute("genericTemplatesOpenDocumentHeadComponentParams",params);
%>
<html>
<body>
<jsp:include page="get.jsp" flush="true" />
<%
int parm_len = (String) request.getAttribute("result");
out.println(parm_len+"\n");
%>
</body>
</html>
And here is the content of "get.jsp":
<%
Object[] paramsHead = (Object[]) request.getAttribute("genericTemplatesOpenDocumentHeadComponentParams");
int j = paramsHead.length;
String parm_len = Integer.toString(j);
request.setAttribute("result", parm_len);
%>
Try to read a book about JSP, e. g. "JavaServer Pages" by Hans Bergsten (http://www.amazon.com/gp/product/0596005636/sr=8-1/qid=1154507640/ref=pd_bbs_1/002-5485374-2480805?ie=UTF8). It should be your basic lecture when beginning with JSP programming. Another hint: visit the J2EE tutorial by SUN (http://java.sun.com/j2ee/1.4/docs/tutorial/doc/, chapters 11-16) - it also provides some basics.
Cheers,
Ren?