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

how to access ajax response in a servlet  RSS feed

 
shanky raja
Greenhorn
Posts: 17
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Team,

I have a requirement where a form is present with first name, last name, file upload etc. File upload is done through ajax call and then the form is submitted to a servlet. I want to know whether the file has been uploaded or not in the servlet which is called after i click on the form submit button.
So is there any way i can read the response of the ajax call in the servlet ?

here is the ajax call below

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So those are two separate calls? In that case the first call should set a session attribute, which can then be read during handling of the second call.
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets should be stateless, so if you upload a file through a servlet you should not expect the servlet to know about this when it processes the next request.
You have two options, upload the file and other parameters together in one request as shown in the tutorial here:http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html
or
Upload the file and save it somewhere where the servlet will be able to query later, e.g database.
 
shanky raja
Greenhorn
Posts: 17
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info.
I am setting some value to a hidden field through javascript which can then be used as a flag when the 2nd request goes. Hope this works.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shanky raja wrote:Thanks for the info.
I am setting some value to a hidden field through javascript which can then be used as a flag when the 2nd request goes. Hope this works.

Hidden parameters go into request scope so you would need to send that value back to the page again so you can send it back with the second response. That would make the whole thing too chatty. Better store that value in session scope like suggested.
Also, what would that value be? I hope it's not some representation of the whole file because then you would be sending the file twice. You also wouldn't want to hold the whole file in memory (let alone session) unless they are really small files.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!