2015-01-08 3 views
-1

데이터베이스에서 레코드를 삭제해야하는 서블릿이 있습니다. 삭제 단추가있는 jsp 페이지에서 선택된 행을 가져오고 javascript를 사용합니다. 문제는 tomcat이 HTTP 상태 405 - HTTP 메서드 GET이 페이지에서 http://localhost:8084/DeleteRecord?i=35 (35는 삭제할 행임)이라는 URL에서 지원되지 않음을 보여줍니다.서블릿 데이터베이스에서 레코드를 삭제할 수 없습니다.

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> 

어떤 아이디어를 여기

내 서블릿입니까? 그것을 고정

+0

http://www.unixwiz.net/techtips/sql-injection.html – BalusC

답변

0

,이 코드를 필요로하는 서블릿 :

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

녹색 진드기를 클릭하여 자신의 답변을 수락 할 수 있습니다 .... –

0

난 당신이 GET 요청 (window.location.href = "http://localhost:8084/"+url+"?i="+i;)와 서블릿을 요구하고있다시피. 그래서 작동 시키려면 서블릿에 doGet() 메소드를 구현해야합니다.

은의 doGet과의 doPost는 또한 서블릿과 적절한 사용에 대한 자세한 정보를 원하시면이 https://stackoverflow.com/tags/servlets/info을 확인 서블릿

에 사용되는 방법이 doGet and doPost in Servlets를 참조하십시오.

관련 문제