Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet to Applet data transfer

 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

I have a requirement where I need to download multiple files from unix server to user local machine (windows) on some user action in JSP. To achieve this task, I am writting an applet embedded in JSP which is making an HTTP connection to servlet running on the server via post method.My code is working fine if I run from RAD but when I run through JSP documents are not getting downloaded but however call to servlet is happening. To test whether my servlet is returing back to applet i am sending a string to applet from servlet via outputstream object and printing it using paint method and I am able to print the value on the applet.

I am kind of stuck here on how to resolve the issue of downloading the documents to local machine. Any help will be highly appreciated

Please find below my applet code


Below is my servlet code


Thanks,
Madhu
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At which point (line of code) do the codes start to deviate in behavior from one another?
 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf Dittmer for the reply

I hope below answers your question.

The servlet is getting called from applet on urlConnection.getInputStream() line. The servlet scans the files in folder under C:\\EPOSTemp and I am able to see the filenames in console. EVen servlet is writing the String by name "Madhu" to outPutStream via outputToApplet.writeUTF("Madhu") which I am able to fetch the value from applet and display it on the browser where applet is running.

Kindly let me know if you need more info...Thanks
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Binary serialization can be tricky unless the JVMs on both ends are of the same version; is that the case in both your scenarios?
 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the application will be accessed by different user machines, i think in some cases JVMs will be a different version. Can you please provide an alternate solution.. Thanks in advance
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a .zip file with all the files in it, and then stream that directly, without any object stream.
 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf Dittmer.. My requirement is to send files individually but not as a zip file, please suggest?. Do i need to make my applet as signed if I have to access the object stream from servlet.

Also please help on the below.

I need to pass a dynamic value from JSP page to applet and retrive it in applet.

Can you suggest/help me how I can accomplish this tasks.

Thanks in advance,
Madhu
 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding my query on making applet as signed, I meant do I need to make applet as signed if I have to download the files to local machine after receiving the object stream from servlet. Please help..?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65113
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need an applet at all. In fact, applets are discouraged at this stage. When the user clicks on a link to get the files, zip up the files and return the zip file as the response. Be sure to set the response headers appropriately.
 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear Bibeault for the reply. This is what I was doing initially. When user clicks on the link, my code was downloading all the documents zipped together to user machine by prompting a save dialogue to the user to save the zip file. But unfortunately users don't want to have the save prompt message and they want individual documents to be downloaded. Because of this I am using the applet approach. Kindly help with your ideas. Many thanks.....
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many documents are we talking about? If it's just a few, then the web page can just show links for all of them. I agree with Bear that applets are almost never the answer these days. Plus (to paraphrase the Rolling Stones), users don't always get what they want, they sometimes get what they need instead. And the way to download several files with one click is a zip file.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65113
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of security concerns, you cannot just silently save something to the user's system. Imagine the nightmare if any web site you visit could start changing your system without you knowing it.

Security concerns are the same reason that applets are often disabled.
 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On an average we will be having 5 documents for each request. After downloading these documents, my code need to open the Lotus notes and attach the downloaded files so that user can send email to intended recpient. In my case users are very lazy to click the save button (Hence this post) to save the zip file to local machine and I am sure they will not agree on having the link for each document because they will have to click so many times to download the documents to local machine. I will be very gald of you guys if I get an feasible approach using applet to achieve this. Many thanks in advance.....
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65113
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't help you with applets -- I haven't written once since about 1998 or so.

I have moved this to the applets forum.
 
Paul Clapham
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you're determined to go ahead with the applet, I would start by simplifying the code. Downloading a file should be easy, just read the bytes from the file on the server and write them to the response. And on the client just read the bytes from the response and write them to a file until there aren't any more. Anything else (like that object serialization business you have there) is unnecessary for a simple file transfer.
 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. Do I need to make the applet as signed If i have to download the files from server to local machine. I tried from local RAD websphere by calling the servlet via applet and I was able to transfer the contents locally from one folder to another folder.Below is the flow

JSP page intiaites applet which calls servlet and servlet will get contents from local folder (for eg:A) and passes it thru outputstream to applet and applet will read the contents and file it to an other folder in the same machine (for eg: B). This was achieved after making applet as selfsigned using keytool and jarsigner commands.

However after deploying the project on the destination server, I am unable to achieve the same but I have confirmed that applet to servlet and viceversa communication is happening. Do I need to sign the applet again to download files locally?

Thanks...
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, applets need to be signed in order to access the local file system.
 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.. I am able to pass the content from servlet to applet. Along with content I need to pass the filenames as well so that I can write the content to a file with the same filename in applet.

Please advise how i can accomplish this.. Thanks for your help...
 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There,

I need to make multiple calls to a servlet from single instance of Applet.. i.e., flow is like this

1) Applet calls servlet requesting for some info (filenames)
2) Servlet sends the requested info (filenames)
3) Applet iterates based on the info (number of filenames) provided and calls servlet to send contents of each filename to applet. In a loop applet downloads file one by one making multiple calls to servlet requesting for content..

can we do this?, please advise....

 
Madhu Sudhan B T
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once the documents are downloaded how to stop/terminate the applet. After downloading if user goes to the webpage, the applet again begins to download the documents. I want to stop/terminate the applet after its first execution no matter even if user keeps the browser as is?. Please help.........
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely the applet should not be written in a way that it downloads the files no matter what - it should only do that if necessary or required. For example, if the user presses a button in the applet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic