Results 1 to 8 of 8
  1. #1
    gb.rashu is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default How Retrive file from html to jsp

    Hello All,
    I am creating web page like sending resume to specified email id via Email. i need to retrive file parameter value from html into the jsp . i am not getting file path . can any one tell me the solution for this?

    thanks in advance
    Rashmi


    Here is the Html Code:
    <div><form method="post" name="postresume" action="Fresher_ack.jsp" onsubmit="return validation(this);">
    <table width="640" border="0" cellspacing="10" cellpadding="0">

    <tr>
    <td>&nbsp;</td>
    <td colspan="2" align="left" valign="top" class="jr-headdings">Submit Your Resume</td>
    </tr>
    <tr>
    <td colspan="3" height="0"></td>
    </tr>
    <tr>
    <td width="224" align="right" valign="middle" class="form-content">Name :</td>
    <td width="146" align="left" valign="top"><input type="text" name="firstname" /></td>
    <td width="230" height="15"></td>
    </tr>
    <tr>
    <td align="right" valign="middle" class="form-content">Contact Details :</td>
    <td align="left" valign="top"><input type="text" name="mobilenumber" maxlength="10"/></td>
    <td height="15"></td>
    </tr>
    <tr>
    <td align="right" valign="middle" class="form-content">E-mail :</td>
    <td align="left" valign="top"><input type="text" name="email" /></td>
    <td height="15"></td>
    </tr>
    <tr>
    <td align="right" valign="middle" class="form-content">Highest Qualification :</td>
    <td align="left" valign="top">
    <select name="combo0" id="combo_0" onChange="change(this);" style="width:150px;">
    <option value="value1">-Select-</option>
    <option value="value2">BE</option>
    <option value="value3">M.Tech</option>
    <option value="value4">ME</option>
    </select> </td>
    <td align="left"><select name="combo1" id="combo_1" onChange="change(this)" style="width:200px;">
    <option value="value1"> </option>
    </select></td>
    </tr>
    <tr>
    <td align="right" valign="middle" class="form-content">Upload Your Resume :</td>
    <td colspan="2" align="left" valign="top"><input type="file" id="file1" name="theFile" /></td>
    </tr>
    <tr>
    <td align="right" valign="middle" class="form-content">Aggregate :</td>
    <td colspan="2" align="left" valign="top"><input type="text" name="aggregate" maxlength="5" /></td>
    </tr>
    <tr>
    <td height="15"></td>
    <td align="left" valign="top"><input type="submit" name="Submit2" value="Submit" /></td>
    <td height="15"></td>
    </tr>
    </table>
    </form>
    </div>

    Here is the JSP code:Fresher_ack.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ page language="java" import="java.util.Date,java.util.Properties,javax. mail.Message,javax.mail.MessagingException,javax.m ail.Session" %>
    <%@ page language="java" import="javax.mail.Transport,javax.mail.internet.I nternetAddress,javax.mail.internet.MimeMessage,jav ax.mail.*,javax.mail.internet.*,javax.activation.* " %>
    <%
    // SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
    String to = "xyz@gmail.com";

    String from = request.getParameter("email");

    // SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
    String host = "192.168.0.68";
    // Create properties, get Session
    Properties props = new Properties();

    // If using static Transport.send(),
    // need to specify which host to send it to
    props.put("mail.smtp.host", host);
    // To see what is going on behind the scene
    props.put("mail.debug", "true");

    Session session1 = Session.getInstance(props);

    try {
    // Instantiatee a message
    Message msg = new MimeMessage(session1);
    //Set message attributes
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] address = {new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject("Test E-Mail through Java");
    msg.setSentDate(new Date());

    //file attachment
    MimeBodyPart mbp2 = new MimeBodyPart();
    String theFile1=request.getParameter("theFile");
    out.println(theFile1);
    FileDataSource fds = new FileDataSource(theFile1);
    mbp2.setDataHandler(new DataHandler(fds));
    mbp2.setFileName(fds.getName());
    // create the Multipart and add its parts to it
    Multipart mp = new MimeMultipart();
    // Set message content
    msg.setText("Hi Happy Diwali");
    mp.addBodyPart(mbp2);

    // add the Multipart to the message
    msg.setContent(mp);
    //Send the message
    Transport.send(msg);
    out.println("your mail was successfully sent");
    }
    catch (MessagingException mex) {
    // Prints all nested (chained) exceptions as well
    mex.printStackTrace();
    }
    %>

  2. #2
    go4soumya is offline Member
    Join Date
    Oct 2010
    Posts
    32
    Rep Power
    0

    Default your first step

    you should include: enctype="multipart/form-data" in the form tag like this.....

    <form method="post" name="postresume" action="Fresher_ack.jsp" onsubmit="return validation(this);" enctype="multipart/form-data">

    while uploading a file from html...

    and as for the path of the file, you won't get the client side file path from html for security reasons (it used to happen in the dinosaur era!! :p), but now u only get the file name with extension.

    So, file or filepath cannot be retrieved from html as -
    request.getParameter("thefile");

    You may like to use oreilly's api to easily upload file: here goes the link..
    Servlets.com | com.oreilly.servlet
    dnld the api and read the documentation.

    Do these things and report back, if itz working or not!!

    good luck...

    n 1 more thing..you didn't respond to your last thread whether u have solved your problem or not...better post or mark the thread as solved or people will lose interest solving your problems!

  3. #3
    gb.rashu is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    hi soumya,
    Thanks for the Quick reply

    Its not working:-(
    i downloaded and added .. but its not working..

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    Have you changed your form?
    "It's not working" is not terribly useful to us.

    What does your Servlet (not JSP!) look like?

  5. #5
    go4soumya is offline Member
    Join Date
    Oct 2010
    Posts
    32
    Rep Power
    0

    Default Give it another try

    Hi Rashmi,

    it shud work....

    download the api, include it in ur classpath...read the documentation and write your servlet properly..it shud work...it worked for me!! ohh..1 more thing....don't forget to include enctype="multipart/form-data" in your form tag!

    give it another try....and if u fail...post your servlet!

    Another thing, as Tolls said, try to avoid jsp and write servlets as controllers. Use jsp as your presentation logic in the front end. It's a better practice!

  6. #6
    gb.rashu is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    Hi Soumya,

    I will try once ..

    Thanks & Regards,

    Rashmi

  7. #7
    gb.rashu is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    Hi Soumya,
    It is woking in IE not in Mozilla. can you tell me the solution for this?


    Thanks & Regards
    Rashmi

  8. #8
    go4soumya is offline Member
    Join Date
    Oct 2010
    Posts
    32
    Rep Power
    0

    Default dats crazy!

    Hi Rashmi,

    Although browsers do trouble a lot of tym...but this tym, i don't think mozilla is the culprit!...it shud work in every browser irrespective of IE or Mozilla or chrome or anything!...Look into your servlet/jsp and try to figure out the problem. If you still can't...paste the code!...We'll be glad to help u out!

    Bdw...is your mozilla browser updated?...if not..do update it! (Altho version shudn't be a problem unless itz too old!)

    good luck!

Similar Threads

  1. cannot retrive data from mysql
    By tmukmkd in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 10-24-2010, 08:26 AM
  2. Retrieving the data posted to a JSP file from HTML file
    By marie in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 10-21-2010, 08:37 AM
  3. How to retrive a string array data from a session
    By shandi in forum New To Java
    Replies: 1
    Last Post: 09-16-2010, 02:02 PM
  4. Retrive from DB
    By Dileep in forum JDBC
    Replies: 12
    Last Post: 09-25-2009, 09:58 AM
  5. How can I include a html file in html textarea?
    By surya_dks in forum New To Java
    Replies: 2
    Last Post: 10-04-2008, 07:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •