Servlet Examples Part 1


Java Servlet Technology
Servlet Request
10m 27s

The course is part of this learning path

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, dear friends. In this video, we'll demonstrate our knowledge of Servlets by developing a project. First, I'd like to show these versions. First, Eclipse. Look, my Eclipse version is here. And look, my server version. And this one is my JDK version. Now, let's go on. First, create a new project. 'New', 'Dynamic Web Project'. Select 'Server'. And add 'web.xml'. Done. Now, we need a package. The name will be servlet pages. Now, let's add our first servlet page. You could also add the page using the 'New', 'Servlet' shortcut. You could also add with 'New', 'Other'. After search Servlet, you can add 'Servlet'. Give a name. This is our URL to access this page. These are methods that we have learned for now. 'doGet' and 'doPost' are enough for us. Now, this is our first servlet page. Look, we have two methods and a super constructor. Now, let's start to design our first page. In any web page, first we have to define our content type. We will select HTML, response.setContentType("text/html"). Now, let's define our print writer. Now, we will append our html codes. First, we need HTML tags, append html. Now, let's add head tags. We need body tags also. Now, we can add any item to our page. If you want, let's add an H1 tag. Welcome To First Servlet Page. That's enough. Let's try. Run. Now, we have our web page that displays our welcome message. And look, the URL is first. Now, let's change the servlet. Let's run again. Look, the URL has changed. You don't need to add any text to the web servlet annotation. If you like, you could set this page as your home page. You can leave it blank. And look, we can access this page without any text. As you can see, this method is difficult to add appends for each tag. So, for servlet pages, using JSP files will be easier. Let's add a JSP file. First, I want to add a new servlet. Name will be AddPerson. Done. Let's add JSP file in web content. Right-click 'New JSP file'. Give it the name, AddPerson. If you want, you could give it another name. We will match the servlet and the JSP file. So, the name is not important. Done. As you can see, we already have some HTML codes. Now, let's add an H1 tag and add some text. Now, let's match servlet and JSP file. Open servlet in Get method. It is very easy. We will use RequestDispatcher of response. RequestDispatcher equals. Now, we can use response.getDispatcher. In parameter area, we will set our JSP file. AddPerson.jsp. Now, to show pages, we have to forward our view, view.forward(request, response). Now, we can try. Run. And look, now we can see our AddPerson.jsp page. Let's go on to design an AddPerson page. First, we will add persons, so we need a form tag. Actually, you can use any template or bootstrap links for a better appearance. But my goal is to teach you how to use servlets. Therefore, I won't use any other technologies. Action Add post. This means, when we post this method, the doPost() method will be executed. Now, in form, we will need some elements like name, surname, job, and maybe age. Let's add them. A label for name. Now, we need an input for name. Name will be person name. Let's add br tags. And divs. And let's look at the result. As you can see now, we have a label and an input for person name. As previously stated, you can use any template for a better appearance, such as AdminLTE, Medtronic, or Bootstrap. But I won't. Let's go on. Let's copy label and input for surname. Change values. Copy for job. Last one is age. Now, we need a button with a value save. Type will be submit. Done. Let's look at the page. Run. As you can see now, we have a form page. If you want, let's copy all these HTML tags. Now, open first servlet. Now, paste. Now, let's try again. And as you can see, there is no difference between JSP and this. But it is more difficult, so we'll keep working with JSP. Let's take a break here. In the following video, we'll examine servlet in Eclipse. 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