I recently started learning java.
I am making a system to upload CD information with servlet and jsp, relying on the information on the net.
I want to upload an image in a form, enter a title, artist name and price, save the image in the images folder under WebContent, and save the entered content and file name in the database.

I don't get any error, and I can save to the database without any problem.
Images are not saved in the images folder. How do you write it to save it?

After that,/tmp/of "@MultipartConfig (location ="/tmp/")"
I don't know what it means. When I checked it on the net, it was a folder to temporarily save.
I can't get it right now, so please tell me about location = "/ tmp /".

Corresponding source code
package management;
import java.io.IOException;
import java.sql.Connection;
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;
import database.BaseDatabase;
/ **
 * Servlet implementation class addProduct
 * /
@WebServlet ("/ AddProduct")
@MultipartConfig (location = "/ tmp /")
public class AddProduct extends HttpServlet {
    protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
      Part part = request.getPart ("images");
      String fileName = part.getSubmittedFileName ();
      part.write (getServletContext (). getRealPath ("/ images") + "/" + fileName);
      String title = request.getParameter ("title");
      String artist = request.getParameter ("artist");String price = request.getParameter ("price");
      Connection con = null;
      PreparedStatement stmt = null;
      try {
        con = BaseDatabase.getConnection ();
        stmt = con.prepareStatement ("INSERT INTO products (title, artist, price, images) VALUE (?,?,?,?)");
        stmt.setString (1, title);
        stmt.setString (2, artist);
        stmt.setString (3, price);
        stmt.setString (4, fileName);
        stmt.executeUpdate ();
        request.setAttribute ("msg", "Upload completed");
      } catch (SQLException e) {
        e.printStackTrace ();
      } catch (Exception e) {
        e.printStackTrace ();
      } finally {
        try {
          if (stmt! = null) {stmt.close ();}
          if (con! = null) {con.close ();}
        } catch (SQLException e) {
          e.printStackTrace ();
        } catch (Exception e) {
          e.printStackTrace ();
      request.getRequestDispatcher ("newProduct.jsp"). forward (request, response);
<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%><! DOCTYPE html><html><head><meta charset = "UTF-8"><title>Product addition screen</title><jsp: include page = "headerMng.jsp" /><section>Add new products<p>${requestScope ['msg']}</p><form action = "AddProduct" method = "post" enctype = "multipart/form-data"><input type = "file" name = "images" required><input type = "text" name = "title" placeholder = "title" required><input type = "text" name = "artist" placeholder = "artist" required><input type = "text" name = "price" placeholder = "price" required><input type = "submit" value = "register"></form></section><jsp: include page = "footer.jsp" />
//headerMng.jsp<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%><link rel = "stylesheet" href = "css/reset.css"><link rel = "stylesheet" href = "css/style.css"><meta name = "viewport" content = "width = device-width, initial-scale = 1.0"></head><body><header>ShopLink   Add product  Customer management</header>
//footer.jsp<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%><footer>Copyright&copy;shoplink All rights reserved</footer></body></html>
What I tried

Try changing/tmp/in @MultipartConfig (location = "/ tmp /") to/images,
I checked all the folders called tmp on my PC.

Supplementary information (FW/tool version, etc.)

The PC is mac, the OS version is 10.15.7, the java version is 15, tomcat is 9.0 and eclipse is used.

  • Answer # 1

    I can't get it right now, so please tell me about location = "/ tmp /".

    The uploaded file is automatically stored somewhere on the server's file system. To specify that "somewhere"locationis.

    This time/ tmp /Since you specified, the uploaded file will be temporarily saved under this directory. After that, the Servletpart.write ()By doing so, the contents of that file are copied to any other file.

    / tmpIs a special directory, and the files under this will be deleted by the OS at the appropriate time (probably).