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.
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.
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.
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.
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.
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.
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