Early web application structure is very simple, usually the user interface is mixed with business logic. Any party to modify this application will make maintenance throughout the application very difficult. Simplify the application's maintenance of the user interface and business logic, so that the application can change the application faster. This article discusses two technologies of Java Servlet and Java Server Page, which can be used to isolate user interface and business logic. We use a simple application to demonstrate how to implement this isolation, and how to achieve faster deployment and change the application.
First, we will brief Java Servlet and JSP and how they coordinate work in a web application.
What is servlet?
Servlet is a stand-alone Java server-side component. It expands the functionality of the server in a variety of ways while minimizing the overhead of maintenance and support. The servlet is different from the CGI script, which is compiled into a Java's bytecode, so it does not introduce any factors related to the platform, thereby achieving real "Write Once, Run Anywhere".
What is Java Server Page (JSP)?
JSP is a Java technology that establishes a web application with dynamic content. A JSP page is a text document that contains static HTML content and dynamic action tags. These actions describe how to deal with the response of returning to the customer. In the application development phase, the JSP and servlet are very different, however it is compiled into a servlet, executed in the JSP engine. The JSP engine exists in any web application server, such as WebSphere V3.x.
Servlet and JSP work together
In theory, users can only use servlets to receive HTTP requests from the web browser, servlet dynamic processing requests (probably the query backend system to complete the request), then send HTML or XML documents directly in the servlet. Respond to the browser.
The above method is feasible in principle, but it also makes the generation of the web page to Java Servlet. If the designer of the web page must change the appearance of the page, you must edit and recompile the servlet. This will ask the dynamic web page designer needs to have application development experience. Obviously, we need to isolate the display of the web page with the servlet processing request.
Methods to meet the above needs are to adopt the Model-View-Controller model to establish a web application. In the MVC model, the backend system is our MODEL, which is used to create a user response interface is View, and the link linked to the two is Controller. JSP Perfect VIEW in the MVC mode, servlet contains how to handle the logic of the request, and actually becomes Controller, and the system has a business rule is Model.
Let's take an example that it uses the Isolation of the MVC mode to realize the performance layer and business logic, which implements an application from the browser to the 3270 backend system.
The architecture of the sample program is shown in Figure 1. The browser uses HTML and JSP to implement View, and a group of Java servlets in the application server provides business rules or models, such as CICS. Servlet, HTML, JSP page calls are shown in Figure 2, and attached to detailed text description.
Example application process detailed
Initiate request
The user uses the browser to load the initial HTML page, which is provided by the web enabled application server. The application server performs a web file service in the same way as Web Server. This page is the entry point of the entire web application. It uses the HTML Form Action tag to access the servlet. In our example, this servlet is loginservlet. The same additional parameters also sent to the servlet as the action of the Form. Here is an example: