File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes document variable is nullified after servlet file download Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "document variable is nullified after servlet file download" Watch "document variable is nullified after servlet file download" New topic
Author

document variable is nullified after servlet file download

Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
Hello,

I started using a global variable defined in an external script to refer to the document:



This was due to a problem with the 'document' variable on a JSP becoming nulled out (supposedly) after a File/Save As box was displayed. I found that storing the document handle to the global variable fixed this.

And everything seemed to work fine, until...

One client machine is having an issue with a JSP that links to the external .js. Bringing in the script seems to fail on the global variable declaration, and so any calls to methods in the file are throwing errors.

Is this bad practice? Any idea what settings on the lone client machine we see this need to be checked? I've verified we're using the same version of IE, security settings seem ok, etc.

thanks,
Tom
[ December 03, 2007: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

The problem with applying band-aids is that they create problems that require other band-aids that create problems that require more band-aids that...

and you end up with something held together by chicken wire, bubble gum and duct tape that is fragile and falls apart if you breath near it.

My advice is to solve the real problem and pursue the "document gets nulled" issue and find out what's up with that. I've never heard of such an issue.

By the way, the JSP-ness of your page is moot. Once it gets to the browser, it's just HTML.
[ December 03, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
Ok, thanks for the advice.

So storing the document variable globally == bad idea then? Is it better to use the 'document' variable itself in the external .js file? I want to rip off the bandaids, but I want to make sure I'm heading in the right direction.

The document is null issue went something like this:

Button page would call servlet that would stream a file back to the client machine, prompting a save as dialog:



This all worked fine, but afterwards, from the same page, any other javascript commands referencing the document would fail. I assumed something was happening to 'document' handle because of the file/save as dialog.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

Ah, you didn't mention a servlet. I'll forward this on to the servlets forum with a suitable topic title change.
Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
Ok, thanks... yes, the file download issue is servlet-related...

Any chance you could address my questions about javascript 'document' handle?



So storing the document variable globally == bad idea then? Is it better to use the 'document' variable itself in the external .js file? I want to rip off the bandaids, but I want to make sure I'm heading in the right direction.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

Originally posted by Tom Katz:
Any chance you could address my questions about javascript 'document' handle?

Yes, holding an alias to browser resources is a very bad idea. If the document handle is getting nullified that means that the browser has determined it's no longer necessary ("why" is a question for people more familiar with what happens during an attachment download). Holding a reference to it behind the browsers back is unlikely to lead to good things.

Generally, if you've got to do something weird to get something to work, chances are it's going to cause other troubles.
Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
Gotcha, thanks again!

I wasn't sure if it was weird, using global references like that in a global script . . .
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

Global variables are generally to be avoided when possible, but holding references to browser resources beyond the time when the browser thinks that they're active is never a good idea.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Hmm...
I haven't seen this problem before and we have plenty of pages with links that download things.

Right now, I don't have access to a machine with MSIE but these two pages work in Firefox.

page1.html


download.jsp


Here's what I did:
1.) drop both pages into the ROOT directory of Tomcat.

2.) Point browser to http://localhost:8080/page1.html

3.) Click the link that says "click me" and note that the popup says "Hello There" (which is the document.title)

4.) Click on the 'download a file' link and save the file.

5.) Click the "click me" link again.

Even after clicking a link that makes a new request and receives a response but doesn't redraw the screen, the code in the "click me" link is still able to locate document.title. Can run this same test in the browser that you're having trouble with?
[ December 04, 2007: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I just tried again but, this time with a form that calls the download page instead of a hyperlink.
Same results.




I don't see how this could be anything but a browser issue.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

I think the time has come for you to code up a minimal example that demonstrates the issue so we can try to recreate it in our own environments.
Tom Katz
Ranch Hand

Joined: Aug 19, 2002
Posts: 169
Ok, I will get something together (once I'm back on my work 'puter).

This was pretty easily recreatable in IE6, and just today I upgraded my work machine to IE7 and couldn't get it to happen.

Thanks for the information/tips. If I can recreate it, I will post the relevant code.

ByTheWay: I got rid of all that global javascript variable funkiness.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: document variable is nullified after servlet file download