0

JSP

<form action="AddCategoryServlet" id="AddCategoryForm" target="_self" method="post">
        <table>
        <tr> 
            <td> <div align="center" class="group">
                 <input type="text" id="CName" name="texCname" required />
                 <span class="highlight"></span>
                 <span class="bar"></span>
                 <label id="CNamecheck">Category Name</label>
                 </div>
        </tr>
        <tr>
            <td> <div align="center" class="group">
                 <input type="text" id="CDescript" name="texCdescript" required />
                 <span class="highlight"></span>
                 <span class="bar"></span>
                 <label id="CDescriptcheck">Category Description</label>
                 </div>
        </tr> 
        <tr>
            <td> <div align="center" class="group">
                 <input type="text" id="CImage" name="texCImage" required/>
                 <span class="highlight"></span>
                 <span class="bar"></span>
                 <label id="CImagecheck">File Path</label>
                 <input style="display:none;" type="file" id="file" name="file"/>
                 <input style="position:absolute;top:10px;left:315px;font-size:10px;" type="button" value="Choose Image" id="uploadbutton" class="myButton" />
                 </div>  
        </tr>               
        </table>
        <input type="submit" value="Add Category" class="myButton" />
        </form>

Choose Image button when clicked fires click event for input type=file using jquery as such-:

$("#uploadbutton").click(function(){
    $("#file").click();
});

My servlet for putting file into databse is -:

try
    {
        String Name,Description=null;
        Name=request.getParameter("texCname");
        Description=request.getParameter("texCdescript");

        InputStream inputStream= null;
        Part filePart= null;
        filePart= request.getPart("file");

        if (filePart != null) 
        {
            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());

            inputStream = filePart.getInputStream();
        }

        Connection con= BaseDAO.getConnection();

        PreparedStatement pst = con.prepareStatement("INSERT INTO CATEGORY(Category_Name, Image, Description) VALUES (?,?,?)");
        pst.setString(1,Name);
        pst.setBlob(2,inputStream);
        pst.setString(3,Description);
        pst.executeUpdate();


    }
    catch(ClassNotFoundException | SQLException e)
    {
        e.printStackTrace();
    }

I am getting an error of Column 'Image' cannot be null. Upon debugging I found that value of variable filePart is null i.e. it is not receiving parameter with name="file" from jsp. Can someone please point out where I'm going wrong.

1 Answers1

0

You cannot handle plain input types like text and input type file at a time in a single form, you need special libraries to handle such request.

You need to write enctype="multipart/form-data" within you form as,

 <form action="AddCategoryServlet" id="AddCategoryForm" target="_self" method="post" enctype="multipart/form-data" >
     ........
 </form>

However, note that by writing enctype="multipart/form-data" you won't be able to handle plain input i.e. input type="text"

So if you want a simple approach create 2 seperate forms where contains enctype="multipart/form-data" when you send image and one as you have specified in your question which only contains text data.

Saumil
  • 2,521
  • 5
  • 32
  • 54