Java servlet and web containers. Building Java Applications with Servlets


Java Servlet is a server-side program written in the programming language of the same name, which receives signals from the client and sends responses back to it. It is the key element that forms the typical Java EE, in addition to JSP, EJB, XML and other related technologies. The application can be packaged into a WAR (Web AR chive) file for deployment on a web server. A server that can run a Java servlet is called a container. A program that runs on such a server can create dynamic web pages.

Java Servlet: Basics

The most popular and widely used containers are Tomcat and JBoss. Technically, a servlet is a normal Java class that has an extension for the Common Client-Server Protocol or HTTP. In practice, it is used to process requests through the HttpServlet GET and POST overrides, respectively. The Java Servlet container provides Http.ServletRequest and Http.ServletResponse, which are request-response objects. And is usually used in combination with JSP to generate dynamic content.

Typical model scenario:

  • The JSP presents a form for the user to enter data.
  • The servlet receives input, processes it, and sends a response.
  • For high-quality work, it uses special filters.
  • Java Servlet Filters are Java plug-in components that are used to intercept and process requests before they are sent to servlets and respond after its code has completed, and before the container sends the response to the client.

    Common tasks performed with filters:

  • Registering request parameters for registering files.
  • Authentication and authorization of resource request.
  • Formatting the request body or header before sending it to the servlet.
  • Compresses response data sent to the client.
  • Changing the answer, adding some cookies.
  • Java Servlet header information.
  • Filters are enabled and configured in a deployment descriptor file (web.xml). Servlets and filters don't know about each other, so you can add or remove a filter simply by editing the web.xml. It is possible to have multiple filters for one resource or create a chain of filters for web.xml or run Java Servlet filters implementing the javax.servlet.Filter interface.

    Parallel requests to the server are processed by threads, which provides important qualities of the web - multithreading and parallelism.

    Main functions:

  • Portability. Since Java is platform independent, the same is true for servlets. For example, you can create it on Windows operating system so that GlassFish developers can use it as a web server and then can run it on any other OS like Unix, Linux with apache Java Servlet web server. This feature makes it portable, and this is its main advantage over CGI.
  • Efficiency and scalability. Once the Servlet is deployed and loaded onto the web server, it can instantly begin executing client requests. It is called by a lightweight thread so multiple client requests can be filled at the same time using Java's multithreading feature. This is in contrast to CGI, where the server initiates a new process for each client request.
  • Reliability. By inheriting top Java features such as garbage collection, exception handling, Java security manager and others, it is less prone to management issues and memory leaks. This makes developing an application in it safe and error-free.
  • The need to use dynamic web pages

    There are many reasons why a business would want to create dynamic web pages on the fly, such as when the data on the website changes frequently. News and weather sites typically rely on CGI to keep content fresh without requiring constant developer attention. E-commerce web pages that list current prices and inventory levels use CGI to fetch this content on demand, pulling it from the company's internal infrastructure.

    Many users have experience using Java technology to create CGI-based web services, but Java Servlets are more efficient, more powerful, easier to use, and cheaper than traditional CGI alternatives.

    Advantages of Java Servlets:

  • Efficiency. In traditional CGI, each HTTP request starts a new CGI process. Even if its code is perfectly implemented, there is often a significant amount of overhead not only when the process starts, but also during its execution. When servlets are used, the JVM remains loaded in memory and each request is processed by a Java thread. As an example of a Java Servlet, if in a traditional CGI model there are X number of concurrent requests, this means that the code for the program is loaded into memory X number of times. This becomes an excessive load on the web server. However, in a servlet environment there are X threads where only one copy of its class is run. This results in increased efficiency and scalability across multiple platforms.
  • Convenience. When using the program, there is no point in learning a new language, such as Perl, just to perform CGI functions. Additionally, servlets have a rich infrastructure for many HTML-related tasks, which greatly speeds up the development process.
  • Power - Unfortunately, traditional CGI scripts leave much to be desired. For example, their regular programs cannot talk directly to web servers, which means that the entire interface must be created. Servlets can communicate directly with web servers, simplifying operations that require direct access to data stores. They are also unique because they can communicate with other servlets and maintain information between requests, making session tracking extremely simple.
  • Java's portability extends directly to servlets. In fact, almost every major web server in use today supports Java Servlets either directly or through a plugin.
  • Economy. From a development perspective, implementing servlets is much cheaper than other options that require custom coding to communicate with web servers correctly. Java Servlet redirect is ready to go and can minimize business cost without sacrificing the benefits of dynamic content.
  • One of the best things about Java is its versatile nature. Of course, creating traditional desktop and even mobile applications is great. But what if you want to go off the beaten path and enter the territory of web application development in Java? The good news for you is that the language comes with a full-fledged Servlet API, which allows you to create reliable web applications without much hassle.

    Building Java Applications with Servlets

    So, we have already created the application configuration files. However, in its current state it literally does nothing. We want clients to be able to register using an HTML form, so the next thing we need to do is to create JSP files that will display the above form and client details once the registration is completed successfully. This is what we will do now.

    We are working on the appearance

    The appearance of the application will be defined by two JSP files - in the MVC context these are called views. The first one will be responsible for displaying the registration form and possible errors caused after checking the entered data. The second will be a regular welcome page that will show the details entered by the customer once the registration process has been successfully completed.

    Here is the first JSP file:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> Registration

    Registration

    $(violation).





    

    2024 gtavrl.ru.