Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

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")); %>
0
knightEknight
Asked:
knightEknight
  • 21
  • 14
  • 2
  • +1
1 Solution
 
cheekycjCommented:
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
 
knightEknightAuthor Commented:
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
 
cheekycjCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
knightEknightAuthor Commented:
it seems that my addMenuItem function isn't very busy.   suggestions?
0
 
cheekycjCommented:
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
 
knightEknightAuthor Commented:
I did try this:

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

is this not the same thing?  
0
 
cheekycjCommented:
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
 
knightEknightAuthor Commented:
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
 
knightEknightAuthor Commented:
oops, that should be:

addMenuItem(out, pageType, pageName);

...same results
0
 
knightEknightAuthor Commented:
this is what I get for not cut-n-pasting ... here is the real code:

addMenuItem(out, pageName="bunk", pageType="blah");
0
 
cheekycjCommented:
<%! void addMenuItem(JspWriter out, String dummy1, String dummy2) {

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

%>
0
 
cheekycjCommented:
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
 
knightEknightAuthor Commented:
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
 
cheekycjCommented:
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
 
knightEknightAuthor Commented:
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
 
cheekycjCommented:
sure.. maybe someone else can enlighten us on alternatives.

CJ
0
 
knightEknightAuthor Commented:
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
 
cheekycjCommented:
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
 
knightEknightAuthor Commented:
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
 
knightEknightAuthor Commented:
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
 
cheekycjCommented:
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
 
cheekycjCommented:
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
 
knightEknightAuthor Commented:
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
 
knightEknightAuthor Commented:
ah-ha!  you were a step ahead of me!  great!
0
 
knightEknightAuthor Commented:
I'll ask the fDecode question seperately.  Thanks again!
0
 
cheekycjCommented:
ok.

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

CJ
0
 
knightEknightAuthor Commented:
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
 
knightEknightAuthor Commented:
also I have this in the include file:

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

same results though
0
 
knightEknightAuthor Commented:
I changed  getAttribute()  to  getParameter()  and I now get the getData, but the pageName and pageType are still blank.
0
 
knightEknightAuthor Commented:
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
 
knightEknightAuthor Commented:
any help?
0
 
MoondancerCommented:
Testing to see if notifications here are generated.
Moondancer - EE Moderator
0
 
cheekycjCommented:
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
 
knightEknightAuthor Commented:
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
 
MoondancerCommented:
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
 
ComTechCommented:
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
 
cheekycjCommented:
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
 
knightEknightAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 21
  • 14
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now