Solved

scope problem

Posted on 2002-03-13
38
277 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
  • 21
  • 14
  • 2
  • +1
38 Comments
 
LVL 19

Expert Comment

by:cheekycj
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 33

Author Comment

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

Expert Comment

by:cheekycj
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
oops, that should be:

addMenuItem(out, pageType, pageName);

...same results
0
 
LVL 33

Author Comment

by:knightEknight
Comment Utility
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
Comment Utility
<%! void addMenuItem(JspWriter out, String dummy1, String dummy2) {

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

%>
0
 
LVL 19

Expert Comment

by:cheekycj
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
sure.. maybe someone else can enlighten us on alternatives.

CJ
0
 
LVL 33

Author Comment

by:knightEknight
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 33

Author Comment

by:knightEknight
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ah-ha!  you were a step ahead of me!  great!
0
 
LVL 33

Author Comment

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

Expert Comment

by:cheekycj
Comment Utility
ok.

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

CJ
0
 
LVL 33

Author Comment

by:knightEknight
Comment Utility
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
Comment Utility
also I have this in the include file:

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

same results though
0
 
LVL 33

Author Comment

by:knightEknight
Comment Utility
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
Comment Utility
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
Comment Utility
any help?
0
 
LVL 1

Expert Comment

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

Expert Comment

by:cheekycj
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
A procedure for exporting installed hotfix details of remote computers using powershell
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now