Gabe Nelson
Gabe Nelson

Reputation:

Posting a form without changing pages in a jsp

In a jsp, how do you post a form without changing the page?

In essence, I'm trying to:

1) First, kick off a servlet on the backend with a post to process the HttpServletRequest. 2) Second, once the servlet completes, a response message will be posted in a DIV on the page using prototype's Ajax.Updater function.

All without leaving the page of course.

Upvotes: 1

Views: 757

Answers (3)

Madhu
Madhu

Reputation: 5766

You can do this thru jQuery.AJAX follow : http://docs.jquery.com/Ajax

example : $.post(servleturl, paramters, callbackfunction, "html");

Upvotes: 1

Eric Nguyen
Eric Nguyen

Reputation: 40592

There are two methods I know of. First, you can extract the values from the form and submit them as a separate Ajax.Request. Alamar posted some good instructions on this:

Use Form.serialize(true) to get a hash with parameter names as keys.

You can do whatever you want to that hash, includeing deleting inputs, altering their values, filtering them and so on.

Ajax.Updated then will accept that modified hash as a parameter.

Another method is to create an iFrame which will take the response from your form request, using the "target" property of the form element. If you're working all in the same domain, you'll have access to the contents of that iFrame and can use the returned information in there to update the parent page with any messages you want to return. This approach is more convoluted, but necessary in some cases. For example, if you're working with file upload inputs, the serialized hash in the first approach won't work.

Upvotes: 0

HtRNcR
HtRNcR

Reputation:

I use a little of prototype, that you want probably work, just submit your form, in my work did something like that, but why you don´t use DWR? I think is better in asyncronus callbacks ands that stuff.

I´ll check the code for the asyncronous submit.

Upvotes: 0

Related Questions