[Java] Web application structure by Java and processing flow in presentation layer

4 minute read

Introduction

In the new employee training, I developed a Web application using Java, so I would like to summarize the lessons learned there. I think there are many mistakes, so I would be grateful if you pointed 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-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 are performed by HTTP protocol. When displaying a dynamic Web page, some processing (program) must be performed on the Web server side. That process is a web application, and we will implement it in a server-side language such as Java or PHP. This time, it is supposed to be implemented in Java.

In order to run that application, you need to have an application server separate from the web server. (Tomcat is well known as Java application server.) The application server, like the client and the web server, needs a certain protocol to interact with the web application. As mentioned above, when Tomcat is used as the application server and Apatch is used as the Web server, the mod_jk linkage module is implemented as an extension function in Apache. The application server and web server can send and receive using the mod_jk protocol called ajp13. Since Tomcat has a simple web server, you can send and receive without any special cooperation.

Web application structure

Now let’s take a look at how the web application is processed on that server side.

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

・Presentation layer ・Business segment ・Integration layer

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

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

Flow of application processing on the server side

 Screenshot 2020-06-04 20.05.12.png

Request information is first passed to FrontController. This Front Controller is a servlet that centrally manages web application processing.

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

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

Pass the Connection object to the DAO constructor argument to generate a DAO object. Then call the DAO method and receive the return value using Entity.

Return value setting processing method in DAO (in case of single search) If there is a search result in `while(res.next()), the process in {} is performed. Set the return value using res.get~ in {}

After the business Logic receives the Entity object, store it in the defined object and set it in the return value.

Generate business logic in Action and call the method. Set the result to the scope etc. Set the transition page destination as the return value.

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

Application behavior in the presentation layer

Servlets and JSP

You need a program called a servlet to generate an HTML file that displays a web page. The servlet is run by a web application server such as Tomcat.

Although it is possible to display a web page with this servlet alone, the program description will be complicated and it will not be a beautiful program, so request the web page display to a JSP with embedded Java code in HTML. Will be done.

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

GET and POST

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

In the GET operation, the arbitrary value set for 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 are that the parameter length is limited and that the security is low as mentioned above.

On the other hand, in the POST operation, the arbitrary value set in the parameter is sent to the Web server in the form of being stored in the request message body instead of being embedded in the URL. Therefore, it is more expensive than GET operation in terms of security. In addition, there is no limit on the length of the parameters, and it is possible to send large amounts of data.

In the process of the application, in the user-defined servlet, write it by overriding the doGet method and doPost method defined in HttpServlet. This allows you to use HTTP features.

Summary

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