Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: The method getPart(String) is undefined for the type HttpServletRequest

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default The method getPart(String) is undefined for the type HttpServletRequest

    my servlet code is:


    package Servlet;
    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.ServletException;
    import javax.servlet.annotation.MultipartConfig;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.Part;
    /**
    * Servlet implementation class FileUploadDBServlet
    */
    @WebServlet("/FileUploadDBServlet")
    @MultipartConfig(maxFileSize = 10177215) // upload file's size up to 16MB
    public class FileUploadDBServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
    * @see HttpServlet#HttpServlet()
    */
    public FileUploadDBServlet() {
    super();
    }
    private String dbURL = "jdbc:mysql://localhost:3306/ecomtest";
    private String dbUser = "root";
    private String dbPass = "admin";
    protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    // gets values of text fields
    String firstName = request.getParameter("firstName");
    String lastName = request.getParameter("lastName");
    InputStream inputStream = null; // input stream of the upload file
    // obtains the upload file part in this multipart request
    Part filePart = request.getPart("photo");
    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);
    // constructs SQL statement
    String sql = "INSERT INTO datawithimage (first_name, last_name, photo) values (?, ?, ?)";
    PreparedStatement statement = conn.prepareStatement(sql);
    statement.setString(1, firstName);
    statement.setString(2, lastName);
    if (inputStream != null) {
    // fetches input stream of the upload file for the blob column
    statement.setBlob(3, 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();
    }
    }
    // sets the message in request scope
    request.setAttribute("Message", message);
    // forwards to the message page
    getServletContext().getRequestDispatcher("/submit.jsp").forward(request, response);
    }
    }
    }




    jar files are following

    1.commons-fileupload-1.2.1.jar
    2.commons-io-1.4.jar
    3.javax.servlet-3.0.jar
    4.javax.servlet-api-3.0.1.jar
    5.mysql-connector-java-5.1.7-bin.jar


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: The method getPart(String) is undefined for the type HttpServletRequest

    Welcome to the forums. Please read the Forum Guidelines for information on how to properly format your code

Similar Threads

  1. The operator / is undefined for the argument type(s)
    By boyjam2 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 28th, 2013, 04:12 PM
  2. Replies: 0
    Last Post: December 11th, 2012, 05:08 PM
  3. The operator + is undefined for the argument type(s) String, void
    By Proletariat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 7th, 2012, 12:10 AM
  4. Replies: 3
    Last Post: October 28th, 2011, 04:42 PM
  5. he operator / is undefined for the argument type(s) String, int
    By mtbr00x in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 8th, 2009, 08:34 PM