Listener

---Restore content begins---

1. Listener Listener

javaEE includes 13 specifications, and mainly learns servlet technology and jsp technology in the course

The servlet specification includes three technical points: servlet listener filter

1. What is a listener?

A listener is a component that listens for changes in the state of an object

Related concepts of listeners:

Event source: monitored object ----- three domain objects request session servletContext

Listener: Listen to the event source object Changes in the state of the event source object will trigger the listener---- 6+2

Registering listeners: Binding listeners to event sources

Response behavior: the function code involved when the listener listens to the state change of the event source---- the programmer writes the code

 

2. What are the listeners?

The first dimension: according to the monitored objects: ServletRequest domain HttpSession domain ServletContext domain

The second dimension: the content of monitoring: monitoring the creation and destruction of domain objects Monitoring the property changes of domain objects

 


3. Listeners that monitor the creation and destruction of three domain objects

(1) The listener ServletContextListener that monitors the creation and destruction of the ServletContext domain

1) The life cycle of the Servlet domain

When to create: The server initiates the creation

When to Destroy: Server Shutdown Destroyed

 

2) The writing steps of the listener (emphasis):

a. Write a listener class to implement the listener interface

b. The method of overriding the listener

c. It needs to be configured in web.xml---registration

 


3) The method of monitoring:

 

 

 

 

4) Configuration file:

 

5) The main role of the ServletContextListener listener

a. Initialization work: initialization object initialization data ---- load database driver connection pool initialization

b. Load some initialized configuration files --- spring configuration files

c. Task scheduling----timer----Timer/TimerTask

 

Task scheduling:

 

(2) The listener HttpSessionListener created in the destruction of the Httpsession domain

1) The life cycle of the HttpSession object

When created: Created when request.getSession is called for the first time

When to destroy: the server shuts down to destroy the session expires and manually destroys

2)

Methods of HttpSessionListener

 

(3) The listener ServletRequestListener that monitors the creation and destruction of the ServletRequest domain

1) Life cycle of ServletRequest

Create: request will be created for every request

Destroy: end of request

2) Method of ServletRequestListener

 


 

4. Monitor the property changes of the three domain objects

(1) Common methods of domain objects:

setAttribute(name,value)

 --- The method that triggers the listener that adds the property  

 --- The method that triggers the listener that modifies the property

getAttribute(name)

removeAttribute(name) 

--- The method that triggers the listener to delete the property

(2) ServletContextAttibuteListener listener

 


(3) HttpSessionAttributeListener listener (same as above)

(4) ServletRequestAriibuteListenr listener (same as above)

 

5. Listeners associated with bound objects in the session ( object -aware listeners)

(1) The object to be bound to the session has several states

Binding state: an object is placed in the session field

Unbound state: This object is removed from the session domain

Passivation state: Persist (serialize) objects in session memory to disk

Activation state: is to restore the object on the disk to the session memory again

 

Interview question: How to optimize the server when there are many users?

Temporarily persist inactive users to the hard disk, and restore them to memory when using

(2) Binding and unbinding listener HttpSessionBindingListener

 


 

(3) Passivation and activation listener HttpSessionActivationListener

Object passivation time can be specified through the configuration file --- how long the object should not be passivated

Note that the object must implement the serialization interface

Create a context.xml under META-INF

 

<Context>

 <!-- maxIdleSwap: How long does the object in the session passivate if it is not used-->

 <!-- directory: In which directory the passivation object file is written to the disk Configure the passivation object file in work/catalina/localhost/passivation file -->

 <Manager className="org.apache.catalina.session.PersistentManager"                                                                                                                           maxIdleSwap="1">

  <Store className="org.apache.catalina.session.FileStore" directory="itcast205" />

 </Manager>

</Context>

 

 

Files passivated to work/catalina/localhost/

2. Mailbox server

1. Basic Concepts of Mailbox Servers

Email client: it can be installed only on the computer or it can be in the form of a web page

Mail server: plays the role of receiving and pushing mail

Email sending protocol:

Protocol: It is the constraint of data transmission

Protocol for accepting mail: POP3 IMAP

Protocol for sending mail: SMTP

 

 

2. Email sending process

 


3. Mailbox server installation

1) Double-click the mailbox server software

2) Configure the mailbox server

 

 

 

 

 

 

4. Email client installation

 

 


5. mail sending code

 

package com.itheima.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtils {

	//email: to whom the mail is sent subject: subject emailMsg: the content of the mail
	public static void sendMail(String email, String subject, String emailMsg)
			throws AddressException, MessagingException {
		
		// 1. Create a program and the mail server session object Session
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "SMTP");//Protocol for sending emails
		props.setProperty("mail.host", "localhost");//Server address for sending mail
		props.setProperty("mail.smtp.auth", "true");// Specifies that the verification is true

		// create validator
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("tom", "123");//Authentication of the account that sent the email
			}
		};

		Session session = Session.getInstance(props, auth);

		// 2. Create a Message, which is equivalent to the content of the message
		Message message = new MimeMessage(session);

		message.setFrom(new InternetAddress("[email protected]")); // set sender

		message.setRecipient(RecipientType.TO, new InternetAddress(email)); // set the sending method and recipient

		message.setSubject(subject);//Subject of the email

		message.setContent(emailMsg, "text/html;charset=utf-8");

		// 3. Create a Transport for sending mail
		Transport.send(message);
	}
}

  


 

---End of recovery content---

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325075862&siteId=291194637