#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    16
    Rep Power
    0

    Question 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.
    thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    NE Pa.
    Posts
    96
    Rep Power
    14
    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.
    DC Dalton
    DCD Designs
    SCJP
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    16
    Rep Power
    0
    word documents.
    what would be an example to do what you say?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    NE Pa.
    Posts
    96
    Rep Power
    14
    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.
    DC Dalton
    DCD Designs
    SCJP
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    16
    Rep Power
    0
    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?????
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    NE Pa.
    Posts
    96
    Rep Power
    14
    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..
    DC Dalton
    DCD Designs
    SCJP
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    16
    Rep Power
    0
    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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    NE Pa.
    Posts
    96
    Rep Power
    14
    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.
    java.util.Enumeration getParameterNames()
    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!
    DC Dalton
    DCD Designs
    SCJP
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    16
    Rep Power
    0
    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:

    org.apache.jasper.JasperException: javax/servlet/ServletRequest


    ?

    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.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    NE Pa.
    Posts
    96
    Rep Power
    14
    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!
    DC Dalton
    DCD Designs
    SCJP
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    16
    Rep Power
    0
    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

    message /tesis/UploadFile

    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
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    NE Pa.
    Posts
    96
    Rep Power
    14
    You need to add the servlet & packaging to the web.xml file like so:
    <web-app>
    <servlet>
    <servlet-name>Servlet_Name</servlet-name>
    <servlet-class>full.package.name.Servlet_Name</servlet-class>
    <load-on-startup>0</load-on-startup>
    </servlet>
    </web-app>

    This tells Tomcat where to find the class files for a given servlet, without it it's lost
    DC Dalton
    DCD Designs
    SCJP

IMN logo majestic logo threadwatch logo seochat tools logo