J2EE Tutorial - Send Email From JSP & Servlet
A simple html form('dbdemo.htm') which invokes dbdemo.jsp follows:dbdemo.htm
<html>
<body>
<form method=post action=dbdemo.jsp>
type the selectquery here
<input type=text size=60 name='text1'> <br>
<input type=submit>
</form>
</body>
</html>
dbdemo.jsp
<html>
<body>
<%@page import="java.sql.*" %>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:telephone"; // an access database registered in odbc
Connection con = DriverManager.getConnection(url);
Statement stm = con.createStatement(); // container for sql
String sql = request.getParameter('text1'); // sql
ResultSet rs = stm.executeQuery ( sql);
while(rs.next())
{
out.println(rs.getString(1)+"<br>"); // name
out.println(rs.getString(2)+"<br>"); // telephone number
out.println("========");
}
%> 0
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------- 1
As for sending mail from webserver, using JavaMail API, the following code shows how the required data
such as 'from', 'to', 'subject' and 'message' are collected by the servlet and then processed for sending the mail.
------------------------------------------------------------------------------------------------------------------ 2
mailservlet.htm
<html>
<body> 3
<form method=post action="http://localhost:8080/servlet/mailservlet">
sender <input type=text name=text1><br>
Reciever <input type=text name=text2><br> 4
Subject <input type=text name=text3><br>
Message <textarea name='area1' rows=5 cols=30> </textarea>
<input type=submit> 5
</form>
</body>
</html> 6
mailservlet.java
import javax.servlet.*;
import javax.servlet.http.*; 7
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*; // important 8
import javax.mail.event.*; // important
import java.net.*;
import java.util.*; 9
public class servletmail extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response) 0
throws ServletException, IOException
{
PrintWriter out=response.getWriter(); 1
response.setContentType("text/html");
try
{ 2
Properties props=new Properties();
props.put("mail.smtp.host","localhost"); // 'localhost' for testing
Session session1 = Session.getDefaultInstance(props,null); 3
String s1 = request.getParameter("text1"); //sender (from)
String s2 = request.getParameter("text2");
String s3 = request.getParameter("text3"); 4
String s4 = request.getParameter("area1");
Message message =new MimeMessage(session1);
message.setFrom(new InternetAddress(s1)); 5
message.setRecipients
(Message.RecipientType.TO,InternetAddress.parse(s2,false));
message.setSubject(s3); 6
message.setText(s4);
Transport.send(message);
out.println("mail has been sent"); 7
}
catch(Exception ex)
{ 8
System.out.println("ERROR....."+ex);
}
} 9
}
Using javamail requires that we provide classpath to mail.jar & activation.jar. These should have been already installed in our machine. Otherwise, we will not be able to compile the servlet. For testing the servlet, we should have installed some mail server in our machine. For compiling the servlet, we have to set classpath to c:\jsdk2.0\src (java servlet development kit). 0
(We are using Tomcat server. The TOMCAT server is a webserver especially created for executing servlets and JSP . It is a joint effort by SunMicroSystems & Apache Foundation. Tomcat can be run on Apache server or independently. Tomcat will work in Windows platform also. The current version is Tomcat 4. Tomcat 5 is expected shortly).
We can move dbdemo.htm and dbdemo.jsp to :
c:\tomcat\webapps\root directory. 1
In another dos window,
c:\tomcat\bin>SET TOMCAT_HOME=C:\TOMCAT
>SET JAVA_HOME=C:\JDK1.3 2
>startup ( this will start the tomcat server in port 8080)
Start InternetExplorer and type the url as 'http://localhost:8080/dbdemo.htm. We will get the form. Type the sql and submit the form. We will get the resultset sent by the server. We need not compile the jsp file. It is automatically compiled at the server.
For running a servlet, copy the servlet's class file to: c:\tomcat\webapps\root\web-inf\classes directory. 3
For javamail , we have to copy mail.jar & activation.jar to classes folder.
copy the invoking html file to: c:\tomcat\webapps\root directory.
In the browser type the URL as 'http://localhost:8080/mailservlet.htm'. Enter the required data and submit the form to the server. The mail will be sent as specified by us. That is as brief a tour as possible of the first group of technologies in the J2EE basket. This is an essential foundation because , even when we use RMI/EJB , the correct practice is to invoke the RMI through a servlet. In the JSP example seen above, the code was exposed to the web-administarator. The better method is to encapsulate the code as a bean and invoke it. This way, only the class file will be seen by the server-administaror. The JSP page also will be easier to read and maintain.In the following example , we will adopt that method.