Search This Blog

Showing posts with label JavaMail API. Show all posts
Showing posts with label JavaMail API. Show all posts

Thursday, 9 January 2014

Expression Language (EL) in JSP

Expression Language (EL) in JSP


There are many implicit objects, operators and reserve words in EL.The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean component, and other objects like request, session, application etc.
It is the newly added feature in JSP technology version 2.0.
EL helps you to save your programming time.

Syntax for Expression Language (EL)

  1. ${ expression }  

Implicit Objects in Expression Language (EL)

There are many implicit objects in the Expression Language. They are as follows:
Implicit ObjectsUsage
pageScopeit maps the given attribute name with the value set in the page scope
requestScopeit maps the given attribute name with the value set in the request scope
sessionScopeit maps the given attribute name with the value set in the session scope
applicationScopeit maps the given attribute name with the value set in the application scope
paramit maps the request parameter to the single value
paramValuesit maps the request parameter to an array of values
headerit maps the request header name to the single value
headerValuesit maps the request header name to an array of values
cookieit maps the given cookie name to the cookie value
initParamit maps the initialization parameter
pageContextit provides access to many objects request, session etc.

Simple example of Expression Language that prints the name of the user

In this example, we have created two files index.jsp and process.jsp. The index.jsp file gets input from the user and sends the request to the process.jsp which in turn prints the name of the user using EL.

index.jsp

  1. <form action="process.jsp">  
  2. Enter Name:<input type="text" name="name" /><br/><br/>  
  3. <input type="submit" value="go"/>  
  4. </form>  

process.jsp

  1. Welcome, ${ param.name }  

Example of Expression Language that prints the value set in the session scope

In this example, we printing the data stored in the session scope using EL. For this purpose, we have used sessionScope object.

index.jsp

  1. <h3>welcome to index page</h3>  
  2. <%  
  3. session.setAttribute("user","sonoo");  
  4. %>  
  5.   
  6. <a href="process.jsp">visit</a>  

process.jsp

  1. Value is ${ sessionScope.user }  

Example of Downloading file from the server using JSP

Example of Downloading file from the server using JSP

In this example, we are going to download the jsp file. But you may download any file. For downloading the file from the server, you should specify the content type named APPLICATION/OCTET-STREAM.

index.jsp

This file provides a link to download the jsp file.
  1. <a href="download.jsp">download the jsp file</a>  

download.jsp

In this example, we are downloading the file home.jsp which is located in the e: drive. You may change this location accordingly.
  1. <%    
  2.   String filename = "home.jsp";   
  3.   String filepath = "e:\\";   
  4.   response.setContentType("APPLICATION/OCTET-STREAM");   
  5.   response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");   
  6.   
  7.   java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath + filename);  
  8.             
  9.   int i;   
  10.   while ((i=fileInputStream.read()) != -1) {  
  11.     out.write(i);   
  12.   }   
  13.   fileInputStream.close();   
  14. %>   



Ref. Javatpoint.com

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. }