java - Inputs are not Storing in database while uploading an image -
i trying insert data's , image path database. image name , path getting stored in database , remaining column null can please me out
below servlet , im new jsp , 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 image_details(title,keyword,description,imagename,imagepath) values('" + title + "','" + keyword + "','" + message + "','" + name + "','" + path + "')"; // system.out.println(q); // st.executeupdate(q); string query = "insert 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 //request.setattribute("message", "file uploaded successfully"); } catch (exception ex) { request.setattribute("message", "file upload failed due " + 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 jsp page
<form action="imageupload" method="post" enctype="multipart/form-data"> <h3 style="font-family:raleway,sans-serif;">upload contents , 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 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 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>
as mentioned in documentation
returns value of request parameter string, or null if parameter not exist http://docs.oracle.com/javaee/6/api/javax/servlet/servletrequest.html#getparameter(java.lang.string)
you need check request object
Comments
Post a Comment