0

I have below form in html page

<form name="input" action="sayHello1.jsp?param1=test1" method="get">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>

i enter the value "test" in text box and submit it.

I expect to see the submitted URL in browser as

http://localhost:8080/helloWorld/sayHello1.jsp?param1=test1&user=test

but i see the url as

http://localhost:8080/helloWorld/sayHello1.jsp?user=test 

why so ?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
emilly
  • 10,060
  • 33
  • 97
  • 172

2 Answers2

2

The HTML specification states

When a form element form is submitted from an element submitter (typically a button), optionally with a scripted-submit flag set, the user agent must run the following steps:

[...]

  • Let query be the result of encoding the form data set using the application/x-www-form-urlencoded encoding algorithm, interpreted as a US-ASCII string.

  • Let destination be a new URL that is equal to the action except that its component is replaced by query (adding a U+003F QUESTION MARK character (?) if appropriate).

Basically, it replaces the existing query string with a new one generated from the form parameters.

Your browser is following the specification.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • +1. Interestingly, the HTML 4 spec says *If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type.* That would obviously result in an incorrect URL if the action already contains a query string, and I guess the behavior is undefined in that case. The HTML5 spec is clearer. – JB Nizet Apr 14 '14 at 18:24
  • but with post request i see the correct url i.e "http://localhost:8080/helloWorld/sayHello1.jsp?param1=test1" – emilly Apr 14 '14 at 18:24
  • 1
    @user3222249 If you follow the link, there are different behaviors for GET and POST. For POST, the browser leaves it as is. – Sotirios Delimanolis Apr 14 '14 at 18:25
1

The implementation of the form appears to be rewriting the query string (not merging it). To get your desired behavior add a <input type="hidden" name="param1" value="test1"> to your form, and don't try to add values to the action URL.

According to this answer, we should expect this behavior.

So, your browser will trash the existing "?..." part of your URI and replace it with a new one based on your form.

Community
  • 1
  • 1
PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56