cancel
Showing results for 
Search instead for 
Did you mean: 

Processing form data from HTML POST

jcobb
Champ in-the-making
Champ in-the-making
Hi
I've been getting on well with creating a first surf application - but have a query regarding the best architecture to adopt in processing user data posted to a url from a browser form.

I've already created a 'login' component that can appear on any page - it uses an html form to accept username / password, and posts to the '/login' url. A hidden field passes the current page as the success or failure urls - so the user ends up back at the same page whatever happens. The component checks for currently logged in user, and will display the username and a 'log out' link rather than the form if logged in.

So far the logout link is not ideal - it doesn't return the user to the same page as the Logout servlet doesn't allow us to pass a success url like login.

Now I want to create a 'register' component. I've successfully created an html form - but now need to write my own webscript to process the user input. My first attempt was to create a 'register' page and post the form data there. However none of my other component webscripts are set up for POSTs - only GETs, so they all throw errors. My register component should be completely independent of the page it's on, or other components, so it's not acceptable to create POST versions of all pages and components.

So now I've set up a webscript - processRegister. The idea is that rather than posting the form to a page url, we post to the webscript which will handle all the registration logic, then redirect back to whatever page the register component form was displayed on.

The login servlet uses response.sendRedirect - but I don't think this will be available.

I can't easily see how share processes posted data - it's all wrapped up in the gui.

So my questions are:

1. Is it a sensible architecture to create a webscript to handle posted form data for each component that uses an html form on the site, and then have it redirect back to the page we came from?
2. What api can I use from a webscript to redirect to a page (like the login servlet does)?
3 REPLIES 3

schambon
Champ in-the-making
Champ in-the-making
Hi,

1. Using a separate web script for handling POSTs and redirecting is what I'm doing too. Haven't found any other satisfactory way of handling user input, and I'm very happy with the principle anyway.

2. In order to perform redirects, you should just write this in your javascript:


status.code=302;
status.location="your redirection URL";

Cheers,
Sylvain.

jcobb
Champ in-the-making
Champ in-the-making
Thanks Sylvain -

I'll try that redirect code when I get a chance.

Incidentally - I'm working on the principal that I'll keep my components completely independent, and unaware of what else is happening on the page. So they'll want to somehow persist some data across page loads without using posts or variables in the url.

I see something about persisting data in the javascript api -

save()
reload()
remove()

Am i reading that right that you can keep a component's state the same across page reloads by storing model data and reloading it each time the component is called?

jcobb
Champ in-the-making
Champ in-the-making
OK - redirection working well now. In my form I use a hidden field:

<input type="hidden" id="returnUrl" name="returnUrl" value="${page.url.url}"/>

and pick that up in the script I'm posting the form to with something like:

status.code=302;
status.location=page.url.args["returnUrl"];
status.location=page.url.args["reuturnUrl"]+"&registerError="+json.message;

Still getting my head round the best way of passing arguments from page to page for a component, will start a new thread…