aspose file tools*
The moose likes Servlets and the fly likes Calling servlet from a Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling servlet from a Javascript" Watch "Calling servlet from a Javascript" New topic
Author

Calling servlet from a Javascript

P Agarwal
Greenhorn

Joined: Jun 03, 2004
Posts: 2
Can anyone help me in calling a servlet from a javascript? I am new to this. Please help. Thanks in Advance.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
You can use following in script
document.formName.action="/servletName";
document.formName.submit();
Regard,
Gajanan
P Agarwal
Greenhorn

Joined: Jun 03, 2004
Posts: 2
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

Joined: Apr 14, 2004
Posts: 10336

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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling servlet from a Javascript