Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling servlet from a Javascript

 
P Agarwal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me in calling a servlet from a javascript? I am new to this. Please help. Thanks in Advance.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can use following in script
document.formName.action="/servletName";
document.formName.submit();
Regard,
Gajanan
 
P Agarwal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your reply. But actually I want to call the servlet which would return me one XML, that is to be parsed. So if I submit the form, how will I get the XML? I don't have any idea as how to retrieve XML from servlet called through Javascript.
If you can give me any clue...
Regards,
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaScript (which runs clientside in this context) cannot directly call a servlet - since it is serverside code. What it can do (as Gajanan C explains) is make an HTTP request to the server. What will come back is probably another HTML page. If you then want to run clientside JavaScript with variables dynamically set in the servlet you will have to render them into the HTML page. For example:
[code]
<script language="JavaScript">
alert("I'm doing something with XML: " + <%=xmlStringFromJSP%> ) ;
</script>
[ June 03, 2004: Message edited by: Paul Sturrock ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic