• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PageContext is not defined

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am new to jsp and just learning by doing.
I am trying to pass a variable from javascript to my jsp page. From what I've read, I have to set the variable as an attribute so that I can read it using EL.

There is my code:



But when I run that page, I get an the alert message saying

ReferenceError: pageContext is not defined

.
Can anyone help me solve this issue?

Thanks
Zain
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Java is not JavaScript and vice versa. I'm not sure where you got the idea that you could mix Java into a JavaScript block, but it's way off the mark.

The Java code that you posted (to set the scoped variable/attribute) should be in the servlet that gets invoked prior to forwarding to the JSP.

As a newcomer to JSP, I recommend reading
  • The Secret Life of JSPs
  • The Front Man

  •  
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Also, for when you get the code into the right place, why the try block? There's nothing that could fail in that code.
     
    Zain Rizvi
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well I also had another approach. This seems to be better working for me.



    This is using JSTL, is that correct?


    I added the try/catch block because at first there was nothing being printed to screen. So I wanted to see if something was going wrong.
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Zain Rizvi wrote:Well I also had another approach. This seems to be better working for me.


    While it may appear to be working, it's still off the mark. Way off!

    The JSTL does not need to be in the JavaScript block. It's not JavaScript and it's very very wrong to think that the JSP mehacnisms have anything whatsoever to do with JavaScript.

    Did you read those articles? Especially the first one?

    Continuing to think that the JavaScript code and the JSTL code have anything at all to do with each other is going to cause big-time problems for you unless you get the concepts straight in your mind right now.

    I added the try/catch block at first there was nothing being printed to screen. So I wanted to see if something was going wrong.


    Not useful. The JSTL and any other JSP mechanisms execute on the server long before the page is sent to the browser. Once at the browser, the JavaScript is interpreted. They know nothing at all about each other!

    Do a View Source at the browser and look at what your JavaScript block turned into. Is it useful?
     
    Zain Rizvi
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for your help. I guess I got a bit more reading to do before jumping into this. I'll be sure to read those articles when I get a chance.

    I did view the page source. The line

    was not in there. And

    was transformed to

    Thanks
    Zain
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Exactly. Read that first article ASAP!

    The JSP stuff is evaluated on the server as a template in order to format an HTML page to send to the browser. The HTML and JavaScript on the page is meaningless to the JSP -- it could be random text for all it matters.

    Once the HTML page is sent to the browser, the browsers interprets the HTML and JavaScript.

    Also read this JspFaq entry: CanIMixJSPWithJavaScript
     
    I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic