Servlet Context and Listener Example
Start course
1h 15m

In this course, we will learn the concepts of Java EE 7 with a focus on Servlet Technology.

Learning Objectives

  • Java Servlet Technology with Request, Filter and Listeners

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers


  • Have at least 2 years of Java development experience 

Hello, my friends. In this video, we'll create a project with Servlet Context and Listeners. Let's start. First, I want to hold person in a list and I want to show all persons in another page. First, let's define a new class as name, Employee. Let's define properties. Name, surname, job, and, age. Generate getters and setters. Now, turn back Servlet in do-post method. I want to hold values in a list. First, define an employee list. Now, we need an instance from employee. Now, set values to the employee. Now, add employee to the employee list. Now, if we try this we cannot see the result is true. Let's show results in console. Now, in do get method I want to add an empty employee list to the ServletContext. First, define ServletContext with getServletContext() and we will set list to context. Done, try. Add, add another one. Let's look at the console. As you can see, there are 2, 1 size. Why? Because we didn't use any listener. 

We need a listener to change ServletContext value. Let's add a listener. New listener, give a name as ContextListener. Next, now select lifecycle of contact servlet. Done, now, we have a listener. Let's adjust for ServletContext. First, get ServletContext value to a new context. Now, get list from context. If list equals null, set a new list to the context. Now, let's turn back Servlet. Now, in this page, we have to change this list creation. We already have a listener so we must create a list from context. Let's try again. Add two employees. Now, let's look at the result. As you can see, we have two employees in Servlet Context. Let's show employees in another page or let's show in first page. First ,we have to get list from Servlet, getServletContext(). Now, we will add a new append, employee names, their name, age, and job. Now, let's use a loop. Done, let's add another button to the JSP. We will redirect page to the first servlet. Let's try, add three employees, go to the other page. As you can see, we have three employees. Now, we have finished this video. In the next video, we will start using AJX. See you in the next video.

About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics