943,765 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 1201
  • JSP RSS
Aug 12th, 2008
0

Sending Email

Expand Post »
Hi I am trying to send the mail to client containing message as a data retrieved from database in table format. I am retrieving data in below servlet. now i want to send all this data in jsp page in table format. from this page i am sending all this data to a servlet where i wrote the code to send mail How can I send all this data in jsp page so that it is available in jsp in table format? if i am stiring all variable in session but how i can use them in value in jsp page
Here is an servlet page.

JSP Syntax (Toggle Plain Text)
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package com.student.igidr.test;
  7.  
  8. import java.io.*;
  9. import java.net.*;
  10. import java.sql.*;
  11. import javax.servlet.*;
  12. import javax.servlet.http.*;
  13.  
  14. /**
  15.  *
  16.  * @author user1
  17.  */
  18. public class tabledata extends HttpServlet{
  19. public void doPost(HttpServletRequest request, HttpServletResponse response) throws
  20. ServletException, IOException{
  21. HttpSession session = request.getSession(true);
  22. response.setContentType("text/html");
  23. PrintWriter pw = response.getWriter();
  24. int QNO;
  25. String noA,noB,noC,noD;
  26. String ID=request.getParameter("id");
  27. String EVENTID=request.getParameter("event");
  28. String connectionURL = "jdbc:mysql://localhost :3306/mynewdatabase";
  29. Statement st = null;
  30. Statement st1 = null;
  31. ResultSet rs=null;
  32. ResultSet rs1=null;
  33. Connection connection=null;
  34. try{
  35. Class.forName("com.mysql.jdbc.Driver");
  36. connection = DriverManager.getConnection(connectionURL, "root", "root123");
  37. st = connection.createStatement();
  38. rs = st.executeQuery("SELECT * FROM Questionbank where Questionid='"+ID+"'");
  39. while(rs.next()){
  40. String no=rs.getString("Qserialno");
  41. session.setAttribute("NO", no);
  42. String name=rs.getString("questionname");
  43. session.setAttribute("NAME", name);
  44. st1 = connection.createStatement();
  45. rs1 = st1.executeQuery("SELECT * FROM Questionbank where Questionid='"+ID+"'");
  46. try{
  47. rs1=st1.executeQuery("select count(*) as total from final where questionid='"+ID+"'and Eventid='"+EVENTID+"'and Qserialno='"+rs.getString("Qserialno")+"'and Answer='A'");
  48. rs1.next();
  49. noA=rs1.getString("total");
  50. session.setAttribute("NOA",noA);
  51. rs1=st1.executeQuery("select count(*) as total from final where questionid='"+ID+"'and Eventid='"+EVENTID+"'and Qserialno='"+rs.getString("Qserialno")+"'and Answer='B'");
  52. rs1.next();
  53. noB=rs1.getString("total");
  54. session.setAttribute("NOB",noB);
  55. rs1=st1.executeQuery("select count(*) as total from final where questionid='"+ID+"'and Eventid='"+EVENTID+"'and Qserialno='"+rs.getString("Qserialno")+"'and Answer='C'");
  56. rs1.next();
  57. noC=rs1.getString("total");
  58. session.setAttribute("NOC",noC);
  59. rs1=st1.executeQuery("select count(*) as total from final where questionid='"+ID+"'and Eventid='"+EVENTID+"'and Qserialno='"+rs.getString("Qserialno")+"'and Answer='D'");
  60. rs1.next();
  61.  
  62. noD=rs1.getString("total");
  63. session.setAttribute("NOD",noD);
  64. }
  65. catch (Exception e){
  66. pw.println(e);
  67. }
  68. finally
  69. {
  70. if (rs1 != null)
  71. {
  72. rs1.close();
  73. rs1 = null;
  74. } if (st1 != null)
  75. {
  76. st1.close();
  77. st1 = null;
  78.  
  79. }
  80.  
  81. }
  82. }
  83.  
  84. } catch (Exception e){
  85. pw.println(e);
  86. }
  87.  
  88.  
  89. }
  90. }

Now here is an jsp page.

