knightEknight
asked on
scope problem
Why doesn't this code print "bunk" and "blah" instead of "PN" and "PT" ?
<%! String pageName = "PN"; %>
<%! String pageType = "PT"; %>
<%! void addMenuItem(String dummy1, String dummy2) { %>
pageName: <%=pageName%><BR>
pageType: <%=pageType%><BR>
<%! } %>
<% addMenuItem(pageName="bunk ", pageType=new String("blah")); %>
<%! String pageName = "PN"; %>
<%! String pageType = "PT"; %>
<%! void addMenuItem(String dummy1, String dummy2) { %>
pageName: <%=pageName%><BR>
pageType: <%=pageType%><BR>
<%! } %>
<% addMenuItem(pageName="bunk
ASKER
yes, I tried that before I posted the question.
What I would really like to be able to do is pass string parameters into a function which outputs them as formatted HTML. The code I posted above was the closest thing I could get to that, but I can't get the variables in the right scope. No matter what I do to pageName and pageType, they always seem to contain the values "PN" and "PT" respectively.
And if I try to output dummy1 or dummy2, I get a compiler error -- which I cannot see because I don't have access to the server. (I know it doesn't compile because it returns a File-Not-Found.)
What I would really like to be able to do is pass string parameters into a function which outputs them as formatted HTML. The code I posted above was the closest thing I could get to that, but I can't get the variables in the right scope. No matter what I do to pageName and pageType, they always seem to contain the values "PN" and "PT" respectively.
And if I try to output dummy1 or dummy2, I get a compiler error -- which I cannot see because I don't have access to the server. (I know it doesn't compile because it returns a File-Not-Found.)
the above code you doesn't compile to the desired effect.
this is the converted servlet:
String pageName = "PN";
String pageType = "PT";
void addMenuItem(String dummy1, String dummy2) {
}
main method:
try {
pageContext.write(_jsp_str ing0, 0, _jsp_string0.length);
pageContext.write(_jsp_str ing1, 0, _jsp_string1.length);
pageContext.write(_jsp_str ing2, 0, _jsp_string2.length);
out.print((pageName));
pageContext.write(_jsp_str ing3, 0, _jsp_string3.length);
out.print((pageType));
pageContext.write(_jsp_str ing4, 0, _jsp_string4.length);
pageContext.write(_jsp_str ing1, 0, _jsp_string1.length);
addMenuItem(pageName="bunk ", pageType=new String("blah"));
} catch (java.lang.Throwable _jsp_e) {
pageContext.handlePageExce ption(_jsp _e);
} finally {
JspFactory.getDefaultFacto ry().relea sePageCont ext(pageCo ntext);
}
bottom of file:
static {
_jsp_string3 = "<BR>\n pageType: ".getBytes();
_jsp_string4 = "<BR>\n".getBytes();
_jsp_string0 = "\n".getBytes();
_jsp_string2 = "\n pageName: ".getBytes();
}
Not exactly what you would think huh?
CJ
this is the converted servlet:
String pageName = "PN";
String pageType = "PT";
void addMenuItem(String dummy1, String dummy2) {
}
main method:
try {
pageContext.write(_jsp_str
pageContext.write(_jsp_str
pageContext.write(_jsp_str
out.print((pageName));
pageContext.write(_jsp_str
out.print((pageType));
pageContext.write(_jsp_str
pageContext.write(_jsp_str
addMenuItem(pageName="bunk
} catch (java.lang.Throwable _jsp_e) {
pageContext.handlePageExce
} finally {
JspFactory.getDefaultFacto
}
bottom of file:
static {
_jsp_string3 = "<BR>\n pageType: ".getBytes();
_jsp_string4 = "<BR>\n".getBytes();
_jsp_string0 = "\n".getBytes();
_jsp_string2 = "\n pageName: ".getBytes();
}
Not exactly what you would think huh?
CJ
ASKER
it seems that my addMenuItem function isn't very busy. suggestions?
I think the problem is:
pageName: <%=pageName%><BR>
pageType: <%=pageType%><BR>
to have methods that output something.. from what I have seen . you need to have a JSPWriter var that uses println statements.
CJ
pageName: <%=pageName%><BR>
pageType: <%=pageType%><BR>
to have methods that output something.. from what I have seen . you need to have a JSPWriter var that uses println statements.
CJ
ASKER
I did try this:
pageName: <%=pageName%><BR>
pageType: <% out.write(pageType); %><BR>
is this not the same thing?
pageName: <%=pageName%><BR>
pageType: <% out.write(pageType); %><BR>
is this not the same thing?
AFAIK, for methods.. you need to have a local reference/separate reference to JSPWriter.
That is why I passed it in as a var in my first comment.
CJ
That is why I passed it in as a var in my first comment.
CJ
ASKER
ok, thanks. I just tried that and I got the same results when printing pageType, and a compiler error when printing dummy1.
<%! void addMenuItem(JspWriter out, String dummy1, String dummy2) { %>
<% out.write(pageType); %><BR>
...
addMenuItem(out, pageName, linkText);
<%! void addMenuItem(JspWriter out, String dummy1, String dummy2) { %>
<% out.write(pageType); %><BR>
...
addMenuItem(out, pageName, linkText);
ASKER
oops, that should be:
addMenuItem(out, pageType, pageName);
...same results
addMenuItem(out, pageType, pageName);
...same results
ASKER
this is what I get for not cut-n-pasting ... here is the real code:
addMenuItem(out, pageName="bunk", pageType="blah");
addMenuItem(out, pageName="bunk", pageType="blah");
<%! void addMenuItem(JspWriter out, String dummy1, String dummy2) {
<out.write(pageType + "<BR>");
%>
<out.write(pageType + "<BR>");
%>
oops I meant:
<%! void addMenuItem(JspWriter out, String dummy1, String dummy2) {
out.write(pageType + "<BR>");
%>
You can't use the <%= .. b/c that is what is screwing up the method.
CJ
<%! void addMenuItem(JspWriter out, String dummy1, String dummy2) {
out.write(pageType + "<BR>");
%>
You can't use the <%= .. b/c that is what is screwing up the method.
CJ
ASKER
so, the two statements below are NOT functionally identical:
<% out.write(pageType); %><BR>
<% out.write(pageType + "<BR>"); %>
because the first uses the global JspWriter object to write the "<BR>", where as the second statement uses the local JspWriter ... is this correct? Can you explain further?
<% out.write(pageType); %><BR>
<% out.write(pageType + "<BR>"); %>
because the first uses the global JspWriter object to write the "<BR>", where as the second statement uses the local JspWriter ... is this correct? Can you explain further?
nope they are not.
As far as I can gather.. the first statement moves the call to the main method..
the second one leaves it as a part of the method.
CJ
As far as I can gather.. the first statement moves the call to the main method..
the second one leaves it as a part of the method.
CJ
ASKER
Thanks CJ ... send me another comment in a week or so to remind me to accept your answer. I want to leave the question open for a little while longer. You have answered correctly, so you will get an A -- but my problem remains unresolved, and I want to see if anyone else can suggest a different way to do what I want.
sure.. maybe someone else can enlighten us on alternatives.
CJ
CJ
ASKER
for 50 more points ... what is the secret to getting this to work:
<%!
String fDecode( String srcStr, String ifStr, String thenStr, String elseStr )
{
if ( srcStr==null || ifStr==null )
return srcStr;
if ( srcStr.equals(ifStr) )
return thenStr;
else
return elseStr;
}
String fDecode( String srcStr, String ifStr, String thenStr )
{
return fDecode( srcStr, ifStr, thenStr, "" )
}
%>
The first function works fine. When I add the second function in, even if I rename it, it doesn't work. I suspect that in the second fDecode function I need to qualify the call to the first fDecode, but I don't know what object to use (something like obj.fDecode(...) Again, I am new to JSP and I can't see the compiler errors because I don't have server access. (bad way to try and learn this stuff)
<%!
String fDecode( String srcStr, String ifStr, String thenStr, String elseStr )
{
if ( srcStr==null || ifStr==null )
return srcStr;
if ( srcStr.equals(ifStr) )
return thenStr;
else
return elseStr;
}
String fDecode( String srcStr, String ifStr, String thenStr )
{
return fDecode( srcStr, ifStr, thenStr, "" )
}
%>
The first function works fine. When I add the second function in, even if I rename it, it doesn't work. I suspect that in the second fDecode function I need to qualify the call to the first fDecode, but I don't know what object to use (something like obj.fDecode(...) Again, I am new to JSP and I can't see the compiler errors because I don't have server access. (bad way to try and learn this stuff)
you don't need to use a object for each method call as long as the methods are static.
try this.fDecode(...)
I will play with the code.. myself too.
CJ
try this.fDecode(...)
I will play with the code.. myself too.
CJ
ASKER
ok thanks ... I already tried this.fDecode ... I also tried the static modifier in front of each function declaration, but it did not (appear to) make a difference.
ASKER
CJ ... going back to the original question ... I wonder if similar functionality can be done using an include file. Something like this:
menuitem.jsp:
pageName: <%=pageName%><BR>
pageType: <%=pageType%><BR>
then from the main page:
<%
String pageName = "page1";
String pageType = "type1";
%>
<%@ include file="menuitem.jsp" %>
<%
pageName = "page2"; // does this compile?
pageType = "type2";
%>
<%@ include file="menuitem.jsp" %>
<%
pageName = "page3";
pageType = "type3";
%>
<%@ include file="menuitem.jsp" %>
menuitem.jsp:
pageName: <%=pageName%><BR>
pageType: <%=pageType%><BR>
then from the main page:
<%
String pageName = "page1";
String pageType = "type1";
%>
<%@ include file="menuitem.jsp" %>
<%
pageName = "page2"; // does this compile?
pageType = "type2";
%>
<%@ include file="menuitem.jsp" %>
<%
pageName = "page3";
pageType = "type3";
%>
<%@ include file="menuitem.jsp" %>
that should work.
A static include is literally just including the file at compile time so what you have in menuitem.jsp will be inserted where ever (line by line) where ever you have your static include.
CJ
A static include is literally just including the file at compile time so what you have in menuitem.jsp will be inserted where ever (line by line) where ever you have your static include.
CJ
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok how about the next step:
<%
for ( int i=0; i<10; i++ ) {
String pageName = "page"+i;
String pageType = "type"+i;
%>
<%@ include file="menuitem.jsp" %>
<% } // end for %>
and how would this make it different?
<jsp:include page="menuitem.jsp" flush="true" />
... upped the points. will go higher if you can help me with the fDecode question above also. thanks
<%
for ( int i=0; i<10; i++ ) {
String pageName = "page"+i;
String pageType = "type"+i;
%>
<%@ include file="menuitem.jsp" %>
<% } // end for %>
and how would this make it different?
<jsp:include page="menuitem.jsp" flush="true" />
... upped the points. will go higher if you can help me with the fDecode question above also. thanks
ASKER
ah-ha! you were a step ahead of me! great!
ASKER
I'll ask the fDecode question seperately. Thanks again!
ok.
Glad I could help. Thanx for the "A".
CJ
Glad I could help. Thanx for the "A".
CJ
ASKER
CJ,
I'm having difficulty getting the parameter data passed to the include file. Here is what I have:
<% String pageType = "type1"; %>
<jsp:include page="mainpage.jsp?getData =junk" flush="true">
<jsp: param name="pageName" value="name1" />
<jsp: param name="pageType" value="<%=pageType%>" />
</jsp:include>
Here is the include file:
<%
String pageType = (request.getAttribute("pag eType")==n ull) ? "" : request.getAttribute("page Type").toS tring();
String pageName = (request.getAttribute("pag eName")==n ull) ? "" : request.getAttribute("page Name").toS tring();
String getData = (request.getAttribute("get Data")==nu ll) ? "" : request.getAttribute("getD ata").toSt ring();
%>
Page Name: <%=pageName%><BR>
Page Type: <%=pageType%><BR>
Here is the output:
Page Name: <BR>
Page Type: <BR>
lil help? thanks
I'm having difficulty getting the parameter data passed to the include file. Here is what I have:
<% String pageType = "type1"; %>
<jsp:include page="mainpage.jsp?getData
<jsp: param name="pageName" value="name1" />
<jsp: param name="pageType" value="<%=pageType%>" />
</jsp:include>
Here is the include file:
<%
String pageType = (request.getAttribute("pag
String pageName = (request.getAttribute("pag
String getData = (request.getAttribute("get
%>
Page Name: <%=pageName%><BR>
Page Type: <%=pageType%><BR>
Here is the output:
Page Name: <BR>
Page Type: <BR>
lil help? thanks
ASKER
also I have this in the include file:
Get Data: <%=getData%><BR>
same results though
Get Data: <%=getData%><BR>
same results though
ASKER
I changed getAttribute() to getParameter() and I now get the getData, but the pageName and pageType are still blank.
ASKER
however, I did get this to work:
<%
for ( int j=0; j<5; j++ ) {
pageName="test"+j;
pageType="bunk "+j;
%>
<%@ include file="menuitem.jsp" %>
<% } %>
but I would still like to know why the other type doesn't work.
<%
for ( int j=0; j<5; j++ ) {
pageName="test"+j;
pageType="bunk "+j;
%>
<%@ include file="menuitem.jsp" %>
<% } %>
but I would still like to know why the other type doesn't work.
ASKER
any help?
Testing to see if notifications here are generated.
Moondancer - EE Moderator
Moondancer - EE Moderator
Moondancer: I did get a notif of your post but none of knightEknight's posts.
knightEknight lets continue this thread here:
https://www.experts-exchange.com/jsp/Q.20280448.html
That way atleast I know I should get notifs.
CJ
knightEknight lets continue this thread here:
https://www.experts-exchange.com/jsp/Q.20280448.html
That way atleast I know I should get notifs.
CJ
ASKER
Moondancer,
FYI - I have noticed the same issue on some of my more recent PAQs (those that I have answered) ... the questioner later adds more comments and I don't get notified.
Thanks CJ, I'll continue on the other Q.
FYI - I have noticed the same issue on some of my more recent PAQs (those that I have answered) ... the questioner later adds more comments and I don't get notified.
Thanks CJ, I'll continue on the other Q.
Thank you for the feedback and sorry to hear that Email notifs are not consistently arriving. I have sent this link to both ComTech, Administrative Liaison and Ian, Community Support Manager to advise them of the Email notif problems. I've already sent prior examples to them as well.
Please check your profile to ensure that your Email and address information is still correct (just in case something changed).
Moondancer - EE Moderator
Please check your profile to ensure that your Email and address information is still correct (just in case something changed).
Moondancer - EE Moderator
I have emailed Engineering, and checked your accounts, all seems to be fine there, unless you have changed the email address, then I may need to re-verify it to make the system see your email address.
Best regards,
ComTech
CS Admin @ EE
Best regards,
ComTech
CS Admin @ EE
This is really wierd.. I haven't gotten a single notif when knightEknight posts.. nor did I get a notif on Moondancer's last post.. but ComTech's post sent me a notif. This is very odd.
CJ
CJ
ASKER
Something to check: the answerer doesn't receive posts from the questioner, but does receive posts from other (or new) commenters (except Moondancer?)
<%! String pageName = "PN"; %>
<%! String pageType = "PT"; %>
<%!
void addMenuItem(JspWriter out, String dummy1, String dummy2) throws java.io.IOException {
out.println("pageName:" + pageName+"<br>");
out.println("pageType:" + pageType+"<br>");
}
%>
<% addMenuItem(out, pageName="bunk", pageType=new String("blah")); %>