Need help submitting and retrieving values to and from a database using the same form in JSP application
Question:
Dear fellow Java developers:
I have a form that allows me to successfully submit data to a MySQL table. However, I also want to use this same form to allow the user to retrieve data from the database into the table also, to make any changes to an existing entry. My form is on a JSP, and when the user submits the form, it calls the doPost() method of my servlet, which in turn, uses hibernate to submit the entry into the MySQL table. I wish to add another button to the form that says “Get” and allow the user to insert values into the same form, and then query the table based on the entries the user has submitted.
This should be possible, as many web applications allow one to do this, but I can’t figure out how, because using whatever little knowledge of JSP, and servlets that I have, in my form I have the following tag:
<form action=”./DataServlet” method=”POST”>
for the “method” attribute, I am able to insert one value (i.e. “POST” or “GET”). Is there a way for me to add both “GET” and “POST”? Just wondering.
Thanks in advance to all who reply.
Solution:
Here is snippet example code.
Please note that this is just an example. I don’t even compile it. So if you have any problem, you can let me know.
Hope this help,
Sompol
HTML FORM
=========
<form name=”form1″ action=”yourservletorjsp” method=”POST”>
… any other fields …
<input type=hidden name=”action” value=”somevalue”>
<input type=button value=”Query” onclick=”document.form1.action=’query’;document.form1.submit();”>
<input type=button value=”Update” onclick=”document.form1.action=’update’;document.form1.submit();”>
</form>
SERVLET
=======
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
…
// action from the forms
String action = request.getParameter(“action”);
if (action != null && action.equals(“query”)) {
… your code for query action …
}
else if (action != null && action.equals(“update”)) {
… your code for update action …
}
}













Comments (0)
Trackbacks - Pingbacks (0)
Leave a Reply