Servlet Examples Part 2


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 will continue building our project using servlets. Let's get started. In the previous video, we have designed a JSP page to add percents. Now we will make operations with doPost. First, let's take value from form. We will take values with request getParameter() method. First, name. name equals request.getParameter. Now we have to set parameter name as person name. This value must be same with name value in input. If you want, let's show name on console. Let's try. Set a name as Bernard. Click button. Now, let's look at the console. And as you can see, person name is Bernard. Done. Now let's go on with the others. Surname. And age. For age, if you want, you can convert integer. Job. After that, you can save these values to your database. Perhaps we can use this project in the JPA section to add these items to the database. Now, let's try again. Let's look at the console. As you can see, all parameters are here. Now we have used doGet parameter. If you want, let's set attribute to the request. If you want, we can make empty controls and we can set error messages to the request. Let's check. I will make this only for name and surname. if name.trim(). The trim method in Java checks this unicode value before and after the string. If it exists, it then removes the spaces and returns the omitted string. .length() == 0. This means name area is empty and we have to give an error. request.setAttribute(error) is true. First, let's define an error's Boolean to check for errors. request.setAttribute(errors)  for first, we will set false. Also, I want to set error message. request.setAttributeName("error"). Now the message will be: Please fill the name area. Done. After operations, we have to check this error. If request.getAttributeErrors is true, this means we have an error, so we have to show an error message. Now go to the JSP file. As you know in JSP file, we can write Java codes also. In percent tags, <% %>, let's write our error message. First, let's define an error state. errorState = request.getAttribute("errors"). Now, we can check this. If errors. Now we can define our message. If request.getName("error") !== null. Now we can add our message. Show this in a div tag. Get name error message. Done. Let's try. First, fill name area. Look there is no problem. Now, let's try again with empty name area. And as you can see, we have a name error message. If you wish, you can change the color of this text using CSS codes. But I will not do so. If you want, let's add for the others also. Now first, I will check all of them together. Now I will set errors one by one. Done. Let's go to the JSP file. We have already added the code for name, so let's add it for surname, job, and age. Done. Let's try. Age is empty. Done. As you can see, your values are deleted but we don't want this. We can solve this problem with get parameters and ternary operator. The ternary operator is an if condition that you will use all conditions in one line. For example, let's use in name. In person name input, add value. Now, add percent tag. In this tag, we have to check person name parameter. If there is a name parameter, this means we have to set to the input. Person name equals null. Add a question mark. Now set person name, else set empty text. Done. Let's copy for the others. Done. Let's try again. As you can see, Name and Surname. Done. If you don't have any error and you want to redirect to the other page, you can use send redirect command of response. Let's redirect to the first servlet page. Let's try. As you can see, there is no problem. Let's take a break here. In the following video, we will continue with the servlet context and listeners example. See you in the following 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