title: Insert Image path to a database using servlet
body: <p>I want to insert an image to the database using servlet. i have created images folder inside web pages and i want sent the to that folder. here is my code.</p>
  
  <p><strong>CompanyReg.jsp</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;
  &lt;meta name="description" content="Colorlib Templates"&gt;
  &lt;meta name="author" content="Colorlib"&gt;
  &lt;meta name="keywords" content="Colorlib Templates"&gt;
  &lt;title&gt;New Member?&lt;/title&gt;
  &lt;link href="resources/CompanyReg/vendor/mdi-font/css/material-design-iconic-font.min.css" rel="stylesheet" media="all"&gt;
  &lt;link href="resources/CompanyReg/vendor/font-awesome-4.7/css/font-awesome.min.css" rel="stylesheet" media="all"&gt;    
  &lt;link href="https://fonts.googleapis.com/css?family=Poppins:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet"&gt;
  &lt;link href="resources/CompanyReg/vendor/select2/select2.min.css" rel="stylesheet" media="all"&gt;
  &lt;link href="resources/CompanyReg/vendor/datepicker/daterangepicker.css" rel="stylesheet" media="all"&gt;
  &lt;link href="resources/CompanyReg/css/main.css" rel="stylesheet" media="all"&gt;
  &lt;link rel="shortcut icon" href="resources/CompanyReg/favicon.ico"&gt;
  &lt;link rel="stylesheet" type="text/css" href="resources/CompanyReg/css/normalize.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" href="resources/CompanyReg/css/demo.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" href="resources/CompanyReg/css/component.css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;div class="page-wrapper bg-gra-02 p-t-130 p-b-100 font-poppins"&gt;
      &lt;div class="wrapper wrapper--w680"&gt;
          &lt;div class="card card-4"&gt;
              &lt;div class="card-body"&gt;
                  &lt;h2 class="title"&gt;Registration Form&lt;/h2&gt;
                  &lt;form method="POST" action="CompanyReg" enctype='multipart/form-data'&gt;
                      &lt;div class="row row-space"&gt;
                          &lt;div class="col-2"&gt;
                              &lt;div class="input-group"&gt;
                                  &lt;label class="label"&gt;Company Name&lt;/label&gt;
                                  &lt;input class="input--style-4" type="text" name="company_name"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="row row-space"&gt;
                          &lt;div class="col-2"&gt;
                              &lt;div class="input-group"&gt;
                                  &lt;label class="label"&gt;Established Date&lt;/label&gt;
                                  &lt;div class="input-group-icon"&gt;
                                      &lt;input class="input--style-4 js-datepicker" type="text" name="est_year"&gt;
                                      &lt;i class="zmdi zmdi-calendar-note input-icon js-btn-calendar"&gt;&lt;/i&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="row row-space"&gt;
                          &lt;div class="col-2"&gt;
                              &lt;div class="input-group"&gt;
                                  &lt;label class="label"&gt;Address&lt;/label&gt;
                                  &lt;input class="input--style-4" type="text" name="address"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="col-2"&gt;
                              &lt;div class="input-group"&gt;
                                  &lt;label class="label"&gt;Email&lt;/label&gt;
                                  &lt;input class="input--style-4" type="text" name="email"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="col-2"&gt;
                              &lt;div class="input-group"&gt;
                                  &lt;label class="label"&gt;Phone Number&lt;/label&gt;
                                  &lt;input class="input--style-4" type="text" name="phone"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="col-2"&gt;
                              &lt;div class="input-group"&gt;
                                  &lt;label class="label"&gt;Password&lt;/label&gt;
                                  &lt;input class="input--style-4" type="Password" name="password"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="col-2"&gt;
                              &lt;div class="input-group"&gt;
                                  &lt;label class="label"&gt;Repeat Password&lt;/label&gt;
                                  &lt;input class="input--style-4" type="Password" name="repeatPassword"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;                            
                      &lt;/div&gt;
                      &lt;div class="col-2"&gt;
                              &lt;div class="input-group"&gt;
                                  &lt;label class="label"&gt;Upload Company Photo&lt;/label&gt;
                                  &lt;div class="box"&gt;
                                      &lt;input type="file" name="photo" id="file-1" class="inputfile inputfile-1" data-multiple-caption="{count} files selected" multiple style="display: none;" /&gt;
                                      &lt;label for="file-1"&gt;&lt;svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"&gt;&lt;path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/&gt;&lt;/svg&gt; &lt;span&gt;Choose a file&amp;hellip;&lt;/span&gt;&lt;/label&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;div class="p-t-15"&gt;
                          &lt;button class="btn btn--radius-2 btn--blue" type="submit"&gt;Submit&lt;/button&gt;
                      &lt;/div&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;script src="resources/CompanyReg/vendor/jquery/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="resources/CompanyReg/vendor/select2/select2.min.js"&gt;&lt;/script&gt;
  &lt;script src="resources/CompanyReg/vendor/datepicker/moment.min.js"&gt;&lt;/script&gt;
  &lt;script src="resources/CompanyReg/vendor/datepicker/daterangepicker.js"&gt;&lt;/script&gt;
  &lt;script src="resources/CompanyReg/js/global.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>this is my servlet code</strong></p>
  
  <pre><code> package Company;
  
   import java.io.File;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.PrintWriter;
   import java.sql.Connection;
   import java.sql.DriverManager;
   import java.sql.PreparedStatement;
   import java.sql.ResultSet;
   import javax.servlet.ServletException;
   import javax.servlet.annotation.MultipartConfig;
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
   import javax.servlet.http.Part;
   @MultipartConfig(maxFileSize = 16177215) 
   public class CompanyReg extends HttpServlet {
  
  private static final String SAVE_DIR ="images";
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      try (PrintWriter out = response.getWriter()) {
          /* TODO output your page here. You may use following sample code. */
          out.println("&lt;!DOCTYPE html&gt;");
          out.println("&lt;html&gt;");
          out.println("&lt;head&gt;");
          out.println("&lt;title&gt;Servlet CompanyReg&lt;/title&gt;");            
          out.println("&lt;/head&gt;");
          out.println("&lt;body&gt;");
          out.println("&lt;h1&gt;Servlet CompanyReg at " + request.getContextPath() + "&lt;/h1&gt;");
          out.println("&lt;/body&gt;");
          out.println("&lt;/html&gt;");
      }
  }
  
  // &lt;editor-fold
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
  
      String savePath = "F:\\JAVA EE Final\\Java Final\\web" + File.separator +SAVE_DIR;
      File fileSaveDir = new File(savePath);
  
      String company_name = request.getParameter("company_name");
      String company_email = request.getParameter("email");
      String est_date = request.getParameter("est_year");
      String company_address = request.getParameter("address");
      String company_pasword = request.getParameter("password");
      String company_contactno = request.getParameter("phone");
      Part part = request.getPart("photo");
      String filename = extractFileName(part);
      try 
      {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/javanew","root","");
          PreparedStatement pst = con.prepareStatement(" insert into company (Name,Email,Est_date,Address,Password,ContactNo,Photo) values(?,?,?,?,?,?,?)");
                      pst.setString(1, company_name);
                      pst.setString(2, company_email);
                      pst.setString(3, est_date);
                      pst.setString(4, company_address);
                      pst.setString(5, company_pasword);
                      pst.setString(6, company_contactno);
                      String filePath = savePath + File.separator + filename;
                      pst.setString(7, filePath);            
          int rs=pst.executeUpdate();            
          if(rs&gt;0)
          {
              getServletContext().getRequestDispatcher("").forward(request, response);
          }
      }
      catch (Exception e)
      {
          PrintWriter out = response.getWriter();
          out.print(e);
      }
  }
  @Override
  public String getServletInfo() {
      return "Short description";
  }
  private String extractFileName(Part part) {
  
      String contenDisp = part.getHeader("content-disposition");
      String [] items = contenDisp.split(";");
      for (String s : items)
      {
          if (s.trim().startsWith("filename"))
          {
              return s.substring(s.indexOf("=") + 2, s.length()-1);
          }
      }
      return "";       
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;CompanyReg&lt;/servlet-name&gt;
      &lt;servlet-class&gt;Company.CompanyReg&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;CompanyReg&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/CompanyReg&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;session-config&gt;
      &lt;session-timeout&gt;
          30
      &lt;/session-timeout&gt;
  &lt;/session-config&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>my database shows the path correctly but the image not be seen in that folder. so plese tell me what to do</p>