<table align="center" width="50%" cellspacing="0" cellpadding="0" border="1" borderColor=#D2691E>
                  <form name="sendmail" action="/student/servletmail" method="POST">
                   <tr class="CellColor">
		<td>To</td>
		<td class="CellColor" width="1%">
			:
		</td>
		<td class="CellColor">
			<input type="text" name="to" size="25" value="">
		</td>
                <td>From</td>
		<td class="CellColor" width="1%">
			:
		</td>
		<td class="CellColor">
			<input type="text" name="from" size="25" value="">
		</td>
                
	</tr>
        <input type="hidden" name="message" value="??????">// what will be value here
        <tr class="CellColor">
		<td class="CellColor" colspan="9" align="center">
			<input type="submit" name="tn1" value="Send" >
			&nbsp;&nbsp;&nbsp;
			
		</td>
	</tr>    
                      
  </form></table>

And here is an servlet from I am trying to send the mail.

JSP Syntax (Toggle Plain Text)
  1.  
  2. package com.student.igidr.test;
  3.  
  4.  
  5. import java.io.*;
  6. import java.net.*;
  7. import java.util.*;
  8. import javax.mail.*;
  9. import javax.mail.event.*;
  10. import javax.mail.internet.*;
  11.  
  12. import javax.servlet.*;
  13. import javax.servlet.http.*;
  14.  
  15. /**
  16.  *
  17.  * @author user1
  18.  */
  19. public class servletmail extends HttpServlet
  20.  
  21.  
  22. {
  23.  
  24. public void doPost(HttpServletRequest request,HttpServletResponse response)
  25.  
  26. throws ServletException, IOException
  27.  
  28. {
  29.  
  30. PrintWriter out=response.getWriter();
  31.  
  32. response.setContentType("text/html");
  33.  
  34. try
  35.  
  36. {
  37.  
  38. Properties props=new Properties();
  39.  
  40. props.put("mail.smtp.host","webmail.igidr.ac.in"); // 'localhost' for testing
  41.  
  42. Session session1 = Session.getDefaultInstance(props,null);
  43.  
  44. String s1 = request.getParameter("to");
  45.  
  46. String s2 = request.getParameter("from");
  47.  
  48. //String s3 = request.getParameter("sub");
  49.  
  50. String s4 = request.getParameter("message");
  51. // out.println(s4);
  52.  
  53. Message message =new MimeMessage(session1);
  54.  
  55. message.setFrom(new InternetAddress(s2));
  56.  
  57. message.setRecipients
  58.  
  59. (Message.RecipientType.TO,InternetAddress.parse(s1,false));
  60.  
  61. // message.setSubject(s3);
  62.  
  63. message.setText(s4);
  64.  
  65. Transport.send(message);
  66.  
  67. out.println("mail has been sent");
  68.  
  69. }
  70.  
  71. catch(Exception ex)
  72.  
  73. {
  74.  
  75. System.out.println("ERROR....."+ex);
  76.  
  77. }
  78.  
  79. }
  80.  
  81. }

Problem is how can i use value of message field in jsp page as all the data retrieved from in table format only. this table contain analysis of perticular event and i want to send such tabular format data to the respective user.

Any suggestion is highly appreciated.
Thanks and Regards
Haresh
Last edited by guravharsha; Aug 12th, 2008 at 5:01 am.
Similar Threads
Reputation Points: 8
Solved Threads: 0
Light Poster
guravharsha is offline Offline
42 posts
since Jun 2008
Aug 15th, 2008
0

Re: Sending Email

Since the data is already stored in session, why get JSP's involved with the mailing process. If your site is authenticated, you already would have the user credentials when processing the request. The only task which remains is the formatting of the content. Also, when sending plain text mail, you don't have a lot of formatting options.

One option would be to tab delimit the data but then again it ends up screwing up with the layout since the width of each column would be unknown. A professional way of doing it would be to use a Reporting tool to create a template, hook a stored procedure to your template and control the report generation based on some input parameters. This report template would then be used to generated PDF reports which can be sent as attachments to the client. Another way would be to mail a link to your user which would direct him to a page which displays properly formatted data pulled from the database.

Of the above three, the third one is by far the easiest since it delegates the responsibility of data formatting to a markup language like HTML, something which it was meant for. But then again, if the requirements mandate that the table based data be passed as mail body, you are out of luck.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: how to call one bean method from other bean in JSP
Next Thread in JSP Forum Timeline: dependent dropdown menu problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC