aspose file tools*
The moose likes JSP and the fly likes PageContext is not defined Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "PageContext is not defined" Watch "PageContext is not defined" New topic
Author

PageContext is not defined

Zain Rizvi
Greenhorn

Joined: Jul 20, 2012
Posts: 5
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

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


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    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

    Joined: Jul 20, 2012
    Posts: 5
    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
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    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

    Joined: Jul 20, 2012
    Posts: 5
    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
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    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 agree. Here's the link: http://aspose.com/file-tools
     
    subject: PageContext is not defined