Java web application types
A web application is a dynamic extension of a web or application server. Web applications are of the following types:
- Presentation-oriented: A presentation-oriented web application generates interactive web pages containing various types of markup language (HTML, XHTML, XML, and so on) and dynamic content in response to requests. Development of presentation-oriented web applications is covered in Chapter 7, «JavaServer Faces Technology,» through Chapter 18, «Java Servlet Technology.»
- Service-oriented: A service-oriented web application implements the endpoint of a web service. Presentation-oriented applications are often clients of service-oriented web applications. Development of service-oriented web applications is covered in Chapter 31, «Building Web Services with JAX-WS,» and Chapter 32, «Building RESTful Web Services with JAX-RS,» in Part III, «Web Services.»
In the Java EE platform, web components provide the dynamic extension capabilities for a web server. Web components can be Java servlets, web pages implemented with JavaServer Faces technology, web service endpoints, or JSP pages. Figure 6-1 illustrates the interaction between a web client and a web application that uses a servlet. The client sends an HTTP request to the web server. A web server that implements Java Servlet and JavaServer Pages technology converts the request into an HTTPServletRequest object. This object is delivered to a web component, which can interact with JavaBeans components or a database to generate dynamic content. The web component can then generate an HTTPServletResponse or can pass the request to another web component. A web component eventually generates a HTTPServletResponse object. The web server converts this object to an HTTP response and returns it to the client.
Servlets are Java programming language classes that dynamically process requests and construct responses. Java technologies, such as JavaServer Faces and Facelets, are used for building interactive web applications. (Frameworks can also be used for this purpose.) Although servlets and JavaServer Faces and Facelets pages can be used to accomplish similar things, each has its own strengths. Servlets are best suited for service-oriented applications (web service endpoints can be implemented as servlets) and the control functions of a presentation-oriented application, such as dispatching requests and handling nontextual data. JavaServer Faces and Facelets pages are more appropriate for generating text-based markup, such as XHTML, and are generally used for presentation-oriented applications.
Web components are supported by the services of a runtime platform called a web container. A web container provides such services as request dispatching, security, concurrency, and lifecycle management. A web container also gives web components access to such APIs as naming, transactions, and email.
Certain aspects of web application behavior can be configured when the application is installed, or deployed, to the web container. The configuration information can be specified using Java EE annotations or can be maintained in a text file in XML format called a web application deployment descriptor (DD). A web application DD must conform to the schema described in the Java Servlet specification.
This chapter gives a brief overview of the activities involved in developing web applications. First, it summarizes the web application lifecycle and explains how to package and deploy very simple web applications on GlassFish Server. The chapter then moves on to configuring web applications and discusses how to specify the most commonly used configuration parameters.
Spring Boot WebApplicationType
Spring Boot WebApplicationType tutorial presents various types of web applications in a Spring Boot application. The example shows how to set the WebApplicationType.
is a popular application framework for creating enterprise application in Java, Kotlin, or Groovy.
WebApplicationType
- NONE — the application should not run as a web application and should not start an embedded web server.
- REACTIVE — the application should run as a reactive web application and should start an embedded reactive web server.
- SERVLET — the application should run as a servlet-based web application and should start an embedded servlet web server.
Spring Boot example
In the following application, we define the web application type of a Spring Boot application.
build.gradle . src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ └── Application.java │ └── resources └── test └── java
This is the project structure of the Spring Boot application.
plugins < id 'java' id 'org.springframework.boot' version '3.1.1' id 'io.spring.dependency-management' version '1.1.0' >group = ‘com.zetcode’ version = ‘0.0.1-SNAPSHOT’ java < sourceCompatibility = '17' >repositories < mavenCentral() >dependencies < implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-starter-web' >test
In the build.gradle file, we have dependencies for a classic servlet and reactive web application.
package com.zetcode; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.ServerResponse; import static org.springframework.web.reactive.function.BodyInserters.fromValue; import static org.springframework.web.reactive.function.server.RequestPredicates.GET; import static org.springframework.web.reactive.function.server.RouterFunctions.route; import static org.springframework.web.reactive.function.server.ServerResponse.ok; @SpringBootApplication public class Application < public static void main(String[] args) < new SpringApplicationBuilder(Application.class) .web(WebApplicationType.SERVLET) .run(args); >> @RestController class MyController < @GetMapping("/") public String hello() < return "Home page"; >> @Configuration class MyRoutes < @Bean RouterFunctionabout() < return route(GET("/about"), request ->ok().body(fromValue("About page"))); > > @Component class MyRunner implements CommandLineRunner < @Override public void run(String. args) throws Exception < System.out.println("Hello there!"); >>
In the Application , we define the Spring Boot application and set up a classic web rest point, a reactive route and a commandline runner.
new SpringApplicationBuilder(Application.class) .web(WebApplicationType.SERVLET) .run(args);
We define the web application type using the SpringApplicationBuilder . For the WebApplicationType.SERVLET , the reactive route is not available.
$ curl localhost:8080/ Home page
The classic servlet rest point is active.
In this article we have worked with a Spring Boot WebApplicationType .
Author
My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.