Click to See Complete Forum and Search --> : How to set up form action


TimAnderson
02-18-2010, 12:49 PM
I've been developing a web app on a single computer and have been using http://localhost:8080/myServlet in my form action field.

Now I'm trying to run my server on a remote machine and can get my web page to come up, but when I hit the Submit button, it tries to access the local host, not the host where the server resides. I'm sure that I can hard-code the name of the server instead of localhost, but what happens if the web app gets moved to a different server? I'd have to modify index.html to set the new name of the server.

How can I setup the action property such that it will access myServlet no matter the name of the server that it resides on?

harrierdh
02-18-2010, 04:09 PM
You could do this with javascript. You could change the url (action) in the form tag or use javacript document.form1.submit();

You could figure out which location the code is running on using javascript location object.

var currentURL = window.location;
alert ( currentURL.host ); // Displays 'www.example.com:80'

criterion9
02-18-2010, 04:17 PM
As an alternative use relative paths (../page.htm, page1.html) instead of absolute paths (http://yourserver.domain/page.htm, http://yourserver.domain/folder/page1.html). Then you can change servers or domains to your hearts content without having to use javascript hacks.

TimAnderson
02-18-2010, 05:12 PM
As an alternative use relative paths (../page.htm, page1.html) instead of absolute paths (http://yourserver.domain/page.htm, http://yourserver.domain/folder/page1.html). Then you can change servers or domains to your hearts content without having to use javascript hacks.

That worked! Thanks!