Search This Blog

Showing posts with label Email Sending in Servlet. Show all posts
Showing posts with label Email Sending in Servlet. Show all posts

Thursday, 9 January 2014

JSP Session

This JSP Tutorial shows you how to track the session between different JSP pages. In any web application user moves from one page to another and it becomes necessary to track the user data and objects throughout the application. JSP provide an implicit object "session", which can be use to save the data specific the particular to the user.
In this tutorial we will create an application that takes the user name from the user and then saves into the user session. We will display the saved data to the user in another page.
Here is the code of the JSP file (savenameform.jsp) that takes the input from user:
<%@ page language="java" %>
<html>
<head>
<title>Name Input Form</title>
</head>
<body>
<form method="post" action="savenametosession.jsp">
<p><b>Enter Your Name: </b><input type="text" name="username"><br>
<input type="submit" value="Submit">

</form>

</body>
 The above page prompts the user to enter his/her name. Once the user clicks on the submit button, savenametosession.jsp is called. The JSP savenametosession.jsp retrieves the user name from request attributes and saves into the user session using the functionsession.setAttribute("username",username);. Here is the code of savenametosession.jsp:
<%@ page language="java" %>
<%
String username=request.getParameter("username");
if(username==null) username="";

session.setAttribute("username",username);
%>

<html>
<head>
<title>Name Saved</title>
</head>
<body>
<p><a href="showsessionvalue.jsp">Next Page to view the session value</a><p>

</body>
 The above JSP saves the user name into the session object and displays a link to next pages (showsessionvalue.jsp). When user clicks on the "Next Page to view session value" link, the JSP pageshowsessionvalue.jsp displays the user name to the user. Here is the code of showsessionvalue.jsp:
<%@ page language="java" %>
<%
String username=(String) session.getAttribute("username");if(username==null) username="";
%>
<html>
<head>
<title>Show Saved Name</title>
</head>
<body>
<p>Welcome: <%=username%><p>

</body>
The function session.getAttribute("username") is used to retrieve the user name saved in the session.

Friday, 6 December 2013

Sending email through JavaMail API in Servlet

The JavaMail API provides many classes that can be used to send email from java. The javax.mail and javax.mail.internet packages contains all the classes required for sending and receiving emails.
For better understanding of this example click steps for sending email from javamail api
For sending the email using JavaMail API, you need to load the two jar files:
  • mail.jar
  • activation.jar
download these jar files or go to the Oracle site to download the latest version.

Example of Sending email through JavaMail API in Servlet

Here is the simple example of sending email from servlet. For this example we are creating 3 files:
  • index.html file for input
  • SendMail.java , a servlet file for handling the request and providing the response to the user. It uses the send method of Mailer class to send the email.
  • Mailer.java , a java class that contains send method to send the emails to the mentioned recipient.

index.html
  1. <form action="servlet/SendMail">  
  2. To:<input type="text" name="to"/><br/>  
  3. Subject:<input type="text" name="subject"><br/>  
  4. Text:<textarea rows="10" cols="70" name="msg"></textarea><br/>  
  5. <input type="submit" value="send"/>  
  6. </form>  

SendMail.java
  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3.   
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9.   
  10. public class SendMail extends HttpServlet {  
  11. public void doGet(HttpServletRequest request,  
  12.  HttpServletResponse response)  
  13.     throws ServletException, IOException {  
  14.   
  15.     response.setContentType("text/html");  
  16.     PrintWriter out = response.getWriter();  
  17.       
  18.     String to=request.getParameter("to");  
  19.     String subject=request.getParameter("subject");  
  20.     String msg=request.getParameter("msg");  
  21.           
  22.     Mailer.send(to, subject, msg);  
  23.     out.print("message has been sent successfully");  
  24.     out.close();  
  25.     }  
  26.   
  27. }  

Mailer.java
  1. import java.util.Properties;  
  2.   
  3. import javax.mail.*;  
  4. import javax.mail.internet.InternetAddress;  
  5. import javax.mail.internet.MimeMessage;  
  6.   
  7. public class Mailer {  
  8. public static void send(String to,String subject,String msg){  
  9.   
  10. final String user="sonoojaiswal@javatpoint.com";//change accordingly  
  11. final String pass="xxxxx";  
  12.   
  13. //1st step) Get the session object    
  14. Properties props = new Properties();  
  15. props.put("mail.smtp.host", "mail.javatpoint.com");//change accordingly  
  16. props.put("mail.smtp.auth", "true");  
  17.   
  18. Session session = Session.getDefaultInstance(props,  
  19.  new javax.mail.Authenticator() {  
  20.   protected PasswordAuthentication getPasswordAuthentication() {  
  21.    return new PasswordAuthentication(user,pass);  
  22.    }  
  23. });  
  24. //2nd step)compose message  
  25. try {  
  26.  MimeMessage message = new MimeMessage(session);  
  27.  message.setFrom(new InternetAddress(user));  
  28.  message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
  29.  message.setSubject(subject);  
  30.  message.setText(msg);  
  31.    
  32.  //3rd step)send message  
  33.  Transport.send(message);  
  34.   
  35.  System.out.println("Done");  
  36.   
  37.  } catch (MessagingException e) {  
  38.     throw new RuntimeException(e);  
  39.  }  
  40.       
  41. }  
  42. }