Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Return Javascript Alert  RSS feed

 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a download servlet that can be accessed directly as long as the link sent out includes the full path of the file:

<http://our.download.site/DownloadContnet?file=/content/prospectDownload/prospect1.xls>

We have some users who send out the link with spaces in between the path and the filename so that the process sends out an error. I can capture the error, but I want to create a javascript alert that will tell the user that only a portion of the file's path was read because of a space in the name. How would I go about returning a javascript message?

Thanks for any help.

And here's the basic code I'm using:


[ May 08, 2007: Message edited by: Pat Flickner ]

[ May 08, 2007: Message edited by: Pat Flickner ]
[ May 08, 2007: Message edited by: Pat Flickner ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just properly encode the URL so that spaces are not an issue?
[ May 08, 2007: Message edited by: Bear Bibeault ]
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can tell you how to send a message to the client, but I think that this problem requires further analysis.

How does the space get there?

If the user uses the input type="file" it should not be a problem.
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not putting in the space; the user is in his or her email. Anything that's on the web page is fine. The problem is when users upload files that they don't want publicly available, so instead of having links on the page, they upload the files and then send out the links. Sometimes they copy and paste the name into the link. It's easy for me to see when they've done that because the link stops before the filename. The problem is, since a link you email doesn't look like a link until after you send it, they don't see the extra space.

I don't want to simply bypass the error instead of throwing an exception -- that will confuse the poor user who was emailed the link in the first place. I want to create a popup file in place of the bad link they got.

I tried replacing the process with contentType="text/html" and sending the filename that contains the javascript popup in it, but it loads it as "dummy.txt" and opens up the file with the html and javascript.

Does that help, info-wise?
[ May 09, 2007: Message edited by: Pat Flickner ]
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, with a little help from my friends, I got it working! I had to move the code for the ServletOutputStream to after the InputStream so that it would not commit the response before I'd actually tested if the file requested was valid. Once I did that, if the filename requested was invalid in any way, shape, or form, it pulled in an html file whose sole purpose was to execute a javascript alert. PDC! (pretty durned cool)

If anyone is interested in code that works really, really well, here you go:


And the badLink.html looks like this:
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do know that the servlet spec provides redirect, forward, and include mechanisms, don't you? It also has mechanisms for you to configure particular JSP pages to be shown for given error codes (even ones that you define).

There is a link to the servlet spec in my signature.
The time spent reading, cover to cover, will not be time wasted.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you do a validation of the url submitted by the user in the client side? Some simple validation you can always do in the client side.If it cannot be processed then show the user a javascript alert message.
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I'll read the Servlet Spec info. I hadn't known that. Thanks for including the links with your sig. I appreciate that very much. One of these days I'll have time to sit and really understand everything I've been doing.
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul, the files are on the server, so there would be no way to do any client-side validation. Unless you mean testing to see if it's a valid file in the first place. I don't care whether the file is valid or not. My primary concern was to create javascript if the link was bad in any way. I do recognize that I went about it in an offhanded manner instead of testing for file.isFile(). I did modify my code this morning when I realized that I'd bypassed that little tidbit, which is what I should have done in the first place, but that's not the point. My primary concern was being able to force a javascript alert so that the user would realize that there was a problem with the emailed link.

Here's my update:
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pat Flickner:


We have some users who send out the link with spaces in between the path and the filename so that the process sends out an error.



Obviously we cannot check for the files which are residing in the server.But what I suggested was to check for whether the URL submitted by the user is ok enough to be accepted by your system.Look for things like whether the url contains space or stuffs like that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!