This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

call something (set array and get its length ) from include file in jsp

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(pl); %>
</body>
</html>

and the include file looks like this :
get.jsp:
<% Object[] paramsHead = (Object[])request.getAttribute("genericTemplatesOpenDocumentHeadComponentParams"); %>
<% int pl = Array.getLength(paramsHead); %>

as you can see im trying to print the length of the array but witout any lock
the error im getting is:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 4 in the jsp file: /get.jsp
Generated servlet error:
Array cannot be resolved

why is that ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paramsHead.length will return the array's length
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan:

Array cannot be resolved


This says it all. Did you import the package containing Array?

Or, as Satou pointed out, why are you getting the length of the array the hard way?
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean by the hard why ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easy way : paramsHead.length
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan:
what do you mean by the hard why ?


java.lang.reflect.Array.getLength(array)

vs.

array.length

Why would you be using the former?
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I fixed that and its working fine but now I have some kind of scoping problem
here is what I have :
the include get.jsp :

<% Object[] paramsHead = (Object[])request.getAttribute("genericTemplatesOpenDocumentHeadComponentParams"); %>
<% int j = paramsHead.length; %>
<% String parm_len = Integer.toString(j);%>

and in the main set.jsp page I have :


<%@ 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(arr_len+"\n"); %>
</body>
</html>

the arr_len can't be read this is what I see in the eclipse debug
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you serious? You obviously can't reference a variable that was never created.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry sorry , i have to be
<% out.println(parm_len+"\n"); %>
that has bean set in the include page .
and it can't be read.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok 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?
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok maybe im doing every thing wrong here , my purpose in the end is to build includes for headers and footers
that takes parameters from the main page and according to that will present the relevant data , is that the way to do that?
can someone please ponit me to the right way ?
thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I would not use includes for headers and footers, I would write custom actions so that data could be easily passed to them via attributes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic