Solved

scope problem

Posted on 2002-03-13
38
286 Views
Last Modified: 2010-04-01
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")); %>
0
Comment
Question by:knightEknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 21
  • 14
  • 2
  • +1
38 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 6862637
have you tried this instead:

<%! 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"));  %>

0
 
LVL 33

Author Comment

by:knightEknight
ID: 6862674
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.)
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6862710
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_string0, 0, _jsp_string0.length);
      pageContext.write(_jsp_string1, 0, _jsp_string1.length);
      pageContext.write(_jsp_string2, 0, _jsp_string2.length);
      out.print((pageName));
      pageContext.write(_jsp_string3, 0, _jsp_string3.length);
      out.print((pageType));
      pageContext.write(_jsp_string4, 0, _jsp_string4.length);
      pageContext.write(_jsp_string1, 0, _jsp_string1.length);
       addMenuItem(pageName="bunk", pageType=new String("blah"));
    } catch (java.lang.Throwable _jsp_e) {
      pageContext.handlePageException(_jsp_e);
    } finally {
      JspFactory.getDefaultFactory().releasePageContext(pageContext);
    }

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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Author Comment

by:knightEknight
ID: 6862745
it seems that my addMenuItem function isn't very busy.   suggestions?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6862755
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
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6862801
I did try this:

   pageName: <%=pageName%><BR>
   pageType: <% out.write(pageType); %><BR>

is this not the same thing?  
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6862810
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
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6862819
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);
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6862820
oops, that should be:

addMenuItem(out, pageType, pageName);

...same results
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6862822
this is what I get for not cut-n-pasting ... here is the real code:

addMenuItem(out, pageName="bunk", pageType="blah");
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6862827
<%! void addMenuItem(JspWriter out, String dummy1, String dummy2) {

  <out.write(pageType + "<BR>");

%>
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6862830
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
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6864393
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?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6864709
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
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6864873
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.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6865044
sure.. maybe someone else can enlighten us on alternatives.

CJ
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6865347
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)
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6865370
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
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6865514
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.
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6867978
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" %>
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6868001
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
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 150 total points
ID: 6868029
to me it would make more sense to do a dynamic include

use:

<jsp:include page="menuitem.jsp" flush="true">
  <jsp: param name = "pageName" value ="<%=varName%>" />
  <jsp: param name = "pageType" value ="<%=varName%>" />
</jsp:include>

or use:

<jsp:include page="menuitem.jsp" flush="true">
  <jsp: param name = "pageName" value ="page1" />
  <jsp: param name = "pageType" value ="type1" />
</jsp:include>

using the first snippet you can iterate over a list and use dynamic var names to generate the menu.

menuitem.jsp:
<%
  String pageName = request.getParameter("pageName");
  String pageType = request.getParameter("pageType");
%>
  pageName: <%=pageName%><BR>
  pageType: <%=pageType%><BR>

0
 
LVL 33

Author Comment

by:knightEknight
ID: 6868039
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

0
 
LVL 33

Author Comment

by:knightEknight
ID: 6868044
ah-ha!  you were a step ahead of me!  great!
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6868054
I'll ask the fDecode question seperately.  Thanks again!
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6868109
ok.

Glad I could help.  Thanx for the "A".

CJ
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6869489
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("pageType")==null) ? "" : request.getAttribute("pageType").toString();
    String pageName = (request.getAttribute("pageName")==null) ? "" : request.getAttribute("pageName").toString();
    String getData  = (request.getAttribute("getData")==null) ? "" : request.getAttribute("getData").toString();
   %>

   Page Name: <%=pageName%><BR>
   Page Type: <%=pageType%><BR>



Here is the output:

   Page Name: <BR>
   Page Type: <BR>



lil help?  thanks
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6869496
also I have this in the include file:

   Get Data: <%=getData%><BR>

same results though
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6869525
I changed  getAttribute()  to  getParameter()  and I now get the getData, but the pageName and pageType are still blank.
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6877342
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.
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6882617
any help?
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6891701
Testing to see if notifications here are generated.
Moondancer - EE Moderator
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6892313
Moondancer: I did get a notif of your post but none of  knightEknight's posts.

knightEknight lets continue this thread here:
http://www.experts-exchange.com/jsp/Q.20280448.html

That way atleast I know I should get notifs.

CJ
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6893912
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.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6906139
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
0
 

Expert Comment

by:ComTech
ID: 6906171
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
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6906397
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
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6910130
Something to check:  the answerer doesn't receive posts from the questioner, but does receive posts from other (or new) commenters (except Moondancer?)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven project in eclipse 11 62
stateless EJB with glassfish 2 75
getting param value 7 62
Suggestion on WebSite Template Sites 6 124
A hard and fast method for reducing Active Directory Administrators members.
By reading this blog, MSPs will gain insight into how to improve communications with their clients as well as establish a more profitable business.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question