make difference between 2 buttons
In my JSP i have 2 buttons submit one for updating and the other is for deleting from database. I put i button like the following:
<td><img src="ok.jpg" onclick="MettreAjourForm();" name="ok"/>开发者_StackOverflow</td>
and in the same servlet i want to make difference in the treatement of the 2 cases. Is there any proposition?
Thanks.
Just give the buttons a name and value as with every other HTML input element. Only the name=value
pair of the actually pressed button will be sent to the server as request parameter. You can then determine the action based on the value:
<input type="submit" name="action" value="edit">
<input type="submit" name="action" value="delete">
..with the following in the Servlet:
String action = request.getParameter("action");
if ("edit".equals(action)) {
// Edit button was pressed.
} else if ("delete".equals(action)) {
// Delete button was pressed.
}
You can also give the buttons a different name so that you just need to check its presence in the request parameter map:
<input type="submit" name="edit" value="edit">
<input type="submit" name="delete" value="delete">
..with the following in the Servlet:
String edit = request.getParameter("edit");
String delete = request.getParameter("delete");
if (edit != null) {
// Edit button was pressed.
} else if (delete != null) {
// Delete button was pressed.
}
No need for Javascript hacks/workarounds. It would only make your website unuseable when the client has JS disabled.
精彩评论