Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to execute a servlet request from javascript?

Status
Not open for further replies.

johnnysorensen1

Programmer
Nov 17, 2003
4
NO
Can I some way execute a servlet request, and receive a text string?

I have made a servlet that respond with contentType("text/plain") and a short text string.
I want to execute this servlet by some javascript command. Or it could be a HTML tag.

I do a similar thing with a servlet that return contentType("image/png") and stream the content of the image to the output device. This is working fine with:

JavaScript:
myImage = new Image()
myImage.src = "imageServlet?someArg=’xx’"

HTML tag:
<IMG SRC&quot;imageServlet?someArg=’xx’&quot;/>

Is there any solution to this?


Johnny Sørensen
 
I can simplify the question by remove the servlet part of it.
What I need is some command or method that can do a request, and receive some text.
The text could be in a HTML file. And then I want to do something like this:

var request = new Request()
request.href = &quot;example.html&quot;
var textString = request.response

or maybe something like this:

var textString = Do.get(&quot;example.html&quot;)

(Of course none of this code is legal.)

Johnny Sørensen
 
Hi,

Some facts about servlets:
1. Servlets execute before the pages loads.
2. There is no dynamic communication between the page and the servlet.
3. If you need some info to dynamically passed and processed by a servlet, you’ll have to submit and reload.

To get a text back from a servlet, try the following:
Code:
out.write(&quot;<TEXTAREA id=\&quot;txtDynamicText\&quot; >&quot;);
out.print(someObject.getSomeText());
out.write(&quot;</TEXTAREA>&quot;);

But this is going to populate onLoad. If you want to process some users request then you’ll have to submit it and reload the page. For example, you want to check something after a user fills in a text box.
Code:
<script>
function processText(txt)
{
        document.<form name>.action = &quot;/servlet/<servlet_name>?someVar=&quot;+txt.value;
        document.<form name>.submit();
}
</script>

<form ........>
.
.
<input onChange = processText(this) value=”Enter a string” id = txtInput>
.
.
</form>

I haven't included the reloading and rest of the stuff but just how you may pass and get your string processed by a servlet. I hope it helps.[wink]

Good luck,[thumbsup]
Roy.
 
Thank you for the response.

What you have answered is correct.
But the HTTP protocol (or a level above) allows several nested requests to perform one page.
This is what is done with a page containing an image. The browser sends a new request for each image. The request is regardless of if it is for a static image or a dynamic servlet.

But my conclusion is that I have to use your solution anyway since it looks that there are no function to perform a request and receive a response without reload the whole page.

So thanks again
Johnny
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top