package com.gtlsoftware;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.sql.PreparedStatement;
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10, // 10MB
maxRequestSize=1024*1024*50) // 50MB
public class MenuServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MenuServlet()
{
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Connection con;
ResultSet rs;
PreparedStatement pst;
//INSERT INTO DATABASE
PrintWriter pw=response.getWriter();
pw.print("hello");
String name=request.getParameter("name");
pw.println(name);
System.out.println(name);
String description = request.getParameter("description");
String image = request.getParameter("image");
String category=request.getParameter("category");
String unit=request.getParameter("unit");
String units=request.getParameter("units");
String price=request.getParameter("price");
byte[] b=null;
try
{
con=MyConnection.getConnection();
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
List items = sfu.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
if (!item.isFormField())
{
b = item.get();
}
}
pst=con.prepareStatement("insert into menu(menu_name,description,image,category,unit,units,price) values(?,?,?,?,?,?,?)");
pst.setString(1, name);
pst.setString(2, description);
pst.setBytes(3,b);
pst.setString(4, category);
pst.setString(5, unit);
pst.setString(6,units);
pst.setString(7,price);
pst.executeUpdate();
System.out.println("inserted successfully");
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (FileUploadException e)
{
System.out.println(e.getMessage());
// TODO: handle exception
}
}
}
I did coding image is inserted .but when i am use in form enctype="multipart/form-data then null values goes to next page i also add commons logging and file upload jar and if i remove enctype then value inserts but image not insert am taking image in longblob
and jsp page is menu.jsp
<body>
<center>
<div>
<table border="0" cellspacing="10" cellpadding="5" style="text-align: left">
<tr>
<td><%@ include file="header.jsp" %></td>
</tr>
<tr>
<td>
<div class="main_div">
<fieldset>
<form name="menuFrm" action="MenuServlet" method="post" onsubmit="reurn(validate())" enctype="multipart/form-data" >
<table cellspacing="7" cellpadding="2" align="center" border="0" style="font-family: sans-serif;font-weight: normal;font-size: medium">
<tr>
<td colspan="2" style="background-color: black;color: white;font-family: Centaur;font-size: 20px;text-align: center;">
<h4>Menu</h4>
</td>
</tr>
<tr>
<td>Menu Id :</td>
<td><input type="text" name="menuId" id="MenuId" value="<%=++cnt %>" tabindex="1" size="30%" placeholder="enter menu name"></td>
</tr>
<tr>
<td>Name :</td>
<td><input type="text" name="name" id="name" value="hdsuidd" tabindex="1" size="30%" placeholder="enter menu name"></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="6" cols="30" name="description" size="30%" tabindex="4" id="description" size="30%" placeholder="enter description"></textarea></td>
</tr>
<tr>
<td>Image:</td>
<td><input type="file" class="txtset" id="image" name="image" tabindex="5"/></td>
</form>
</tr>
<tr>
<td>Category:</td>
<td>
<select id="category" class="txtset" name="category">
<option selected="selected">select category</option>
<option value="veg">Veg</option>
<option value="non-veg">Non-Veg</option>
</select>
</td>
</tr>
<tr>
<td>Units:</td>
<td><input type="text" name="unit" id="unit" class="txtset" tabindex="6" width="10%" onkeyup="myNum()"/>
<select id="units" name="units">
<option selected="selected">select units</option>
<option value="kg">KG</option>
<option value="gram">GRAM</option>
<option value="unit">Unit</option>
</select>
</td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" class="txtset" id="price" onkeyup="myNum()" tabindex="7" width="30%" height="10%"> /-Rs.</td>
</tr>
<tr>
<td><font color='white'> <DIV id="une" style="background-color: red;font-weight: bold;"> </DIV> </font></td>
</tr>
<tr>
<td><input type="submit" value="Add" style="font-family: sans-serif;font-size: large;text-align: center;width" onclick="valid();check()"/></td>
<td><input type="reset" value="Cancel" style="font-family: sans-serif;font-size: large;text-align: center;width" onclick="reset()"/></td>
</tr>
<tr>
<td colspan="2">
<br>
<br>
<a href="edit.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px;">
<u>Edit</u></a>
<a href="update.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px;">
<u>Update</u></a>
<a href="delete.jsp" style="color: black;text-align:center;font-family: Centaur;font-size: 20px";>
<u>Delete</u></a>
</td>
</tr>
</table>
</form>
<%
con.close();
%>
</fieldset>
</div>
</td>
</tr>
<tr>
<td><%@ include file="footer.jsp" %></td>
</tr>
</table>
</div>
</center>
</body>