Web application structure by Java and processing flow in the presentation layer

Introduction

In the training for new employees, I developed a web application using Java, so I would like to briefly summarize what I learned there. I think there are many mistakes, so I would be grateful if you could point out.

We will focus on the processing flow on the server side of the Web application. As an assumed sample, it will be a simple one-item search sample.

Interaction between servers

A brief description of the dynamic exchange of information between the client and server when implementing a dynamic web page.

First, the client sends a request to the web server. Transmission and reception between this client and the Web server is performed by the HTTP protocol. If you want to display a dynamic web page, you need to do some processing (program) on the web server side. The process is a web application, which will be implemented in a server-side language such as Java or PHP. This time, it is assumed to be implemented in Java.

In order to run the application, you need to prepare an application server separately from the web server. (Tomcat is famous for Java application servers.) An application server, like sending and receiving between a client and a web server, requires a protocol to send and receive to and from a web application. If you use Tomcat for the application server and Apache for the Web server as described above, implement a cooperation module called mod_jk in Apache as an extension. The application server and the web server can send and receive using the protocol called ajp13 of mod_jk. Since Tomcat has a simple web server, it can send and receive without any special cooperation.

Web application structure

Now let's see how the web application is processed on that server side.

Web applications are designed with the MVC architecture as a model, and are roughly divided into the following three layers.

・ Presentation layer ・ Business ・ Integration layer

The roles of the programs contained in each layer are clearly separated.

Next, I will describe how the processing is done based on the request information from the client.

Application processing flow on the server side

スクリーンショット 2020-06-04 20.05.12.png

The request information is first passed to the Front Controller. This Front Controller is a Servlet that centrally manages the processing of Web applications.

The Front Controller distributes the process according to the request information. (Switch-case statement is valid) Action is called in it, and that Action checks the input value, manages the session (creates and releases session objects), and requests processing to the business logic of the business layer. (As an aside, in session management, in order to make effective use of server resources, it is better to release the session as soon as possible, such as disabling the session if access is interrupted for a certain period of time.)

And the core business Logic controls business processing. Information required for business processing is acquired by accessing the database. Use Connection Manager to access the database. Specifically, use the getConnection method of ConnectionManager to get the Connection object (connection to the database).

Create a DAO object by passing that Connection object as an argument to the DAO's constructor. Then, it calls the DAO method and receives the return value using Entity.

How to set the return value in DAO (in the case of 1 search) If there is a search result with while (res.next ()), the processing inside {} is performed. In {}, set the return value using res.get ~

After Business Logic receives the Entity object, it is stored in the defined object and set as the return value.

Create a business Logic in Action and call a method. Set the result to the scope etc. Set the transition page destination as the return value.

In FrontController, it receives the transition page destination from Action and forwards it.

Application behavior in the presentation layer

Servlets and JSPs

A program called a Servlet is required to generate an HTML file that displays a web page. The Servlet is run by a web application server such as Tomcat.

It is possible to display a web page with this Servlet alone, but since the program description becomes complicated and it will not be a beautiful program, request the JSP that embedded the Java code in HTML to display the web page. Will be.

In addition, in order to transition the Web page, it is necessary to add parameters to the request sent by the client. There are two ways to send the request: GET and POST.

GET and POST

Both the GET and POST methods require you to set any value as a parameter.

In the GET operation, any value set in the parameter is sent to the web server in the form of being embedded in the URL. Therefore, if you want to browse the web page again, you can easily access it. In other words, bookmarks are saved for each parameter. Disadvantages include the limitation of parameter length and low security as mentioned above.

On the other hand, in POST operation, any value set in the parameter is not included in the URL and is sent to the web server in the form stored in the message body of the request. Therefore, it is more secure than the GET operation. In addition, there is no limit to the length of the parameters, and a large amount of data can be sent.

In the processing in the application, it is described in the user-defined Servlet in the form of overriding the doGet method and doPost method defined in HttpServlet. This allows you to use the HTTP features.

Summary

This time, I briefly explained the behavior of Web applications on the server side and the behavior of applications on the presentation layer. It will be longer than this, so I would like to explain the business layer and the integration layer in a separate article.

Recommended Posts

Web application structure by Java and processing flow in the presentation layer
[Java] How to get the key and value stored in Map by iterative processing
Role of JSP in Web application [Java]
The story that the Servlet could not be loaded in the Java Web application
Regarding the transient modifier and serialization in Java
I received the data of the journey (diary application) in Java and visualized it # 001
Parallel and parallel processing in various languages (Java edition)
[Java] Various methods to acquire the value stored in List by iterative processing
[Java] Judgment by entering characters in the terminal
[Java / PostgreSQL] Connect the WEB application to the database
Parse the date and time string formatted by the C asctime function in Java
[Android / Java] Screen transition and return processing in fragments
Write ABNF in Java and pass the email address
Read the packet capture obtained by tcpdump in Java
Image processing: The basic structure of the image read by the program
Asynchronous processing and Web API integration in Android Studio
Java classes and instances to understand in the figure
Roughly the flow of web application development with Rails.
Create a JAVA WEB application and try OMC APM
What is the difference between the responsibilities of the domain layer and the application layer in the onion architecture [DDD]
Branch processing by the return value of RestTemplate and the status code of ResponseEntity in Spring Boot
Let's make a calculator application in Java ~ Display the application window
JSON in Java and Jackson Part 1 Return JSON from the server
Web application development environment construction in Java (for inexperienced people)
Understand the Singleton pattern by comparing Java and JavaScript code
Correct the character code in Java and read from the URL
Think about the differences between functions and methods (in Java)
Understand the Iterator pattern by comparing Java and JavaScript code
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
Measured parallel processing in Java
Rails web server and application server
Comparison of WEB application development with Rails and Java Servlet + JSP
Search and execute method by name from instance with processing (java)
What I have learned in Java (Part 1) Java development flow and overview
[Java] Get the dates of the past Monday and Sunday in order
Get the current date and time by specifying the time zone in Thymeleaf
Automatically deploy a Web application developed in Java using Jenkins [Preparation]
Automatically deploy Web applications developed in Java using Jenkins [Tomcat application]
[Probably the easiest] WEB application development with Apache Tomcat + Java Servlet
What is the difference between a web server and an application server?
Let's create a TODO application in Java 5 Switch the display of TODO