Reputation: 5800
I am working with plain JSP (JSF is NOT an option) on a web application. In a JSP file, I have a table, and in each row I display each student. Also, in each row I want to have an "edit" button, which will redirect to a page where we can edit this student and delete it.
So far, I think the best think to do is to put a submittable form with only a button inside in each row.
<table border="1">
<tr>
<th>Name</th>
<th>LastName</th>
<th>Send Message</th>
<th>Erase</th>
</tr>
<c:forEach items="${students}" var="bean">
<tr>
<td>${bean.name }</td>
<td>${bean.lastName }</td>
<td><input type="button" name="edit" value="Do!"
onclick="foo();" /></td>
<td><form action="<%="EditStudent?studentid=6" %>"><input type="submit" value="Edit" /></form></td>
</tr>
</c:forEach>
</table>
The form submission URL is just for test, hence the hardcoded "6" parameter as a student id. However, when I press the button, I get redirected to a page with URL "http://localhost:8080/JSPProj/EditStudent?"
, where I can clearly see that any text after the question mark is ignored! If I remove the question mark, the URL redirect works (but there is no way to pass the parameter I want to the next servlet).
Can anyone give me any insight as to why this happens?
PS. I also tried with EL, and the same thing happens.
Upvotes: 0
Views: 17117
Reputation: 2360
1) First of all you should not use form action in the given context because using POST method is not logical in this scenario.
2) You should never use static content inside jsp expression language. ie: action="<%="EditStudent?studentid=6" %>"
3) In the given situation, you need to use <a>
for each listed student and to redirect them to a servlet based on their IDs. And this approach will use GET
method.
This is how you should do <a href="EditStudent?studentid=${bean.studentID}"> click me </a>
Regards,
Upvotes: 1
Reputation: 61
First, you should specify your method=get or method=post. But that's just nitpicking on my part. The problem is this: Do not put your ? and parameters in the action url. Put your parameters in input tags.
<form action='EditStudent'>
<input type='hidden' name='studentid' id='studentid' value='6' />
<input type='submit' value='Edit' />
</form>
Edit: Actually GET vs POST isn't nitpicking. It was kinda flipant of me to say that. It matters. By default the browser will treat it as GET which means the parameters will be visible in the address bar and can be easily changed by the most unsophisticated of users. With POST it at least requires a bit more sophistication to monkey with. Although, obviously, you shouldn't rely on publicly viewable HTML for your security.
Input Type='Hidden' doesn't mean a user can't see it if they view source, obviously, but just that no textbox is put there.
Upvotes: 2