calling Servlet's method from JSP
Solution 1
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.
Solution 2
<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>
Bhushan
Updated on January 07, 2020Comments
-
Bhushan over 4 years
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?
-
Bhushan over 11 yearsThanks @cowls for your quick response. But I had also tried this way, and even if I try to call this method from <FORM> then also it is called when the page is loaded
-
cowls over 11 yearsIt is probably something else that is calling that. Do you have any javascript running? Try disablying JS and see if it still happens
-
Bhushan over 11 yearsThanks @Subin S for response. But I wants to call a method of servlet. How should I map Method of servlet?
-
Bhushan over 11 yearsNo there is no javascript running. Is it not working because client only receives HTML-CSS etc and not java code? or is there mistake in my code?
-
Subin Sebastian over 11 yearsabove code will create a button. onclick of that button a
get request is sent to /myservletpath
. In web.xml you are mapping this url to a servlet. That servlet should have a overridendoGet
method which will be invoked. within doGet you can call whatever method you want. -
cowls over 11 yearsDid you try disabling JS and trying? Also it may be helpful if you post your form html
-
Bhushan over 11 yearsYes I can do it that way also. but the problem is that doGet() method and doPost() method already having some source code.. so I don't want those methods to be called. (If i create a separate servlet and put the Update mathod's code in that servlets doGet() or doPost() method then it will work. but I want to put that code in the same servlet)