title: Upload a photo to a DB using servlets
body: <p>I am trying to make a Servlet that upload and insert an image to the Data base .
  this image is of blob type in the database .
  The code gives me this error :
  org.netbeans.modules.web.monitor.server.MonitorRequestWrapper cannot be cast to org.apache.tomcat.util.http.fileupload.RequestContext [Ljava.lang.StackTraceElement;@30ec706b</p>
  
  <p>here is a part of my code  :
   ## web.xml ##</p>
  
  <pre><code>        &lt;servlet&gt;
            &lt;servlet-name&gt;UploadServlet&lt;/servlet-name&gt;
            &lt;servlet-class&gt;Servlet.UploadServlet&lt;/servlet-class&gt;
              &lt;init-param&gt;
                &lt;param-name&gt;uploadDir&lt;/param-name&gt;
                &lt;param-value&gt;/tmp&lt;/param-value&gt;
               &lt;/init-param&gt;
           &lt;/servlet&gt;
  </code></pre>
  
  <h2>DataUtente</h2>
  
  <pre><code>    protected void processRequest(HttpServletRequest request, HttpServletResponse           
                                     response)
              throws ServletException, IOException {
          response.setContentType("text/html;charset=UTF-8");
          HttpSession session=request.getSession(false);
          PrintWriter out = response.getWriter();
  
          try {
  
              out.println("&lt;!DOCTYPE html&gt;");
              out.println("&lt;html&gt;");
              out.println("&lt;head&gt;");
              out.println("&lt;title&gt;Servlet DataUtente&lt;/title&gt;");            
              out.println("&lt;/head&gt;");
              out.println("&lt;body&gt;");
              Utente utente =(Utente)session.getAttribute("utente");
              out.println("&lt;h1&gt; Welcome  " + utente.getUsername() + "&lt;/h1&gt;");
  
              out.println("    &lt;form name=\"myWebForm\" action=\"UploadServlet\"                  
                               method=\"POST\" ENCTYPE=\"multipart/form-data\"&gt;\n" + 
                          " &lt;input type=\"file\" name=\"uploadField\" /&gt;\n" +
                          " &lt;INPUT TYPE='submit' VALUE='upload'&gt;"+
                          "&lt;/form&gt;");
              out.println("&lt;/body&gt;");
              out.println("&lt;/html&gt;");
          } finally {
              out.close();
          }
      }
  </code></pre>
  
  <h2>UploadServlet</h2>
  
  <pre><code>public class UploadServlet extends HttpServlet {
     DBManager dbman;
     private int maxFileSize = 50 * 1024;
     private int maxMemorySize = 4 * 1024;
     private File file ;
     private String dirName;
  
      public void init(ServletConfig config) throws ServletException{
          super.init(config);
  
            //read the uploadDir from the servlet parameters 
          dirName=config.getInitParameter("uploadDir");
          if(dirName== null){
              throw  new ServletException("Please supply uploadDir parameters");
          }
      }
      protected void processRequest(HttpServletRequest request, HttpServletResponse   
                                      response)
              throws ServletException, IOException {
               response.setContentType("text/html;charset=UTF-8");
               HttpSession session=request.getSession(false);
               PrintWriter out = response.getWriter();
          try {
              // Apache Commons-Fileupload library classes
              DiskFileItemFactory factory = new DiskFileItemFactory();
              //maximum size that will be stored in memory 
              factory.setSizeThreshold(maxMemorySize);
             //create a file upload handler
              ServletFileUpload upload  = new ServletFileUpload(factory);
             // maximum file size to be uploaded.
              upload.setSizeMax( maxFileSize );
  
              if (! ServletFileUpload.isMultipartContent(request)) {
                  System.out.println("sorry. No file uploaded");
                  return;
              }
  
                 // parse request
              List items = upload.parseRequest((RequestContext) request);
                 // get uploaded file
              FileItem file = (FileItem) items.get(0);
  
              // Connect to the DB
  
              dbman = new DBManager();
              Utente utente=(Utente)session.getAttribute("utente");
              //String query = "insert into photos values(?)";        
              PreparedStatement ps = dbman.connectToDB().prepareStatement("insert  into 
                          Utente(avatar) values(?) where Id='" + utente.getId() + "'");
  
  
              ps.setBinaryStream(1, file.getInputStream(), (int) file.getSize());
              ps.executeUpdate();
             // dbman.connectToDB().commit();
              dbman.connectToDB().close();
              out.println("Proto Added Successfully. &lt;p&gt; &lt;a href='listphotos'&gt;List Photos   
                           &lt;/a&gt;");
          }
          catch(Exception ex) {
              out.println( "Error ==&gt; " + ex.getMessage());
              out.print(ex.getStackTrace());
          }
       }
  </code></pre>
  
  <p>and here is the DDmanager:</p>
  
  <pre><code>public void  closeConnection(){
          try {
              dbConnection.close();
          } catch (SQLException ex) {
              ex.printStackTrace();
              Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
      public Connection connectToDB() throws SQLException, ClassNotFoundException{
           String dbString = null;
              Class.forName(driver);
              dbString = "jdbc:derby://localhost:1527/DB";
              dbConnection = (Connection) DriverManager.getConnection(dbString, userName, 
                                password);
          return dbConnection;
  
      }
  
      public boolean connectToDb() {
          //conneto a db 
          try {
              String dbString = null;
              Class.forName(driver);
              dbString = "jdbc:derby://localhost:1527/DB";
              dbConnection = (Connection) DriverManager.getConnection(dbString, userName, 
                                password);
  
          } catch (Exception e) {
              e.printStackTrace();
         //     System.out.print("erore");
              return false;
          }
          return true;
  
      }
  
      public ResultSet executeQuery(String command) {
          try {
              stmt = dbConnection.createStatement();
              ResultSet ress = stmt.executeQuery(command);
              return ress;
  
          } catch (Exception e) {
              e.printStackTrace();
              return null;
          }
      }
  
      public void  executeUpdateQuery(String command) {
          try {
              stmt = dbConnection.createStatement();
              int ress = stmt.executeUpdate(command);
              stmt.close();  
  
          } catch (Exception e) {
              e.printStackTrace();
  
          }
  </code></pre>