File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Sending Email from jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Sending Email from jsp page" Watch "Sending Email from jsp page" New topic
Author

Sending Email from jsp page

Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi,
I am retrieving the data from database in jsp page in table format. This data is evaluation of particular event. I want to send this data to respective authority for evaluation.
Is it Possible to send this data into next jsp page message body part. In next jsp page I have sender, receiver email-id field and message field. Is it possible to obtain this data into that message field so I can send it to the respective user easily?

Here with my first page where I am retrieving the data.






Really it is possible .I have doubt on this.
Is there any alternative for above .
Any suggestion is highly appreciated
Thanks and Regards
Harshal

[ UD: added line breaks to preserve layout ]
[ August 07, 2008: Message edited by: Ulf Dittmer ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
For starters, you shouldn't be doing DB access in a JSP. That's seriously bad design. Use a backing bean or servlet instead.

If you have data you need in more than one JSP page you can store it in a user session (or in the web app context if you don't have -or don't want- sessions).


Ping & DNS - my free Android networking tools app
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
HI ulf,
I will definately follow your suggestion regarding to db connectivity.
can you gelp me on above topics.?
Thanks and Regards
Harshal
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi;


Harshal
[ August 08, 2008: Message edited by: Harshal Gurav ]
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi,
I know some of the ranchers are unhappy with my database connectivity pattern in jsp. Now I am converting it into session format in servlet so that I can user various parameter in different page ..
Kindly help me on below issue.
I want to send email of result of data in database to client. So I have jsp page containing form where field like To, From, Message body. Now here in message body I want fill up the data retrieved from database in tabular format.
For sending mail I am using JavaBeans for property to ,From ,Message like.
But problem is that How can I refer to the message body part in my Sendmail.jsp page with message variable in JavaBeans class. I don�t have the name ,type field in form .
I can present data in message body part in html format. So there is no input type tag.Here is an java Bean class.

Please help me.
Thanks and regards
Harshal
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
Are you familiar with request attributes? They can be used to pass objects from a servlet to the JSP page that renders the view. Session attributes could also be used if you have a session.
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi,
Can you more elaborate the above?
Thanks
Harshal
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi ulf,
To implement your idea ,whether i have to modify my javabean class.
How can relate property message in javabean class?
Please help me .

Thanks and Regards
harshal
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
whether i have to modify my javabean class.

To work as a bean, the class needs to have the appropriate getter methods. Then just add it as a request attribute in the servlet, and you'll be able to access its values in the JSP.

How can relate property message in javabean class?

I don't understand what you're asking here.
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi Ulf,
Can you give me the steps required to send the mail as message field in the mail will be above mentioned data?
I am unable to understand what are you trying to say.
Thanks and Regards
Harshal
[ August 10, 2008: Message edited by: Harshal Gurav ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
What are you having trouble with understanding? If it's the JavaBean concept, take a look at the Sun Java Tutorial; it has a chapter on that. If it's the concept of request attributes (or sessions), any decent servlet introduction (or book) will cover that, e.g. the online version of Core Servlets.
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi ,
After going to the various site as suggested by Ulf, I found that I am Really making dufficult my problem as i already displayed the data from database in my Eventtable.jsp. I know Ulf suggestion will works 100% but as i little bit new in servlet concept I thought that I will go with my earlier jsp page. Ulf ,i will definately go through the servlet part as you suggested to me.
My problem is deadline of my project is already over. So If i complete this task then it will couses 98% of my project work will over and i can take servlet part more effectively.
I really need help from the rancher here as I am troubling this issue last from four days,really.....

In below jsp page i had Retrieved data from table and presented it in table format. I am getting all the necessary data.In below jsp page I have form where i Have text form field for To,From and one hidden form field where i am using value=message variable from above jsp page.



And below is the servlet where i am gettting the value of form input field.
I able to send the mail to the user but message body contain data form above displayed table in inappropriate form.

I am getting ouput as hole data available in table with html tag like <td></tr><table> .I am expecting output available in table format which is i am displaying in above page.



I really required help from ranchers.It will help me lot of more.
Waiting for your precious suggestion.

Thanks and Regards
Harshal
[ August 12, 2008: Message edited by: Harshal Gurav ]
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi,
can anyone help me on Above topics?
I want hertly thanks to who help me .
Regards
Harshal
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
What is type="html" supposed to be? There's no such form element in HTML. Try using type="text" instead.
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi Ulf,
As said above, the mail are sending successfully but the message body get
data in not usefull form. The same page where i am displaying data in table form and same table i want to sent as message.

Please help me.
Thanks and regards
Harshal
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
So you fixed the problem I pointed out and it made no difference?
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi Ulf,
while modifying the input type like text/plain,text/html,hidden i had forgotten the deleting this name html. you are right as there is no input type as html. By modifieng it into text, my output is not vieving in the expected form i.e table not appear in the output.
Thanks and Regards
harshal
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
You didn't tell us what the contents of the text field are and what precisely you're seeing, so it's hard to advise on what to look at. But it seems that unless the page displaying the form is correct and working, then there's a good chance that any form submission from that page will be faulty.
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi Ulf,
What will be wrong with my code?Is there any alternative to send this mail.
Please help me.
Thanks and Regards
Harshal
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
What will be wrong with my code?

As I said, we can't know that, because you're not telling us what the contents of the field are, and what exactly is happening: TellTheDetails
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi Ulf,
I am really sorry as i unable to describe exact scenario.
Now i am trying to do this.
In my first jsp page,I am retrieving the data and presented it in same page in table format

<table border="1" cellpadding="2" cellspacing="3" bgcolor="#E6E6FA" align="center"><tr><th>Questions</th><th>A</th><th>B</th><th>C</th><th>D</th></tr>
....
.
.
<td><%=noA%></td><td><%=noB%></td><td><%=noC%></td><td><%=noD%></td></tr>

In above code I am displaying the data from database. the data is result of one event presented in tabular form.
Now i am using a variable message to store the above table in same jsp page.

<% message="<table border=1 cellpadding=2 cellspacing=3 bgcolor=#E6E6FA align=center><tr><th>Questions</th><th>A</th><th>B</th><th>C</th><th>D</th></tr>";%>
.
.
.
message=message+"<tr><td>"+rs.getString("Qserialno") + "." + rs.getString("questionname")+"</td>";
.
.
.
<% message=message+"<td>"+noA+"</td><td>"+noB+"</td><td>"+noC+"</td><td>"+noD+"</td></tr></table>";%>

I am not sure about storing the table in variable message.
And in below form I am using the variable message as value of messge hidden field.

<input type="hidden" name="message" value="<%=message%>">


now i am simply passing all this value to the sendmail servlet where it receives message as input parameter for message variabe


String s1 = request.getParameter("to"); String s2 = request.getParameter("from"); //String s3 = request.getParameter("sub"); String s4 = request.getParameter("message");


Is there anything i have to say so that you can understand more effectively.The mail is delievering successfully but not in the form as i presenting it in jsp page

Plese help me.
Thanks and Regards
Haresh
[ August 13, 2008: Message edited by: Harshal Gurav ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
You'll need to encode the <, > and " characters, lest you confuse the browser. You can't just store those in a hidden field.

Now I understand what you meant by:
I am getting ouput as hole data available in table with html tag like <td></tr><table> .I am expecting output available in table format which is i am displaying in above page.

But at the same time, it also doesn't make sense: you explicitly send all the data with the embedded HTML tags, but that isn't really what you want or expect? What did you expect?
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi Ulf,
Thank you very much for your post.
I am trying to send the table displayed on the jsp page as message body content of the mail.
Please suggest me how to send it?
I will be always thankfull to you .
Thanks and Regards
Harshal
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
As I said, you need to encode them if you want to include them in a field of an HTML form. Doesn't matter how you do that, but putting HTML tags straight into an HTML form field won't work. You should also remove any line breaks. Once the form has submitted the field to the server you need to reverse the encoding.
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi Ulf,
I really dont understand what are you trying to say.
Please.
Regards
Harshal
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi;
Is anyone there to help me on above issue?
Any help is highly appreciated.
Thanks and Regards
Harshal
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
Eric said essentially the same thing I did in his post. Did that make sense to you?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending Email from jsp page