• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

how to access ajax response in a servlet

 
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

 
Rancher
Posts: 43016
76
  • 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.
 
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.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic