Reputation: 6181
I am developing a web application using JSP and Servlets.
I wants to call servlet's method from JSP page when user click's on the Update button.
<input type="button" value="Update" onclick="<%MyServletName.Update(request, response);%>"></input>
When I click on Update button then this method is calling the servlet, But the problem is that when form is loaded then this method is called automatically.
Thanks in advance.....
Source Code....
<%@page import="MyPackageName.MyServletName"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update</title>
</head>
<body>
<form>
<%
String[][] data = (String[][])request.getAttribute("data");
String[] columnNames = (String[])request.getAttribute("columnNames");
//fill the table data
if(columnNames.length!=0 && data.length!=0)
{
%><table><%
}
for(int i=0;i<data.length;i++){
%>
<tr> <td><%= columnNames[0] %></td> <td><input type="text" name="text1" value="<%= data[i][0] %>"></td> </tr>
<tr> <td><%= columnNames[1] %></td> <td><input type="text" name="text2" value="<%= data[i][1] %>"></td> </tr>
<tr> <td><%= columnNames[2] %></td> <td><input type="text" name="text3" value="<%= data[i][2] %>"></td> </tr>
<%
}
%>
<tr>
<td></td>
<td><input type="button" value="Update" onclick="<%PlanProtocolEdit.Update(request, response);%>"></input></td>
</tr>
</table>
</form>
</body>
</html>
Is there any way that I can call the servlet method other than dogGet() and doPost() without invoking the servlets dogGet() and doPost() methods?
Upvotes: 2
Views: 46946
Reputation: 677
You can call a servlet method other than doPost() and doGet() by creating a useBean of jsp.
<jsp:useBean id="someid" class="SomePackageName.PlanProtocolEdit">
and call the servlet method onclick as:
<input type="button" value="Update" onclick="<% someid.Update(args[0], args[1]); %>" />
This jsp bean would identify your servlet class which you now can access in your jsp page with the id given to it in useBean tag.
Do not forget to close the useBean tag.
Upvotes: 0
Reputation: 24334
You should specify the action on the form itself not the actual input button.
If you define an action on the form. E.g.
The forms submit button will submit the form to that URL. E.g.
<form action="/mypath/someurl">
<input type="submit" value="Submit" />
</form>
You can add an additional attribute to the form
tag to specify whether you want the request to be a get
or a post
.
<form action="/mypath/someurl" method="POST">
Would send a post request, you can then pick this up in your Servlet with the handlePost
method
The above approach should be used, currently you are trying to call a Java method on a javascript onclick event. This is incorrect and it is not doing what you think it is.
The code in PlanProtocolEdit
. Update should be in a doGet
or doPost
method of a servlet which will be triggered by configuring your form as described above.
Upvotes: 7
Reputation: 10997
<input type="button" value="Update" onclick="<%MyServletName.Update(request, response);%>"></input>
This line in your jsp will be evaluated to
<input type="button" value="Update" onclick=""></input>
in HTML page. If you want to call your servlet on click, first map your servlet to a url path say /myservletpath in web.xml and then
use
<input type="button" value="Update" onclick="location.href='/myservletpath'"></input>
Upvotes: 5