Isolation Express Layer and Business Logic, implement multi-level web applications

xiaoxiao2021-03-05  25

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:

..

The request is typically initiated in HTTP or HTTPS and then processed by servlet. It uses the httpservletRequest.getParameter () method to access the parameters provided by the Form form.

Servlet then tests if the user has session. Session is used to associate a series of requests from the remote client, which is necessary for the HTTP protocol for stateless holding functions.

Session can be created as follows:

HttpSession session = req.getations (TRUE);

Objects can be added to the session:

Session.putValue ("Host", HostToconnectto;

Session.putValue ("Port", TMPPORT;

CONTROLLER

The servlet is responsible for calling the JavaBean that handles the user request. It is also responsible for creating responses to users. In our suggested application structure, the response will be passed to JSP. Therefore, servlet is called Controller.

Back to our example. The following code instantiates a new class of a type CICSemulator called Newemulator. This is a 3270 terminal simulation class provided by third-party manufacturers, which provides a set of methods for direct manipulation of simulation terminals (similar classes can also be generated with Enterprise Access Builder in the IBM Visualage for Java Enterprise Edition). Newemulator can transfer variables from the session, which variables are remote hosts and ports.

CICSemulator newemulator = new cicsemulator (); newemulator.settn3270port (porttoconnectto); newemulator.settn3270host (HostToconnectto);

The second instantiated class is a Java bean called acctDetails. It is a data access bean that looks up and obtains data to the rear system. AcctDetails uses Newemulator as its 3270 terminal, then delivers the set method of the variable (surname and name).

AcctDetails getAccountDetails = new AcctDetails (); getAccountDetails.set3270Emulator (newEmulator); getAccountDetails.setSurname (inputsurname); getAccountDetails.setFirstName (inputfirstname);

Data Access Bean Execution Request

Bean instance getaccountDetails method PerformWork (), which connects the rear end 3270 system, and obtains the details of the user account based on the name provided by the user.

Try {getAccountDetails.performWork (); session.putValue ("resultsbean", getaccountdails;

Catch (IllegalStateException E) {// Handle the Error}

If the execution is successful, servlet puts the getaccountdetails bean into the user's session. This bean's identifier is RESULTSBEAN, which contains the result of the user query, which can be accessed with a bean GET method.

Control right to JSP

We have mentioned that the focus of MVC mode is that it isolates the performance layer and business logic. Servlet is responsible for handling the request, which will call the data access bean, and the data access bean getaccountdetails contains server-side logic that accesss and obtains data from the backend system. The backend system (model) contains business rules. The response to the user is created by JSP.

Servlet follows the following syntax while passing the request and response objects to JSP:

GetServletContext (). getRequestDispatcher ("/ jspdemo / login.jsp"). Forward (REQ, RES);

The Forward method allows the servlet to pass the process of the response to a third party. Its parameters Request and Response must be the object that is passed when calling the service method of the servlet. It uses the getRequestDispatcher object to get RequestDispatcher to determine the path to the purpose of JSP. The JSP path name starting with "/" is explained to the root path relative to the current application context environment.

Visit ResultsBean

JSP is responsible for creating a response to the user, which can access the data of the getAccountDetails bean (use the identifier ResultsBean to reference). These use the following syntax in HTML to complete:

..

Where is to find existing objects (this example is RESULTSBEAN). In this example, we put the getaccountdetails beans into the session and named RESULTSBEAN. Then, the above JSP statement has obtained ResultsBean from the session, and then it can be obtained by accessing the resultSbean's GET method.

You can also be implemented by the following JSP statement:

<% = ResultsBean.getDetails_title ()%> <% = resultsbean.getdetails_initial ()%> The above statement is also an example of a JSP expression. All content between <% = and%> is put into the JSP engine, and the result is sent to the JSP file as the output. The above two expressions will perform the GET method of the ResultsBean to display the user's title and middle_initial. As with any scripting language (such as JavaScript), the JSP code can be embedded anywhere in the HTML page. JSP's grammar is also very simple, which means that JSP can be maintained by web page designers rather than application developers. Developers are responsible for servlet and data access beans. Any changes in JSP will not affect the servlet, and vice versa.

response

JSP is dynamically translated into Java Servlets during the request and caching in the application server. The response of subsequent JSP requests will be significantly accelerated. The ultimate response received by the user is the HTML page (with .jsp is extension), which contains dynamic content.

to sum up:

In the application structure of this example, the request initiated by the client browser directly reached the servlet, and then uses the data to process the request to obtain data from the backend system. Servlets Pack the result into ResultsBean, put it in the session, then call JSP to handle this response. Servlet is an initial request to an overall controller that generates a response.

JSP decides to generate content to the user response. JSP should only contain how to format the logic of the performance layer. This isolation has the advantage in which it creates reused, portable, platform-independent components in applications, which can be part of a more apparent application.

Isolation Servlet Development and JSP show great convenience to the application developers and web page designers. Similarly, this method also perfectly complies with the MVC design model described in this article.

Author: Wells Ming Chan E-mail: jingmc@cn.ibm.com

转载请注明原文地址:https://www.9cbs.com/read-35746.html

New Post(0)