jsp form

hi

i have a form which takes name,age,sex and a photo of the user for registration. if in two forms i am able to succeed the task, but in the same form i am not able to do it. can anyone please help me with the data storing and file uploading script.

[264 byte] By [aschandra2704a] at [2007-11-27 10:47:40]
# 1

Which upload API are you using?

The Apache Commons FileUpload API is able to do that.

BalusCa at 2007-7-28 22:02:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

ok . just now i got it. need to download the apache api.. thank you for that

aschandra2704a at 2007-7-28 22:02:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

can you tell me how to do it

aschandra2704a at 2007-7-28 22:02:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Google for "apache commons fileupload api", open the first hit and in the "Documentation" section, checkout the "User guide" link.

BalusCa at 2007-7-28 22:02:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

can any one please help me with the code for user registration with photo upload using jsp

aschandra2704a at 2007-7-28 22:02:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Where exactly are you stucking while writing code?

BalusCa at 2007-7-28 22:02:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

hello

<%@ page language="java" import="java.sql.*" %>

<%@ page language="java" import="java.lang.*" %>

<%@ page language="java" import="java.text.*" %>

<%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %>

<%@ page import="java.io.FileWriter,java.io.IOException" %>

<%@ page session ="true"%>

<%

String adminId=(String) session.getAttribute("adminId");

if(adminId==null)

{

adminId="";

response.sendRedirect("newlogin.html");

}

%>

<%

String savePath = "", filepath = "", filename = "";

String contentType = "", fileData = "", strLocalFileName = "";

int startPos = 0, endPos = 0;

int BOF = 0, EOF = 0;

%>

<%!

//copy specified number of bytes from main data buffer to temp data buffer

void copyByte(byte [] fromBytes, byte [] toBytes, int start, int len)

{

for(int i=start;i<(start+len);i++)

{

toBytes[i - start] = fromBytes;

}

}

%>

<%

contentType = request.getContentType();

if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))

{

DataInputStream in = new DataInputStream(request.getInputStream());

DataInputStream in1 = in;

int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];

int byteRead = 0;

int totalBytesRead = 0;

while (totalBytesRead < formDataLength)

{

byteRead = in1.read(dataBytes, totalBytesRead, formDataLength);

totalBytesRead += byteRead;

}

//String file = new String(dataBytes);

//out.println("<br>File Contents:<br>////////////////////////////////////<br>" + file + "<br>////////////////////////////////<br>");

byte[] line = new byte[128];

if (totalBytesRead < 3)

{

return;//exit if file length is not sufficiently large

}

String boundary = "";

String s = "";

int count = 0;

int pos = 0;

//loop for extracting boundry of file

//could also be extracted from request.getContentType()

do

{

copyByte(dataBytes, line, count ,1);//read 1 byte at a time

count+=1;

s = new String(line, 0, 1);

fileData = fileData + s;

pos = fileData.indexOf("Content-Disposition: form-data; name=\""); //set the file name

if(pos != -1)

endPos = pos;

}while(pos == -1);

boundary = fileData.substring(startPos, endPos);

//loop for extracting filename

startPos = endPos;

do

{

copyByte(dataBytes, line, count ,1);//read 1 byte at a time

count+=1;

s = new String(line, 0, 1);

fileData = fileData + s;

pos = fileData.indexOf("filename=\"", startPos); //set the file name

if(pos != -1)

startPos = pos;

}while(pos == -1);

do

{

copyByte(dataBytes, line, count ,1);//read 1 byte at a time

count+=1;

s = new String(line, 0, 1);

fileData = fileData + s;

pos = fileData.indexOf("Content-Type: ", startPos);

if(pos != -1)

endPos = pos;

}while(pos == -1);

filename = fileData.substring(startPos + 10, endPos - 3);//to eliminate " from start & end

strLocalFileName = filename;

int index = filename.lastIndexOf("\\");

if(index != -1)

filename = filename.substring(index + 1);

else

filename = filename;

//loop for extracting ContentType

boolean blnNewlnFlag = false;

startPos = endPos;//added length of "Content-Type: "

do

