Hello, need help from experts in jsp/servlet..

i have a program in jsp that needs to upload a file and save this in desired folder and send the filename to database..
and its working properly..
my problem is.. i dont know how to save the additional input text in database.. when i run it..

heres my index.jsp

<FORM action="upload.jsp" method="post"  enctype="multipart/form-data">
Name: <input type="text" name="name">
Address: <input type="text" name="address">
Image: <input type="file" name="file">
<input type="submit" value="submit">

and heres my upload.jsp

<%@ page import="java.io.*,java.sql.*,java.util.zip.*, org.apche.*" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>

// String Name = request.getParameter("name");                i try to put this code to get the value of the input text
// String Address = request.getParameter("address");          but nothings happen, instead it appears null in my DB.

String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File("C:/Servers/Apache7/webapps/MyProject/uploadedpics/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));


Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/error_page";
PreparedStatement psmnt = null;
FileInputStream fis;
InputStream sImage;

// if (Name != null && Address != null ) {    here i also try to input this code                     

connection = DriverManager.getConnection(connectionURL, "root", "password");
File f = new File("C:/Servers/Apache7/webapps/MyProject/uploadedpics/"+saveFile);
psmnt = connection.prepareStatement("insert into error_page(ERROR_IMG) values(?)");

// psmnt.setString(1, Name);
// psmnt.setString(2, Address);

fis = new FileInputStream(f);
psmnt.setString(3, saveFile);
int s = psmnt.executeUpdate();
System.out.println("Uploaded successfully !");
catch(Exception e){


actually i'd try to get the parameter of the input text.. like this.. String Name = request.getParameter("name);
but when i run it, it doesn't sae to my DB.. can you edit my code..or give me some idea..

i'm just new in JSP so its hard for me to understand some code :(

Edited by peter_budo: Moving to JSP section

6 Years
Discussion Span
Last Post by Philippe.Lahaie
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.