2017-03-08 43 views
0

我正在嘗試製作一個servlet將blob格式的圖片上傳到數據庫中,但我無法嘗試創建@WebServlet註釋。有問題在jsp中使用WebServlet註釋

當我提交我的表單時,它說沒有找到資源。

studentdashboard.jsp

<form class="form-inline" action="changedp" method="post" enctype="multipart/form-data"> 
    <div class="col-md-4"> 
     <div class="form-group"> 
      <input class="btn" type="file" name="dp" id="dp"> 
     </div> 
    </div> 
    <div class="col-md-4"> 
     <div class="form-group"> 
      <input class="btn btn-primary" type="submit" value="Upload File"> 
     </div> 
    </div> 
</form> 

changedp.java

import java.io.IOException; 
    import java.io.InputStream; 
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.PreparedStatement; 
    import java.sql.SQLException; 
    import javax.servlet.annotation.MultipartConfig; 
    import javax.servlet.annotation.WebServlet; 
    import javax.servlet.http.Part; 
    import javax.servlet.ServletException; 
    import javax.servlet.http.HttpServlet; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import javax.servlet.http.HttpSession; 

    @WebServlet(name = "changedp",urlPatterns = {"changedp"}) 
    @MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB 
    public class changedp extends HttpServlet { 

     // database connection settings 
     private String dbURL = "jdbc:mysql://localhost/cll"; 
     private String dbUser = "root"; 
     private String dbPass = "root"; 

     protected void doPost(HttpServletRequest request, 
       HttpServletResponse response) throws ServletException, IOException { 


      InputStream inputStream = null; // input stream of the upload file 

      // obtains the upload file part in this multipart request 
      Part filePart = request.getPart("dp"); 
      if (filePart != null) { 
       // prints out some information for debugging 
       System.out.println(filePart.getName()); 
       System.out.println(filePart.getSize()); 
       System.out.println(filePart.getContentType()); 

       // obtains input stream of the upload file 
       inputStream = filePart.getInputStream(); 
      } 

      Connection conn = null; // connection to the database 
      String message = null; // message will be sent back to client 

      try { 
       // connects to the database 
       DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 
       conn = DriverManager.getConnection(dbURL, dbUser, dbPass); 
       HttpSession session=request.getSession(); 
       // constructs SQL statement 
       String sql = "UPDATE student_login_tabl SET image=? where `sid` = '"+session.getAttribute("sid")+"'"; 
       PreparedStatement statement = conn.prepareStatement(sql); 
       if (inputStream != null) { 
        // fetches input stream of the upload file for the blob column 
        statement.setBlob(1, inputStream); 
       } 

       // sends the statement to the database server 
       int row = statement.executeUpdate(); 
       if (row > 0) { 
        message = "File uploaded and saved into database"; 
       } 
      } catch (SQLException ex) { 
       message = "ERROR: " + ex.getMessage(); 
       ex.printStackTrace(); 
      } finally { 
       if (conn != null) { 
        // closes the database connection 
        try { 
         conn.close(); 
        } catch (SQLException ex) { 
         ex.printStackTrace(); 
        } 
       } 
      } 
     } 
    } 
+3

也許顯示確切的錯誤信息 –

回答

0

您可以將您的註釋改爲

@WebServlet(name = "changedp", urlPatterns = {"/changedp"}) 

,它應該工作。

還存在短變異

@WebServlet("/changedp") 

通過類的方式,每個單詞的第一個常規字母應該大寫。

另外Servlet中的實例變量不是線程安全的,所以儘量避免將來使用它們。

表單操作:

<form id="form-inline" name="form-inline" action="${pageContext.request.contextPath}/changedp" method="post" enctype="multipart/form-data" accept-charset="utf-8"> 

<form id="form-inline" action="changedp" method="post" enctype="multipart/form-data"> 

嘗試切換註釋的順序。

@MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB 
    @WebServlet(name = "changedp", urlPatterns = {"/changedp"}) 
     public class changedp extends HttpServlet { 

另外,請檢查您的網址。您是否正確輸入http://localhost:8080/YourProjectName/changedp

請提供更多信息。