jsp-tags


jsp code to upload jgp files and save in mysql db


This is my servlet code
package classes;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
#WebServlet("/FileUploadDBServlet")
#MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
public class FileUploadDBServlet extends HttpServlet {
// database connection settings
private String dbURL = "jdbc:mysql://localhost:3306/studreg";
private String dbUser = "root";
private String dbPass = "";
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// gets values of text fields
String matric = request.getParameter("matric");
//String lastName = request.getParameter("lastName");
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
Part filePart = request.getPart("photo");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
Connection conn = null; // connection to the database
String message = null; // message will be sent back to client
try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
// constructs SQL statement
String sql = "INSERT INTO contacts (matric, photo) values (?,?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, matric);
//statement.setString(2, lastName);
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(2, inputStream);
//statement.setBlob(3, inputStream);
}
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "File uploaded and saved for transcript processing";
}
} catch (SQLException ex) {
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// sets the message in request scope
request.setAttribute("Message", message);
// forwards to the message page
getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
}
}
}
This is my jsp code
<%# page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%# page import ="java.sql.*" %>
<%#page import="classes.DbConn2"%>
<%#page import="classes.FileUploadDBServlet"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<form method ="post" action ="receipt.jsp" name="form1">
<p>
<input type="hidden" name="matric" value="<%=matric %>">
<td colspan="2"> <input name="invoiceno" type="hidden" value="<%=invoiceno%>" />
<input type="submit" value="Print Payment Receipt">
</p>
<p> </p>
</form>
<left>
<p>Click here to print <a target="_blank "href="http://www.collegesch.com/reg/receipt.jsp?matric=<%=matric%>&invoiceno=<%=invoiceno%>">Receipt</a>
<h1 align="left">File Upload for Transcript Request</h1>
<form method="post" action="FileUploadDBServlet" name="form2" enctype="multipart/form-data">
<table width="759" border="0">
<td colspan="2"> <input name="matric" type="hidden" value="<%=matric%>" />
<tr>
<td><p>Picture( jpeg, 450X500): </p>
</td>
<td><input type="file" name="photo" size="10"/></td>
<td><p>Result( jpeg, 450X500): </p>
</td>
<td><input type="file" name="photo1" size="10"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save">
</td>
</tr>
</table>
<p> </p>
</form>
</center>
 <br /> </td>
<td> </td>
</tr>
<tr>
<td width="4"> <br />
<br /> </td>
<td width="35"> </td>
<td width="505"> </td>
<td width="547"> </td>
</tr>
</table>
</body>
</html>
But after uploading a file, I get this error:
The requested URL /reg/FileUploadDBServlet was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Change
action="FileUploadDBServlet"
To
action="<%= request.getContextPath () %>/FileUploadDBServlet"
and try again - the relative path was probably not found under the path.

Related Links

when I run my jsp pgogram I got this exception error type Exception report
jstl formatNumber in input leads to NumberFormatException
Youtube onYouTubePlayerReady() function not firing
username and password cannot be resolved or is not a field
How page directive “extend” in jsp works?
Simple property not found?
Reusing JSP custom tags inside html
JSP Quiz process is not giving me the correct percentage scored
Refer the id variable of logic - iterate using jstl-el
Invalid tagdir attribute while web-fragment is used
JSP foreach tag for two variables
Adding Parameter to href tag before uri
Servlet exception stream closed jsp
creating and exporting a csv from Display tag table
bean write to number type id
restrict image file size in alloy UI

Categories

HOME
maven
client
cloud
webpack
image-processing
mediawiki
push-notification
smarty
layout
adb
paw-app
rubygems
packages
survey
umd
retrofit
ebean
azure-storage-tables
handsontable
timeout
usergrid
visual-studio-2005
dtrace
ups
clojurescript
finite-automata
propel
conemu
xlsxwriter
riot.js
clickonce
mustache.php
zurb-foundation-6
rundeck
h2db
applozic
saas
log4js-node
jspm
windows-server-2000
file-rename
bluestacks
bosh
twilio-api
copying
openoffice.org
wallpaper
mmenu
main
space-complexity
hilbert-curve
temporary-files
cookiecutter-django
galen
janrain
no-www
acoustics
wptoolkit
scorm
skeleton-css-boilerplate
auto-update
google-datalayer
menuitem
mu
statsd
brightcove
pdfclown
cudafy.net
imanage
iso8601
spring-mongodb
color-profile
django-scheduler
orthogonal
ableton-live
httplistener
ado.net-entity-data-model
file-writing
lua-5.1
python-stackless
sdhc
eclipse-clp
ios9.1
sortedlist
system.reflection
qpainter
mdt
hippomocks
balanced-payments
flexmojos
ora-00911
shellexecute
eventual-consistency
pygit2
free-variable
selected
pvrtc
cascalog
twrequest
netbeans-6.9
removeclass
custom-backend
xetex
noscript
nerddinner
private-members
defensive-programming
geneva-server
caching-application-block

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App