CSC419 ยท 2080 ยท 5 marks
What do you mean by JSP implicit objects? Discuss Java Mail API.
What do you mean by JSP implicit objects? Discuss Java Mail API. [5]
Quick answer
JSP implicit objects are pre-defined objects that the JSP container automatically makes available to every JSP page without requiring the developer to explicitly declare or instantiate them. These objects can be used directly inside JSP scriptlets and expressions.
JSP Implicit Objects and Java Mail API
Part 1: JSP Implicit Objects
Note: Unlike Servlets, JSP has implicit objects while Servlet does not.
The commonly used JSP implicit objects are:
| Object | Type | Description |
|---|---|---|
request | HttpServletRequest | Represents the client HTTP request |
response | HttpServletResponse | Represents the HTTP response to the client |
out | JspWriter | Used to write output to the response |
session | HttpSession | Manages session data for a user |
application | ServletContext | Shared data across the entire application |
config | ServletConfig | Servlet configuration information |
pageContext | PageContext | Provides access to all JSP namespaces |
page | Object | Refers to the current JSP page instance (like this) |
exception | Throwable | Available in error pages to handle exceptions |
These objects simplify JSP development by removing the need for boilerplate code.
Part 2: Java Mail API
Definition
The JavaMail API is an API used to compose, write, and read electronic messages (emails). It provides a protocol-independent and platform-independent framework for sending and receiving emails.
Core Packages
java.mailjava.mail.activation
Setup Requirements
- For J2EE 1.3 (Enterprise Edition): No additional setup required.
- For J2SE 1.1+ (Standard Edition): Must download and install:
- JavaMail API
- Java Activation Framework
Sending an Email (3 Steps)
Step 1: Get the Session Object Stores all host information such as host name, username, password, etc.
Properties properties = new Properties();
Session session = Session.getDefaultInstance(properties, null);
Step 2: Compose the Message
MimeMessage class is mostly used for composing the email.
MimeMessage message = new MimeMessage(session);
Step 3: Send the Message
Transport class provides the method to send the message.
Transport.send(message);
Receiving an Email (Steps)
- Get the session object.
- Create the POP3 store object and connect with the POP server.
- Create the folder object and open it.
- Retrieve the messages from the folder into an array and print them.
- Close the store and folder objects.
Summary
| Feature | Detail |
|---|---|
| Purpose | Send and receive emails |
| Nature | Protocol-independent, Platform-independent |
| Key Classes | Session, MimeMessage, Transport |
| Protocol Used (Receive) | POP3 |
| Core Package | java.mail |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.