Hi all,

I need help with my servlets. I'm working on "email with attachment"
functionality. I use com.oreilly.servlet.MultipartRequest to upload the file
first in the server before attaching it to the mail. I use attach.jsp to allow
the user to keep adding attachment and message.jsp to send the mail thru
sendmail servlet. The first time I pass the session object holding the file
paths (in a vector) back to the calling jsp(attach.jsp) it works fine but the
second trip(like for example the user wants to attach another file, the session
object becomes null). I used both request objects(using RequestDispatcher) and
session objects(using Redirect) to pass the vector around but it doesn't seem to
help. Any help will be highly appreciated. Thanks.

Yuri

My code:
>>>message.jsp:

    HttpSession session1 = request.getSession();
     Vector toVtr = new Vector();
     toVtr.addElement("[EMAIL PROTECTED]");
     session1.setAttribute("toVtr", toVtr);
...................
   Vector filenames = (Vector)session1.getAttribute("filenames");
    if (filenames==null){
       System.out.println("Message:session filenames="+filenames);
       filenames = new Vector();
    }else System.out.println("Message:session filenames="+filenames);

   ...........................
<input type=button name='attach' value='Attach/Detach File..'
onclick="window.location='Attach.jsp'" >&nbsp;
<INPUT type=submit value='Send'>&nbsp;<INPUT type=reset value='Cancel'>

>>>>>>>>>>attach.jsp
<form name=attach id=attach action="<%= request.getContextPath() +
"/servlet/Attach" %>"
 ENCTYPE="MULTIPART/FORM-DATA" method=post>
<b>Select file to attach:</b>&nbsp;<input type=file name='filename' size=30
><br>
<%

    HttpSession session1 = request.getSession();
    Vector filenames = (Vector)session1.getAttribute("filenames");
    if (filenames==null){
       System.out.println("Attach:session filenames="+filenames);
       filenames = new Vector();
    } else System.out.println("Attach:session filenames="+filenames);
          ...................................................
<INPUT type=submit name='button' value='Attach'>&nbsp;
<INPUT type=submit name='button' value='Detach'>&nbsp;
<INPUT type=submit name='button' value='Done'>&nbsp;


>>>>>>>>>>>attach servlet

HttpSession session = request.getSession();
        String button = request.getParameter("button");

        Vector filenames = (Vector)session.getAttribute("filenames");
        if (filenames==null){
            System.out.println("Attach servlet:session filenames="+filenames);
            filenames = new Vector();
        }else System.out.println("Attach servlet:session filenames="+filenames);


        System.out.println("Button clicked="+button);
        if (button.equalsIgnoreCase("Attach")) {

               String path = "";

               //upload file to server
                MultipartRequest mr = new
MultipartRequest(request,"C:\\testmailattach\\");

                Enumeration params = mr.getParameterNames();
                while (params.hasMoreElements()) {
                      String name = (String)params.nextElement();
                      String value = mr.getParameter(name);
                      System.out.println(name + " = " + value);
                }
                System.out.println("Files:");
                Enumeration files = mr.getFileNames();
                while (files.hasMoreElements()) {
                    String name = (String)files.nextElement();
                    String filename = mr.getFilesystemName(name);
                    String type = mr.getContentType(name);
                    File f = mr.getFile(name);
                    System.out.println("name:"+name);
                    System.out.println("filename:"+filename);
                    System.out.println("type:"+type);
                    if (f!=null){
                        System.out.println("length:"+f.length());
                        path = f.getAbsolutePath();
                        System.out.println("path:"+f.getAbsolutePath());
                       /* boolean deleted = false;
                        deleted = f.delete();
                        if (deleted){ System.out.println("file deleted");}
                        else { System.out.println("file not deleted.pls remove
manually.");}*/

                     }
                 }

                 //check if selected file is already attached..
                 boolean attached = false;
                 if ((filenames!=null)&&(filenames.size()>0)){
                    for(int i=0;i<filenames.size();i++){
                        if (path.equals(filenames.elementAt(i))){
                            String afile = (String)filenames.elementAt(i);
                            System.out.println("vector file:"+afile);
                            System.out.println("file selected:"+path);
                            attached = true;
                            break;
                        }
                    }
                 }

                 if (attached==false){
                     filenames.addElement(path);
                 }

                 session.setAttribute("filenames",filenames);
                 response.sendRedirect("/NASApp/javamail/Attach.jsp");
       }else if (button.equalsIgnoreCase("Detach")) {
                 session.setAttribute("filenames",null);
                 response.sendRedirect("/NASApp/javamail/Attach.jsp");
       }else if (button.equalsIgnoreCase("Done")) {
                System.out.println("size="+filenames.size());
                session.setAttribute("filenames",filenames);
                response.sendRedirect("/NASApp/javamail/Message.jsp");

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to