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

Embedding Tag Files in a JAR
Errors when the compiler is trying to build a custom JSP tag based on an Java Bean
Creating nested custom tag using jsp2 tag files
Unable to access session on JSP
Dealing with <html:options>
No tag “tableFacade” defined in tag library imported with prefix “jmesa”
how to refer values of one jsp file to other jsp
incorrect username and passwords
creating the welcome page
How to use list object in <c:forEach tag?
How to call a Servlet when I click on a Hyperlink
EL Messages on JSP page
showmodaldialog function in ie8
displaytag export options banner - place at top of table
JSP custom tag library (Unable to find setter method for the attribute)
JSP custom tag library (Passing Attributes)

Categories

HOME
vim
netbeans
gremlin
raspberry-pi
framework7
yahoo-oauth
packages
binary-tree
angular-ui-bootstrap
in-app-purchase
bs4
cross-validation
tomcat6
timeout
phaser
visual-studio-2005
decimal
oracle-coherence
amazonsellercentral
scriptcs
chromium-embedded
autosys
trading
user-interaction
spring-mybatis
large-file-upload
libuv
webkitspeechrecognition
google-cloud-nl
directx-10
binary-data
yadcf
http-get
phpfreechat
amazon-kinesis-kpl
jmonkeyengine
retina-display
uft-api
lumberjack
sfdc
ibpy
firebase-admin
eigenvalue
nxlog
isbn
dotcover
zip4j
prolog-setof
blogengine.net
akka-cluster
sqlbulkcopy
cudafy.net
background-service
angular-strap
azure-sdk
hexdump
livequery
pyke
energy
srand
marching-cubes
sframe
cakephp-3.1
event-bubbling
system32
csquery
qpainter
codeigniter-url
codeigniter-routing
eol
aapt
app42
cloud-connect
intentservice
mysqltuner
geos
ccss
inbox
dbconnection
batterylevel
html4
rabl
mqx
sabredav
bluepill
getmessage
ticoredatasync
uiviewanimation-curve
psi
netbeans-7.1
krl
xfbml
cxxtest
gamequery
ext3
memory-size
adrotator
boost-smart-ptr

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