{

copyByte(dataBytes, line, count ,1);//read 1 byte at a time

count+=1;

s = new String(line, 0, 1);

fileData = fileData + s;

pos = fileData.indexOf("\n", startPos);

if(pos != -1)

{

if(blnNewlnFlag == true)

endPos = pos;

else

{

blnNewlnFlag = true;

pos = -1;

}

}

}while(pos == -1);

contentType = fileData.substring(startPos + 14, endPos);

//loop for extracting actual file data (any type of file)

BOF = count + 1;

do

{

copyByte(dataBytes, line, count ,1);//read 1 byte at a time

count+=1;

s = new String(line, 0, 1);

fileData = fileData + s;

pos = fileData.indexOf(boundary, startPos);//check for end of file data i.e boundry value

}while(pos == -1);

EOF = count - boundary.length();

//file data extracted

out.println("<script>alert('Upload Successfull')</script>");

//create destination path & save file there

String appPath = application.getRealPath("/");

String destFolder = appPath + "images/";//change this as required

filename= destFolder + filename;

FileOutputStream fileOut = new FileOutputStream(filename);

fileOut.write(dataBytes, BOF, (EOF - BOF));

fileOut.flush();

fileOut.close();

//file saved at destination

//out.println("<br>File data : <br><br>**************************<br>" + (new String(dataBytes,startPos, (endPos - startPos))) + "<br><br>**************************");

String driver = "com.mysql.jdbc.Driver";

Class.forName(driver).newInstance();

Connection con=null;

ResultSet rst=null;

Statement stmt=null;

try

{

String url="jdbc:mysql://localhost:3306/ffr";

con=DriverManager.getConnection(url,"","");

stmt=con.createStatement();

}

catch(Exception e){

System.out.println(e.getMessage());

}

if(request.getParameter("action") != null)

{

String spid=request.getParameter("fpid");

String spname=request.getParameter("fpname");

String spadd1=request.getParameter("fpadd1");

String spadd2=request.getParameter("fpadd2");

String spcity=request.getParameter("fpcity");

String spstate=request.getParameter("fpstate");

String spzip=request.getParameter("fpzip");

String sapprove = "false";

String sdate = request.getParameter("fpdate");

String sprice = request.getParameter("fpprice");

String spath = "images/"+spid+".jpg";

stmt.executeUpdate("insert into ffr1(pid,pname,padd1,padd2,pcity,pstate,papprove,pdate,pprice,pfilename,pzip) values('"+spid+"','"+spname+"','"+spadd1+"','"+spadd2+"','"+spcity+"','"+spstate+"','"+sapprove+"','"+sdate+"','"+sprice+"','"+spath+"','"+spzip+"')");

String redirectURL = "template.jsp";

response.sendRedirect(redirectURL);

}

else

{

out.println("Error storing information");

}

}

else

{

out.println("Error in uploading ");

}

%>

can you see, this code is having both file upload and jdbc script. but there is some mistake. when used in differnet file like fileupload.jsp and processform.jsp the code is working fine. when i club is not working. please help me

aschandra2704a at 2007-7-28 22:02:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Why aren't you just using the FileUpload API?

Also rather use servlets instead of scriptlets.

BalusCa at 2007-7-28 22:02:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

can you please help me with the code as i am beginner and dont know where to start ..

aschandra2704a at 2007-7-28 22:02:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

I'll repeat myself:

> Google for "apache commons fileupload api", open the first hit and in the

> "Documentation" section, checkout the "User guide" link.

So:

1) Open http://www.google.com

2) Enter "apache commons fileupload api" in that input field.

3) Hit that button next to the field.

4) Explore the results, one of the first hits should point to http://jakarta.apache.org/commons/fileupload/

5) There is a link called User Guide here which points to http://jakarta.apache.org/commons/fileupload/using.html

That ought to be sufficient. You've written a heap of code here above which parses a simple multipart request, I may assume that you know at least how it works. Or is it just copypasted code and haven't you interpreted how it works?

BalusCa at 2007-7-28 22:02:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

both the file upload and data storing scripts were written by me. but when i combine them into one peice so that file upload will work along with form data upload. can you check the code and make any modifications please

aschandra2704a at 2007-7-28 22:02:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...