简体   繁体   中英

Having issues making WebServlet annotation in jsp

I am trying to make a servlet to upload a picture in blob format into the database, but I am having trouble trying to make the @WebServlet annotation.

When I submit my form, it says resource not found.

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();
                    }
                }
            }
        }
    }

You can change your annotation to

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

and it should work.

Also there is short variant

@WebServlet("/changedp")

By convention first letter of each internal word of the class should be capitalized.

Also instance variables in Servlet are not thread safe so try to avoid them in future.

Form action:

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

or

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

Try to switch the order of annotations.

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

Also check your URL. Do you type it correctly http://localhost:8080/YourProjectName/changedp

and please provide more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM