deleting data from mysql database trough servlet / jsp
Hi guys, i have my servlet that calls mysql database and it looks like this:
adminController.java:
Code:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String operation = request.getParameter("operation");
if(operation == null)
{
ArrayList<PageData> list;
ArrayList<PageData> subList;
Iterator<PageData> pageIterator;
list = dao.getPagesByParent("main");
pageIterator = list.iterator();
StringBuffer st = new StringBuffer();
st.append("<ul>");
while(pageIterator.hasNext())
{
PageData pageData = pageIterator.next();
st.append("<li>");
st.append(pageData.getTitle());
st.append(" <a href=\"adminController?operation=edit&id=");
st.append( pageData.getId());
st.append("\">Edit</a>");
st.append(" <a href=\"adminController?operation=delete&id=");
st.append( pageData.getId());
st.append("\">Delete</a>");
st.append("</a></li>");
subList = dao.getPagesByParent(pageData.getId());
Iterator<PageData> subpageIterator = subList.iterator();
st.append("<ul>");
while(subpageIterator.hasNext())
{
PageData subPageData = subpageIterator.next();
st.append("<li>");
st.append(subPageData.getTitle());
st.append(" <a href=\"adminController?operation=edit&id=");
st.append( subPageData.getId());
st.append("\">Edit</a>");
st.append(" <a href=\"adminController?operation=delete&id=");
st.append( subPageData.getId());
st.append("\">Delete</a>");
st.append("</a></li>");
}
st.append("<li><a href=\"adminController?operation=add&id=");
st.append( pageData.getId());
st.append("\">Add page</a></li>");
st.append("</ul>");
st.append("</li>");
}
st.append("<li><a href=\"adminController?operation=add&id=main\">");
st.append("Add page</a></li>");
st.append("</ul>");
RequestDispatcher dispatcher = request.getRequestDispatcher("/mainadmin.jsp");
request.setAttribute("menu", st.toString());
dispatcher.forward(request, response);
}
else if(operation.equals("add"))
{
RequestDispatcher dispatcher = request.getRequestDispatcher("/add.jsp");
dispatcher.forward(request, response);
}
else if (operation.equals("delete")) {
RequestDispatcher dispatcher = request.getRequestDispatcher("/delete.jsp");
dispatcher.forward(request, response);
}
else if (operation.equals("deletepage")) {
PageData pageData = new PageData();
pageData.setId(request.getParameter("id"));
dao.deletePage(pageData);
response.sendRedirect("adminController");
}
}
and my deletePage method looks like this:
Code:
public void deletePage(PageData delete) {
String firstQuery ="Delete FROM pages Where ID= '"+delete.getId()+"';";
try {
statement = connection.createStatement();
statement.executeUpdate(firstQuery);
System.out.println("Expense is deleting");
}
catch (SQLException e) {
System.out.println("Expense isn't deleting - SQLException");
e.printStackTrace();
}
}
my delete.jsp is as follows:
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add pages</title>
</head>
<body>
<form action="adminController">
<p>
<input type="hidden" name="operation" value="deletepage">
<input type="hidden" name="parentid" value="<%= request.getParameter("id") %>">
Enter ID of page you want to delete:<input name="id"><br>
<input type="submit">
</p>
</form>
</body>
</html>
my mainadmin.jsp:
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= request.getAttribute("menu") %>
</body>
</html>
if you run this code you'll see that my delete buttons stands after each page name.
what i want to do is by pressing the delete button near the page to delete this page, there should be smth like "are you sure you want to delete?", submit button"delete". but what i've managed to do looks like this(as in my code): whenever delete button is pressed, you have to enter id of page you want to delete, so no matter what delete button i press, it requests for id. but i just want to delete it right away.
can anyone help?
Re: deleting data from mysql database trough servlet / jsp
oh, i got it, don't worry guys.