Servlet 无法从数据库中删除记录



我有一个servlet,应该从我的数据库中删除记录。我从带有删除按钮的 jsp 页面获取所选行,并使用 javascript。问题是tomcat显示HTTP状态405 - 此URL不支持HTTP方法GET,在页面上:http://localhost:8084/DeleteRecord?i=35(35是要删除的选定行)。

这是我的 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";
    }
}  

以及用户选择要删除的记录的all_proteins.jsp:

<%@ 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>

有什么想法吗?

修复了它,servlet 需要以下代码:

statement = connection.createStatement();
deleteProtein = "DELETE FROM protein WHERE i = '"+i+"'";            
int j = statement.executeUpdate(deleteProtein);

正如我所看到的,您正在使用GET请求(window.location.href = "http://localhost:8084/"+url+"?i="+i;)调用servlet。因此,为了使它正常工作,您应该在 Servlet 中实现 doGet() 方法。

在 Servlet 中查看 doGet 和 doPost,了解如何在 Servlet 中使用 doGet 和 doPost。

另请查看此 https://stackoverflow.com/tags/servlets/info 以获取有关 Servlet 和正确用法的更多信息。

相关内容

  • 没有找到相关文章

最新更新