how to use confirmation alert box before delete in jsp?

Ads
 

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){}
    }
}

Ads









Related Tutorials/Questions & Answers:
how to use confirmation alert box before delete in jsp?
how to create own tld and use it in jspx file
Advertisements
how to create own tld and use it in jspx file
change appearance of alert box
JSP1
JSP2
Java Alert Box
How to set left and top positions of alert box using JQuery in the below code?
why alert box is not appearing in servlets...?
ModuleNotFoundError: No module named 'jspp'
ModuleNotFoundError: No module named 'JSPy'
Use of delete() method in hibernate.
Flex Alert Box example
how to write jsps and databasetables,umldiagrams in fuelmanagement system project
How to force cp to overwrite without confirmation
jQuery Hello World alert
jQuery alert()
how to delete a letter in a word?
How to delete file in Java code?
How to delete data from MySQL?
how to insert check box
How to delete objects using Hibernate?
how to delete the access database value in jsp
how to delete the access database value in jsp
How to delete all Docker local Docker images
how to create alert message after data submitted into database
How to delete files in Java?
how to use an editable combobox
Hibernate delete Query
how to delete a row in sql without using delete command.
how to use JOptionPane.YES_NO_OPTION

Ads