0

I am trying to insert data's and image path into database. But only the image name and path is getting stored in database and remaining column is null can you please help me out

below is my servlet and im new to jsp and servlet

    protected void ProcessRequest(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {
        throws ServletException, IOException {
    String Title = request.getParameter("title");
    String Keyword = request.getParameter("key");
    String Message = request.getParameter("mess");


            if(ServletFileUpload.isMultipartContent(request)){

                try {

                    List<FileItem> multiparts = new ServletFileUpload(new   DiskFileItemFactory()).parseRequest(request);



                    for(FileItem item : multiparts){

                        if(!item.isFormField()){

                            String name = new File(item.getName()).getName();

                            item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
                            String Path = "Upload/" + name;

                            Connection con =  connection.useConnection();
                           // Statement st = con.createStatement();
                           // String q = "insert into image_details(Title,Keyword,Description,ImageName,ImagePath) values('" + Title + "','" + Keyword + "','" + Message + "','" + name + "','" + Path + "')";
                           // System.out.println(q);
                           // st.executeUpdate(q);
                           String Query =  "insert into image_details(Title,Keyword,Description,ImageName,ImagePath) values (?,?,?,?,?)";                              
                           PreparedStatement ps = con.prepareStatement(Query);
                           ps.setString(1, Title);
                           ps.setString(2, Keyword);
                           ps.setString(3, Message);
                           ps.setString(4, name);
                           ps.setString(5, Path);
                           ps.executeUpdate();
                           System.out.println(ps);

                        }

                    }                                                     
                   //File uploaded successfully

                   //request.setAttribute("message", "File Uploaded Successfully");

                } catch (Exception ex) {

                   request.setAttribute("message", "File Upload Failed due to " + ex);

                } 




           }
           // response.sendRedirect("Admin_FileUpload.jsp?msg=1");
            request.getRequestDispatcher("/Admin_FileUpload.jsp").forward(request, response);


}

@Override
 protected void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {
       ProcessRequest(request,response);
 }
 @Override
 protected void doPost(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {
       ProcessRequest(request,response);
 }

}

This is my JSP page

  <form action="ImageUpload" method="post" enctype="multipart/form-data">
  <h3 style="font-family:Raleway,sans-serif;">UPLOAD YOUR CONTENTS AND IMAGES</h3>
  <hr/>
  <div class="form-group">
    <h4 style="font-family:Raleway,sans-serif">Title:</h4>
    <input type="text" class="form-control" name="title" placeholder="Enter the Title" required="">
  </div>
  <div class="form-group">
    <h4 style="font-family:Raleway,sans-serif">Keyword:</h4>
  <input type="text" class="form-control" required="" placeholder="Enter the Keyword" name="keyword">
  </div>
  <div class="form-group">
    <h4 style="font-family:Raleway,sans-serif">Description:</h4>
    <textarea class="form-control" name="mess" required=""  rows="5" cols="50"></textarea>
  </div>
   <div class="form-group">
    <h4 style="font-family:Raleway,sans-serif">Image Upload:</h4>
   <input type="file" name="Image" style="font-family:Raleway,sans-serif;font-size:17px" class="btn btn-default" required="" >
   </div>
   <div class="form-group">
   <input type="submit" class="btn btn-success" name="Upload" value="Upload" style="width:150px;height:45px">
   </div>
   </form>
Career
  • 13
  • 4
  • title, key and mess are formfields. Change your for-each loop to include if formfield then get title, key and mess and if not then get file. – rickz Oct 22 '15 at 15:52
  • http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet?rq=1 – rickz Oct 22 '15 at 15:53
  • I need to get both the file and the form field. I need to store the image path and the form fields in the database – Career Oct 22 '15 at 17:47
  • Did you look at the solution given by BalusC at the link I posted? He shows the for-each loop you need to use. – rickz Oct 23 '15 at 02:10
  • I tried it but it didnt work. I can get the image name into the database but not the inputs – Career Oct 23 '15 at 10:40
  • if i change the for-each loop and include if formfield then i cant get the image name and the path into the database – Career Oct 23 '15 at 10:42
  • Why not? What problem occurs? Can't you just use the same code you have above here to update? – rickz Oct 23 '15 at 16:11

1 Answers1

0

as mentioned in the documentation

Returns the value of a request parameter as a String, or null if the parameter does not exist http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)

You need to check your request object

nick sapronov
  • 1
  • 1
  • 1
  • 2