February 24th, 2003, 05:05 PM
Attachments with Oreilly classes
can anyone please give me the code to get the attachment from a JSP and then how to store it into a mysql database?
i really dont know very much of this, so if you could send me a code example i would really be grateful.
February 26th, 2003, 06:24 PM
What kind of attachments are we talking about here? Images?...generally you NEVER store these type things in a database, you store a file name or reference link to them in the db, put the files in a folder.
February 28th, 2003, 04:48 PM
what would be an example to do what you say?
February 28th, 2003, 06:56 PM
In the action of your upload form I would have your jps call a worker servlet that uses the orielly package. Please also note that your form tag MUST include ENCTYPE='multipart/form-data' or no data will be uploaded! Believe it or not the upload process is only a few lines of code such as:
javax.servlet.ServletContext sc = getServletContext();
MultipartRequest mp = new MultipartRequest(req, sc.getRealPath("path_to_folder"), 50000000);
The first parameter to the constructor is a standard HttpRequest object. This uploads the file to the folder specified in the path_to_folder. The servlet context is required to correctly return the REAL path on the server to your folder & is absolutely critical to the process. The third parameter to the MulipartRequest object is the limit on the file size. If someone attempts to upload a file bigger than what you specify they will get an error page.
Once again I would NOT store these documents in any form of database, just the file name (which BTW, you can rename using the File object) then store the file name in a database. Storing the actual files in the db would require using a blob whihc is very weighty on any db. Once you filename is in the db you can create links to the documents using the path / filename combo..simple as 1,2,3....hope this helps!
Last edited by DC Dalton; February 28th, 2003 at 07:06 PM.
February 28th, 2003, 07:19 PM
it really does!!! but got a problem.
when using multipart/form-data, i cant get the parameters like this request.getParameters("name");
if i dont set multipart/form-data i can do it.
then, how can i get parameters from the request using multipart/form-data?????
February 28th, 2003, 07:44 PM
What parameters are you trying to get? The file names from the upload can be grabbed by getFileNames() Method of the MultipartRequest object, it returns an java.util.Enumeration
I really suggest your read the API that comes with the Oreilly package, there are tons of methods for handling most anything you can think of..
March 1st, 2003, 02:45 PM
Ok, I'll get the API.
I now there is a method getFileNames() to get the names
but what if want to get a simple <input type="hidden" name=">
or the select options?
cant i use request.getParameter("flag") ????
Do i have to put the <input type="file"> in a different form than the othe stuff?
Last edited by omarwehbe; March 1st, 2003 at 02:49 PM.
March 1st, 2003, 02:57 PM
There are overridden methods in the MultipartRequest object (look in the API to handle these esp:
java.lang.String getParameter(java.lang.String name) -
Returns the value of the named parameter as a String, or null if the parameter was not sent or was sent without a value.
Returns the names of all the parameters as an Enumeration of Strings.
java.lang.String getParameterValues(java.lang.String name)
Returns the values of the named parameter as a String array, or null if the parameter was not sent.
If you are trying to use the standard request object I believe you will get null because the request is being handled via a multipart request so you MUST use the overridden methods of this object NOT the javax.servlet.http.HttpServletRequest object....I think thats where you are loosing everything.
I have to ad that to become an effective Java programmer you really need to become intimate with the API for whatever package you are using....Read first then try, trust me, its the ONLY way you are going to learn!
March 1st, 2003, 03:09 PM
Does it really show I am just staring? Jejeje
I got the API and did what you said, but tell me sometyhing
in a JSP i am trying to do this:
<%MultipartRequest mr = new MultipartRequest (request, "/temp");%>
Is there any reason why I get this:
PS: Sorry to bother, but my english is not very good and mi Java isnt either
Last edited by omarwehbe; March 1st, 2003 at 03:15 PM.
March 1st, 2003, 03:49 PM
Hey we all have to start somewhere but the sooner you learn to try things before asking for help the faster you ill learn. You will be amazed that after a while you will look at a brand new (to you) API & say, oh cool..I can do that!
BTW, I didnt think your english was bad at all
Now next rule for any true Java programmer: USE GOOGLE!
I have thrown ten lines of exception print outs into Google & sure enough on the first page was my answer....you will be astounded.....GOOGLE RULES!
Not sure what the exception is coming from but I would go to, (again with the APIs) the Tomcat API to see if there is any help there... to be honest I avoid Tomcat like the plauge, absolutely despise it but when forced to I will work on it.....on that not RESIN RULES!
March 2nd, 2003, 11:34 AM
i know I should've used RESIN, but this project was started with TOMCAT.
One question, since i cant instantiate a MultipartRequest on a jsp God knows why, I am trying to do all that in a servlet, but another problem came around.
I get this error:
HTTP Status 404 - /tesis/UploadFile
type Status report
description The requested resource (/tesis/UploadFile) is not available.
I do have my UploadFile.java & UploadFile.class in /tesis/Upload,
Why doesn't Tomcat find my servlet? I think my classpath is ok too.
can you give me a hint on this please? I promise I'll hire you when I run my own business. jejeje
March 3rd, 2003, 10:27 PM
You need to add the servlet & packaging to the web.xml file like so:
This tells Tomcat where to find the class files for a given servlet, without it it's lost