Bajro Bajro - 6 months ago 13x
Java Question

JSP and Servlet parsing Parameter

Let's say I got a delete Button on my jsp site which has the following code:

<a href="deleteMovie?movieID=${movie.movieID}">
<button type="button" class="btn btn-xs btn-danger">
<span class="glyphicon glyphicontrash" aria-hidden="true"></span> Delete </button></a>

As we can see whenever I click my Delete Button it is forwarding to the site


I also got a servlet which is mapped to "/deleteMovie" so the servlet is called.
Inside the servlet i have the following code:

String movieID = request.getParameter("movieID");

How can you explain me that whatever I write behind the equals sign in the href value, that this specific text is returned under the String movieID?
For example if I would write:

<a href="deleteMovie?movieID=Foo">

Then the String movieID would contain "Foo".
But why? Why is it parsing the movieID out of the href?


In a GET request, you can give parameters to a request by using a query string. A query string is a ? followed by name-value pairs such as movieID=Foo. In this case, the value of movieID is Foo. If you want more than one name-value pair, put an & between them.

When you submit a form, it creates these parameters for you. So if you were to submit this form:

<form method="GET" action="deleteMovie">
    <input type="hidden" name="movieID" value="Foo" />
    <input type="submit" />

It would generate a URL of deleteMovie?movieID=Foo and then send it off.