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.