how to use confirmation alert box before delete in jsp?

how to use confirmation alert box before delete in jsp?

I use submit button.I want alert box before deleting data.

View Answers

April 3, 2012 at 6:00 PM

1)application.jsp:

<%@ page import="java.sql.*" %>
<html>
<head>
<script language="javascript">
function deleteRecord(id){
    var doIt=confirm('Do you want to delete the record?');
  if(doIt){
   var f=document.form;
    f.method="post";
    f.action='../DeleteServlet?id='+id;
    f.submit();
    }
  else{

    }
}
</script>
</head>
<body>

<br><br>
<form method="post" name="form">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
<%
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String userName ="root";
String password="root";

int sumcount=0;
Statement st;
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db,userName,password);
String query = "select * from employee";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
%>
<%
while(rs.next()){
%>
<tr><td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><input type="button" name="delete" value="Delete" style="background-color:red;font-weight:bold;color:#ffffff;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>
<%
}
catch(Exception e){
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>

2)DeleteServlet.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class DeleteServlet extends HttpServlet { 
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = conn.createStatement();
st.executeUpdate("DELETE FROM employee WHERE id = '"+no+"'");
response.sendRedirect("/examples/jsp/application.jsp");
}
catch(Exception e){}
    }
}









Related Tutorials/Questions & Answers:
how to use confirmation alert box before delete in jsp?
How to create a confirmation box?
Advertisements
how to create own tld and use it in jspx file
how to create own tld and use it in jspx file
change appearance of alert box
JSP1
JSP2
alert box in iphone
Java Alert Box
Alert Box in Flex
ModuleNotFoundError: No module named 'jspp'
ModuleNotFoundError: No module named 'JSPy'
why alert box is not appearing in servlets...?
Display two alert box alternately by clicking on text
Flex Alert Box example
How to set left and top positions of alert box using JQuery in the below code?
Display alert box on clicking link
Name Display in alert box - JSP-Servlet
How to force cp to overwrite without confirmation
jQuery alert()
how to write jsps and databasetables,umldiagrams in fuelmanagement system project
how to delete a letter in a word?
jQuery Hello World alert
how to delete the access database value in jsp
how to delete the access database value in jsp
how to create alert message after data submitted into database
How to delete file in Java code?
How to delete data from MySQL?
how to delete a row in sql without using delete command.
How to delete objects using Hibernate?
alert
How to create an input box?
How to delete all Docker local Docker images
How to delete files in Java?
Create Delete Trigger in SQL
how to insert check box
Protect JSPs from direct access
Confirmation Mail
How to delete file in java ?
Use of delete() method in hibernate.
how to delete a jar from mobile
how to delete a jar from mobile
how to delete a jar from mobile
how to delete a jar from mobile
how to delete a jar from mobile
how to delete a jar from mobile
how to delete a jar from mobile
how to delete a jar from mobile
how to delete a jar from mobile
how to delete a jar from mobile

Ads