-1

I have a servlet that should delete record from my database. I get the selected row from a jsp page with a delete button and I use javascript. The problem is that tomcat shows HTTP Status 405 - HTTP method GET is not supported by this URL, on page: http://localhost:8084/DeleteRecord?i=35 (35 is the selected row to be deleted).

Here is my Servlet:

package package_ergasia;

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

public class DeleteRecord extends HttpServlet 
{
   @Override
   public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException 
   {
    response.setContentType("text/html");    
    Connection connection= null;    
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "ergasia";    
    String user = "root";
    String password = "password"; 
    PreparedStatement deleteProtein = null;
    ResultSet resultSet = null;  
    ArrayList al = null;
    PrintWriter out = response.getWriter();
    int i;

        try {
            connection = DriverManager.getConnection(url + dbName, user, password);
            i = Integer.parseInt(request.getParameter("i"));           
            deleteProtein = connection.prepareStatement("DELETE FROM protein WHERE i = '"+i+"'");            
            resultSet = deleteProtein.executeQuery();

            RequestDispatcher view = request.getRequestDispatcher("http://localhost:8084/secured/all_proteins.jsp");
            view.forward(request, response);          

        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("Error!");
        }

   }

    @Override
    public String getServletInfo() {
        return "info";
    }
}  

and the all_proteins.jsp where the user selects the record for delete:

<%@ page language="java" import="java.sql.*,java.util.* "%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
        <link rel="stylesheet" type="text/css" href="../CSS/mystyle.css">
        <title>Database management</title>       
    </head>
    <body class="menu">
          <h1>Welcome to KSPO Database, the Database of Porins with known 3D Structure</h1>           
          <script type="text/javascript">
              function deleteRecord(i){
                  url = "DeleteRecord";
                  window.location.href = "http://localhost:8084/"+url+"?i="+i;
              }
              function editRecord(i){
                  url = "EditRecord";
                  window.location.href = "http://localhost:8084/"+url+"?i="+i;
              }
          </script>

        <table border="1">             
<% 


                int count = 0;    
                int i=-1;
                if (request.getAttribute("protein_data") != null) {
                    ArrayList al1 = (ArrayList) request.getAttribute("protein_data");
                    request.getAttribute("protein_data");


                   Iterator itr = al1.iterator();

                    while (itr.hasNext()) {
                        count++;
                        i++;
                        ArrayList pList = (ArrayList) itr.next();
%>                


    <tr>
                <td><%=pList.get(0)%></td>
                <td><%=pList.get(1)%></td>
                <td><%=pList.get(2)%></td>   
                <td><%=pList.get(3)%></td>
                <td><input type="submit" value="Edit" name="edit" onclick="editRecord(<%=pList.get(4)%>);"></td>
                <td><input type="submit" value="Delete" name="delete" onclick="deleteRecord(<%=pList.get(4)%>);"></td>
    </tr>
 <%
                   }
                }  
%>
</table>
</body>         
</html>

Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Luna NA
  • 5
  • 1
  • 1
  • 7

2 Answers2

0

Fixed it, the servlet needed this code:

statement = connection.createStatement();
deleteProtein = "DELETE FROM protein WHERE i = '"+i+"'";            
int j = statement.executeUpdate(deleteProtein);
Luna NA
  • 5
  • 1
  • 1
  • 7
0

As i see you are calling the servlet with a GET request (window.location.href = "http://localhost:8084/"+url+"?i="+i;). So in order to get it working you should implement the doGet() method in your Servlet.

See this doGet and doPost in Servlets on how doGet and doPost is used in Servlets

Also check out this https://stackoverflow.com/tags/servlets/info for more info about Servlets and proper usage.

Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125