my dog learned polymorphism*
The moose likes Servlets and the fly likes how to upload using html or javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to upload using html or javascript?" Watch "how to upload using html or javascript?" New topic
Author

how to upload using html or javascript?

J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
hi i have code of type




what i require is suppose i press submit button. the file selected using browse should be uploaded in some location.

can anybody give hints like what i can do??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42932
    
  68
Since you know about the proper HTML element already, I suppose this question is about the server side? If the receiving end is a Java servlet, then you can use a library like Apache Commons FileUpload to deal with it; it's quite easy to use.
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
this is my front end html code




this is my jsp code (server side)



this is the error i am getting.
any idea why is it so?

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 9 in the generated java file
Only a type can be imported. org.apache.commons.fileupload.servlet.ServletFileUpload resolves to a package

An error occurred at line: 10 in the generated java file
Only a type can be imported. org.apache.commons.fileupload.disk.DiskFileItemFactory resolves to a package

An error occurred at line: 34 in the jsp file: /upload_file_multipale.jsp
String literal is not properly closed by a double-quote
31: item.write(savedFile);
32:
33:
34: out.println("<tr><td><b>Your file has been saved
35: at the loaction:</b></td></tr><tr><td><b>"+config.getServletContext().getRealPath
36: ("/")+"uploadedFiles"+"\\"+itemName+"</td></tr>");
37: } catch (Exception e) {


J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
i think this is a issue realted to adding some external jar to be added can anyone tell me which jar has to be added?
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
am using tomacat server does anybody knows which are the external jars that has to be added and which one's to keep in lib folder?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42932
    
  68
Is your first name really "january"? If not, we have a policy on screen names here at JavaRanch. Basically, it must consist of a real-sounding first name and last name, and not be obviously fictitious. If yours does not conform with it, please take a moment to change it, which you can do using the "My Profile" link at the top of the page.

Obviously, you need to have the FileUpload jar file in the WEB-INF/lib folder of your web app. It's usually called something like "commons-fileupload-1.2.1.jar".

It's also bad design to keep file I/O code in your JSP pages. It makes proper error handling (amongst other things) hard to do. You should move that code to a servlet instead.
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
yes my first name is january.


After adding the jar file to the LIB folder also am not able to get the files uploaded to the given path


The Jsp is executing and just showing

your files uploaded..

but acutal uploading is not happening.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Do not write Java code in JSP files. Scriptlets indicate a bad design and are receipt for disaster. They are highly discouraged.

Write Java code in Java classes. Servlets are highly suitable for your requirement.

Read the 'User Guide' and 'Frequently Asked Questions' sections of http://commons.apache.org/fileupload how to use it properly.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Since this is not about HTML, and should be done in a servlet, I've moved this to the Servlets forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
i have done everything possible by me

the following are the changes i did

added the following jar in my LIB folder

commons-fileupload1.2.1.jar
commons-io1.4.jar


still when i send a request tp the jsp program

i get the JSP executed and get

YOUR FILES UPLOADED
with no errors or other trace
but my selected files are not uploaded.

i know its not a good design as you said earlier but my team wants this design . so have to do it??

anybody knows what else can i do to resolve this issue?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Have you read the appserver logs?
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
hi thank you for your previous posts.
rest of the problem have been solved.

Now am Trying to find how can you retrieve Text values in a jsp in a upload file program.

Because request.getParameter("name") always gives null.

How can i come over this problem??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

That greatly depends upon which upload package you are using. Read its documentation.
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
Am using apache uploading package
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42932
    
  68
If you have code inside of an if-block not executing, then the first guess would be that the if condition evaluates to false. Do you understand what "isFormField" checks?
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
yes. its basically a text field. but in my case isFormField always returns false and Document Name textField is never read.




Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42932
    
  68
The HTML of the documentname field is malformed; that's probably what causes the trouble.

A tool like the Firefox extension LiveHttpheaders can help troubleshoot problems like this one by showing what exactly gets transmitted in the HTTP request and the response.
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
Thnkx for the reply..

so should i change my front end style.. i mean HTML tags.. because as per my requirement more styling is not required..!!

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Who said anything about style? Your HTML is invalid.
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
Hi All

when i saved the above malfunctioned file as jsp and clicked submit i was not able to read the text field value

but when the same thing i saved as HTML and submitted values i was able to read the text value entered.

can anyone tell me why this happened??
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
this is my final 2 files
FIRST FILE


SECOND FILE


now my question when i saved the FIRST file here as jsp and clicked submit i was not able to get Document name from the second file. but when i saved the first file as HTML and clicked submit i got the DOCUMENT name from the second file.

So can any anybody expalin why the difference?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Again, JSP is a view technology. You should not write raw Java code in a JSP file. Java code belongs in Java classes. In your specific case, a Servlet is perfectly suitable for this.

Apart from your problem, the HTML <center> tag is deprecated since HTML 4.01 in 1998. You shouldn't be using it as it is subject to removal from the future HTML specifications. The same applies to the HTML <font> tag, if you wasn't aware about it. Both are pure styling tags and thus totally not semantic. You should be using CSS for styling.
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
Thanks for your reply.

But i was asked to design it using jsp only by my project manager so did it.

he does not like servlets i think. :roll:
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121


/**** can i retrieve these values without using Iterator?? *********/ i want the two files attached in separate variables
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
Like in LINE30 the variable filename is in loop and i have 2 files which are coming from a client
so i want to keep these files stored in 2 different variables,rather then in a loop so what can i do??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to upload using html or javascript?