From the course: Jakarta EE Servlets

Developing web-based systems with Servlets - Jakarta EE Tutorial

From the course: Jakarta EE Servlets

Developing web-based systems with Servlets

- [Kevin] Servlets are the foundation of many web-based systems that are built with Java. Within this course, you'll learn how to create the servlets those systems are built upon. But before we dive into those details, let me explain the concept behind a web-based system, that way you'll understand exactly what types of systems you can build using a servlet. As you go about your day, you'll most likely access dozens of web-based systems using your web browser. In some cases, these systems can be really simple. Take this GitHub page, for example, that lists a few of my other LinkedIn learning courses. If we inspect the source code behind the page, we'll see that it just contains some static HTML, so no matter how many times you visit the page, nothing's going to change and the browser's going to display the same content. When you started to learn HTML, you might have created something similar to this and you probably just called it a website. That example is hosted through GitHub pages but it's also common to drop static content like HTML and CSS into a web server and host them from there. So, here's the point I want you to take away from this. Some simple systems, they just need to serve static content, that way it's accessible through the web. Their content will rarely change, and if it does, it's because a developer has manually changed the static content that's being served. Now, there's also this other class of web-based systems that have dynamic content which will constantly change as users interact with them. Most of our favorite websites like LinkedIn fall into this category. So for example, if I create a post on LinkedIn, this content automatically displays in my followers feeds and it's going to appear within the activity area of my profile. LinkedIn is built to dynamically display the content that a user creates. That way other members of the platform can consume and interact with it. We often refer to this type of system as a web application. Web applications are one of the most common architectures that organizations use to enable their employees or customers to perform different business capabilities that they provide. The types of web applications you encounter will vary quite a bit. You might use a web application provided by your financial institution to manage your bank account, or a cloud provider may provide a web application that you use to manage resources on the cloud. You'll find two main characteristics that apply to most web applications. First, their accessed via the web using a web browser. It's kind of implied by their name. And second, they provide their end users with the ability to complete tasks that involve the creation or consumption of some form of dynamic data. When web applications are built with Java, a servlet allows the developer to create this dynamic experience for an end user. Within a servlet, a developer can add logic that changes what a user views on each visit to a page within the application, or they can include logic that allows the user to perform some sort of transaction within the application, like when we created a post on LinkedIn. Now, there's also this other type of web-based system known as a web service. It's built to allow two systems to interact with each other. So within a web service architecture, there isn't a user involved. Instead, one system, which we'll call a client, sends requests over the web to another system, which is the web service. The client makes those web requests to access the data and other capabilities that are provided by the web service. Here again, the interactions between the client and the web service, they require the exchange of some form of dynamic data. So when developers build web services with Java they can rely upon servlets to handle these web requests from a client. Now that we've established the general concept of how a web-based system works and we've defined them, we can build upon that to better understand the purpose of a servlet. When we build a system in Java that's meant to be consumed over the web, servlets are the components that are used to receive and process web requests. We'll get into the details of a web request later but for now, you can just think of it as a call from a web browser or a web service client that requests that a Java application performs some sort of work for them. That work might be retrieving some data, executing a transaction, or returning the markup for a dynamic webpage. It can pretty much be anything, which is kind of exciting to think about. In any case, the web request gets routed to a servlet within the Java application. The servlet then executes the application logic we've coded to perform the work that was requested. So we use servlets to build web-based systems with Java that need to provide data or capabilities dynamically, like the web application and web service that we discussed. Servlets can serve static content, like the simple website I showed you, but if that's all your system needs to do a servlet it may not be the best solution.

Contents