File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlets and database connectivity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets and database connectivity" Watch "Servlets and database connectivity" New topic
Author

Servlets and database connectivity

Chak Terlapu
Ranch Hand

Joined: Oct 20, 2000
Posts: 32
Hi,
I have got a basic question. I am connecting to a database through a servlet and displaying a table. What I need to do is have three buttons at the bottom of the table like "ADD", "DELETE" "MODIFY". I am wondering how I can add buttons to the HTML page through the servlet and how to capture the actions of each button when clicked like when "ADD" button is clicked it should pass control to a servlet "add.java". If anyone could throw me some light or atleast point me in the right direction that would be great.
Thanks
Chak
Jeff Sunder
Ranch Hand

Joined: Jun 26, 2001
Posts: 47
Outputting the buttons within the servlet is same as outputting any other html code within a servlet. Use the out.print or out.println to output your html code. So you would have something like:
out.print("<input type=\"button\" name=\"button1\">");
I thought of one way to capture the button click, but have never tried it myself. But here it is anyways: using javascript, call a function that takes one parameter. the parameter will indicate which button is pressed, from there you can call a servlet depending on the button clicked. Here's an example,
<script language="JavaScript">
function sendToServlet(buttonPressed) {
if (buttonPressed == 1) {
window.location = "servlet/Add";
}else if (buttonPressed == 2) {
window.location = "servlet/Delete";
}else if (buttonPressed == 3) {
window.location = "servlet/Update";
}
</script>
In your servlet you will have:
out.println("<input type=\"button\" name=\"add\" Value=\"Add\" onClick=\"sendToServlet(1)\">");
Let me know if this works.
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
Another way that i will do is to associate a hidden field with each form element. Then set the action of the form to the same servlet and check which hidden values is passed.For example

<Form method="post" action="yourservlet_paht">
<Input type="submit" value="Add">
<Input type="hidden" name="action" value="Add">
</Form>
<Form method="post" action="yourservlet_paht">
<Input type="submit" value="Modify">
<Input type="hidden" name="action" value="modify">
</Form>
<Form method="post" action="yourservlet_paht">
<Input type="submit" value="Delete">
<Input type="hidden" name="action" value="delete">
</Form>
In servlet use request.getParameter("action"), then use .equals() method to check the string value to determine which action to employ. Hope this help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets and database connectivity
 
Similar Threads
applet or jsp for comm with servlet ?
Capturing selected check boxes
How to populate, clear and append records into JTable
javascript window problem
Workaround for browser refresh/back button clicks?