J2EE performance layer design thinking

xiaoxiao2021-03-06  86

Several issues that need to be considered when designing performance layers, can use different models when designing the performance layer, and some related design problems are required. These problems and model relationships are also different, they can affect all aspects of the system, including secure, data integrity, manageability, and scalability. Although most of these design problems can be expressed in the form of a model, we don't plan to do so, because this is more abstract, we choose to represent informal documents. We are just based on different models, and each need to consider the problem. Session Management User Session refers to a conversation between multiple requests between a client and server. We will discuss this problem based on the concept of user session. The status of the client SSSION state refers to the status of the session to serialize the status of the session and embedded to the HTML page returned to the customer. The status of the Session in the client has the following benefits: It implements relatively easy. When saving a small amount of status information, it works very well, this policy also eliminates the problem of copying the status across multiple servers, for example This will encounter this situation when load balancing between multiple servers. Save the Session status in the client usually two methods - HTML hidden fields and http cookies - we will discuss these strategies below. The third policy is embedded in Session status information in the URL of each page, such as

. Although the third method is relatively rare, it also has many other restrictions on two other methods. HTML Hidden Field (HTML Hidden Fields) Although this method is relatively easy, there is still a lot of shortcomings in the client to save the session status using the HTML hidden field. These disadvantages are particularly prominent when saving a large number of states. Save a lot of status will have a big impact on performance. Because each request and response each time you issue a request, you need to transfer these status information in your network. In addition, when you use hidden fields to save the SESSION state, these persistent status values ​​can only be a string value, so all object references must be "character string", and this information unless special encryption, otherwise They are all displayed in the source code of HTML in the form of clear text. HTTP cookies is the same as the hidden fields, and the way HTTP cookies is relatively simple. Unfortunately, these two methods have many identical disadvantages. In particular, the performance will have a large impact on performance when saving a large amount of status information, because every time the request and response, all Session status information must be transmitted on the network. When the Session status is saved, we will also encounter the limitations of size and types. The size of the cookie headers is limited so that the amount of data that can be kept saved and the method of hiding fields, when you use cookies to save the SESSION state, these persistent status information can only use string values. . Security issues in the client Save the SESSION status When you save the Session status on the client, you must take into account the security issues that come. If you don't want data to expose to the client, you need some way to encrypt data to ensure the security of the data. Although it is relatively easy to implement in the client, it has a lot of shortcomings, these must spend time to solve. This approach is not compensated for items that need to handle large amounts of data, especially the company's system.

The session state of the performance layer When the session status is saved on the server side, it is used to get a session ID and keep it until the following scenario: a predefined session timeout happens. Session is manually set to invalid. The status is removed in the session that the server management mechanism in some memory may not be recovered after the server is turned off. Obviously, it is better to place their session status in the server for applications to save a large SESSION state. When the status is saved on the server, you do not have the size and type limits of the client session management. In addition, security issues that have been avoided, and it is not encountered in the performance impact due to the transmission of the SESSION status between each request. Using this way, you can do more flexibly and make it easy to expand and improve performance. If you save the SESSION status on the server, you must decide how to get the status information by each server, that is, the server you run the application. If the cluster software is on the hardware running a load balancing, then the replication problem of this session status is to process, this is a multi-dimensional problem, but numerous application servers now offer a variety of solutions. That is, there is a solution in the level of the application server. One of the methods is to ensure that the user is only dealing with one server. It is used in traffic management software, such as the software of Resonate [Resonate], in the user's session, each request sent by the user will be routed One server processing. This method is also known as Server Affinity. Another alternative way is to save the SESSION state in the commercial layer or resource layer. Enterprise JavaBeans components can be used to save the SESSION in the business layer, and a relational database can be used in the resource layer. Controling customer access There are many times we must restrict or control client access to some application resources. Let's discuss two such situations. One reason for restricting or controlling customer access is to prevent one view or part of the view directly accessed directly by a customer. This problem will occur in the following cases, such as only registration or login users can allow access to a special view, or to limit the user access section according to the user's role. After describing this problem, we will discuss the second case, and it is related to the process of controlling a user in the application. The latter discussion and duplicate Form submission are related, because multiple submission will lead to unnecessary repetitions. Control View Access In some cases, resources are restricted to not allow certain users to access. There are several ways to do this. One way is to join the application logic to the process of processing the controller or the view, disable some users from accessing. Another solution is to set up a running system, for some resources, only allowed to be invoked by another application resource. In this case, access to these resources must be performed by an application resource of another performance layer, such as a servlet controller. For these restricted resources, it is not allowed to be directly called via a browser. A common way to handle this problem is to use a controller as a client as the type of access control. Another common way is to place a protection setting in a view. We mainly discuss view-based control strategies. Before considering what way to choose to control access, we first describe these strategies. Placing protection logic in the view There are two common applications to place a protection logic in the process of a view. One is to prevent access to the entire resource, and the other is the resource restricted to the access part. In each view contains an all-or nothing protection in some cases, the logic placed in the view processing code allows or rejects access to all-or-nothing mode. That is, this logic limits a particular user to access a special view. Usually this type of protection is preferably encapsulated into a centralized controller so that centralized management is convenient.

If there is only a few pages that require protection, you can use this policy. Usually this situation is a static file that happens in a small part of the website that needs to update the website. If the customer still needs to log in to the website to browse these pages, you can use a custom Tag (tag) to add a custom Tag (tag) at the top of each page. As shown in the example of 3.1. Example 3.1 In each view contains an all-or odhing protection <% @ taglib URI = "/ Web-inf / corej2eetaglibrary.tld" prefix = "Corepatter> ...

To add some parts of the view to protect in other cases, the logic placed to the view processing code can reject certain parts of a view. This strategy can be used with the all-or-nothing policy above. To illustrate this, we use control to access one of a room in a building as a comparison. ALL-or-Nothing protection policy tells whether the user can enter the room, and the second protection policy is to tell the user that after entering the room, it allows them to see something. Here is some examples you can use. Depending on the user's role, it is determined whether certain parts of the view according to the user's role, and some part of the view may not be displayed. For example, when a manager is watching management information, he can access the child view of its employee, and as an employee, he can only see the information you organized, instead of accessing other information, as shown in Example 3.2. Example 3.2 According to the user's role, the part of the view is not displayed

<% @ Taglib Uri = "/ Web-INF / COREJ2EETAGLIBRARY.TLD" prefix = "Corepatterns"%> ... this sales be seen only by managers! ...

According to the status of the system or the error, the display may be modified according to the environment of the system, according to the environment of the system. For example, if the user uses a single CPU hardware device, some devices using multiple CPUs can be not displayed.

Access to configure control resource To limit a customer directly access a special view, you can configure that the performance layer can only access these resources through internal resources, such as a servlet controller using RequestDispatcher. In addition, you can also use the security technology built in the web container, according to servlet2.2 or later. Safety restrictions are defined in the configuration description file called Web.xml (deployment descriptor).

The authentication method of BASIC and FORM-BASD is also described in the servlet specification. Here we don't intend to repeat this specification, you can go to the following URL to see the details of the current specification (http://java.sun.com/products/servlet/index.html). You have understood what is used when joining security restrictions on your app, we briefly discuss this issue and introduce how to configure it and all-or-nothing protection. Finally, we describe a simple and common way as all-or-nothing protection to limit access to a resource. The security restriction protection resource application may be configured in a secure limit, and this security restriction allows the use of programmed methods to control access according to the user's role. Resources can be accessed by certain characters and disable other roles. In addition, a portion of a view can also be restricted to access according to the role of the user. If some resources are completely prohibited from all direct browser requests, such as those mentioned above All-OR-Nothing scenarios, these resources can only allow access to some security roles, and these security roles are not assigned to any one. This will only be disabled from all browsers from accessing all browsers as long as this security role is not assigned. Example 3.3 is part of a web.xml configuration file that defines a secure role to limit direct browser access. The name of the role is "Sensitive", the name of the restricted resource is sensitive1.jsp, sensitive2.jsp, and sensitive3.jsp. These customers cannot directly access these JSP pages unless a user or group is assigned to the "SENSITIVE" role. However, due to the internal requests are not limited by these security, the requests for processing by a servlet controller will be guided to these Limited pages, so they can access these JSP pages indirectly. Finally, it is important to note that there is a substantive phenomenon in this area when the products of different manufacturers are implemented in the Servlet 2.2 version. However, the server that supports servlet2.3 does not have this compatibility issue. Example 3.3 Provides all-or-nothing control by dissemination of security roles

SensitiveResources A Collection of Sensitive Resources / trade / jsp / InternaaCcess / sensitive1.jsp / trade / jsp / interfaceAlaccess / sensitive2.jsp / trade / jsp / interfaceAlaccess / sensitive3.jsp get post sensitive Setting up resource protection There is a simple and common way to limit a customer directly accessing a resource, such as JSP. As with the example of 3.3, this method does not need to modify any configuration files. This method just needs to place resources in the web application / web-inf / directory. For example, to prevent the browser directly from accessing a view called Info.jsp, we assume that this file is a web application that belongs to the securityissues. We can put the JSP file in the following subdirectory: / scrub projectues/web-inf/internalAccessonly/info.jsp. For the / web-inf / directory and their subdirectories are prohibited from being accessed directly from the browser, INFO.JSP can also be accessed directly. However, if you need, a controller servlet can still be guided to this resource. This control uses all-or-nothing mode because resources configured in this way are completely prohibited from directly accessing them.

Duplicate Form Submit users When using a browser, you can use the back button frequently, so it is possible to repeat the form they have submitted, which will bring a duplication of transaction processing. Similarly, a user may also press the stop button before receiving a confirmation page, then submit the same form again. For these situations, we want to track and prohibit these repetitions, we can use a control servlet to provide a control point to solve this problem.

Synchronizer (OR DVU) token This policy is to resolve duplicate Form submission issues. A synchronized marker is set in the session of a user and is included in each Form returned to the customer. When the Form is submitted, the synchronization tag in the Form is compared to the synchronization tag in the session. These two tags should be the same when Form is submitted. If the tag is different, then the Form will prohibit submission, and an error will return to the user. When the user submits an FORM, if you press the back button in the browser and try to resubmit the same form, the mark will do not match. On the other hand, if the two tag values ​​match, then we can confirm that the entire process is correct. In this case, the tag value in the session will be modified to a new value while allowing the FORM to be submitted. You can also use this policy to control direct access to some pages, just as described in the above resource protection. For example, suppose a user collects a page A of an application to a favorites, while the page A is only allowed to access by page B and C. When the user accesses the page A directly through the favorites, the access order of the page is incorrect, so that the synchronization tag will be in a state in which it is not synchronized, or it does not exist at all. No matter what, the access is banned. Verification usually we all want to verify at the client and server. Although the client's verification process does not have a professional, it can provide a high-level check, such as whether a field in the Form is empty. The server-side verification is usually much larger. Although in one application, both types of processing are appropriate, but it is not recommended to use only the client authentication. One of the main reasons why the client's verification is the use of the client's script language, users can use them at any time to skip these scripts. It is not intended to discuss verification strategies in detail. We just mention here that this is a question that needs to be considered when designing the system. If you want to know more deeply, you can refer to existing literature. Verification of the client verification input is performed on the client. Usually this operation is performed by embedded script code, such as JavaScript. It has been mentioned above, the client's verification is a supplement to the server-side verification, but it should not be used independently. Verification of the server-side verification input is performed on the server. There are several typical strategies to be used as server-side authentication. These strategies are Form-Centric Validation and Validation Based on Abstract Types (based on abstract type authentication). FORM-CENTRIC VALIDATION FORM-CENTRIC VALIDATION The policy forces an application contains many ways to verify the status of the Form. Typically, these methods are overlap in terms of logic they include, which is not conducive to reuse and modularization. One verification method is related to a particular form of Form, there is no common code to handle common operations, such as required fields or only the fields of numbers. In this case, when a field is used in a plurality of different forms and is a required field, it is handled independently in the application. This strategy is relatively easy, and the efficiency is also high, but it will bring repetitive code when the application becomes large. To provide a more flexible, reusable and easy-to-maintain method, the data model should make greater abstraction. This method is the "verification according to the abstraction type, the example 3.4 is shown in the example 3.4 of Form-Centric verification.

Example 3.4 Form-Centric Validation / ** If the first name or last name fields were left blank, then an error will be returned to client. With this strategy, these checks for the existence of a required field are duplicated. If this valid- ation logic were abstracted into a separate component, it could be reused across forms (see Validation Based on Abstract Types strategy) ** / public Vector validate () {Vector errorCollection = new Vector (); if ((firstname == null) | (firstname.trim.length () <1)) ErrorCollection.addelement ("firstname required"); if (LastName == Null) || (Lastname.trim.length ()) ErrorColction.addelement (" Lastname Required "); Return ErrorCollection;} Abstract class verification This method can be applied to the client or server, but it is best to apply on the server side in a browser or thin client-based environment. The input and restriction information is abstracted in the model state and put it in a universal architecture. This reduces the application logic of the model's verification and the application of the model, thereby reducing their coupling. Model verification is performed by comparing metadata and restrictions and model status. The metadata and limits of the model can usually be accessed by some simple data storage, such as an attribute file. The advantage of this method is the system more common because it separates the status and restricted information by the application logic. For example, there is a logic of component or subsystem package authentication, including determining whether a string is empty, a number of inputs in a range, or a string formatted to some way. When different application components need to verify different aspects of a model, each component does not need to write their own verification code. Instead, use centralized verification techniques. Centralized verification techniques can be set by some ways, such as programming, some generators, or usually declared, using configuration files, etc. This verification technology is more common, it focuses on the status of the model and its needs, and is independent of the other parts of the application. One disadvantage to use this method is that efficiency and performance are affected, and usually more common methods, although more powerful, it is difficult to understand and maintain. The following is an example. A configuration file using XML describes a variety of verification, such as "required fields", "requires all of the numbers", etc. In addition, each verified management class can be designed. Finally, a shot corresponds to the value of the HTML table and a particularly verification. Verify that an example code for a particular field is shown in Example 3.5. Example 3.5 Validation Based on Abstract Types

//firstnamestring="dan"//mfieldname="form1.firstname"validator.getInstance( ).validate (FirstNameString, FormfieldName); Helper Properties - Integrity and Consistency JavaBean Helper class is usually used to save a customer request transmission Come over the middle state. The JSP running engine provides a technology that automatically copys these parameter values ​​by a servlet request object to the properties of these JavaBean Helper objects. The JSP syntax is as follows: The above statement tells the JSP engine to copy all the matching parameter values ​​to a JavaBean's corresponding attribute (this JavaBean's name is "Helper "), As shown in Example 3.6. Example 3.6 Helper Properties - a Simple JavaBean Helper

public class Helper {private String first; private String last; public String getFirst () {return first;} public void setFirst (String aString) {first = aString;} public String getLast () {return last;} public void setLast (String {Last = astring;}} So how do you match? If the name and type of a request parameter are, like the properties of Helper Beans, they think they are matched. It is usually compared to the type of each parameter and the name of the name of each bean property and the type of the setter method of the bean property. Although this skill is very simple, it can produce some confusing things. First, pay attention to if the value of the request parameter is empty, how will it be? Many developers may think that when an REQUEST parameter is an empty string, if it and a bean property match, the bean property will receive an empty String value or NULL. In this case, the result is no modification of the matching bean property. Also, since the JavaBean Helper object is a cross-multiple request reuse, this vague approach will make the data value is incorrect and incorrect. Figure 3.1 shows the problems that will bring this skill.

************** Figure 3.1 *************** Request 1 contains a parameter value name "first" and "Last", and These values ​​set the corresponding bean properties. Request 2 only contains a value of the "Last" parameter, so only one of the parameters in the bean, and the value of the "first" parameter has no change. It is not set to an empty field string or NULL, which is because there is no value in the requesting parameters. As shown in Figure 3.1, if the value of the bean does not have a manual reset between the request, this is inconsistent with the problem. When designing your app, another question to consider is how the HTML FORM interface is handled when the project in the Form is not selected. For example, when an Form has multiple Checkbox, the value of the server is not emptied when no checkbox is selected. When the Request object is generated by this interface, there is no corresponding Checkbox parameter in the Request object. Therefore, these Checkbox-related parameter values ​​will not be sent to the server side (complete HTML specification, see http://www.w3.org/). Since there is no parameter value to transmit to the server, as described above, when using , the matching bean property will remain unchanged. In this case, the application will have inconsistent and incorrect data values ​​unless the developer manually modifies these values. A simple solution is to reset the status value of all javabeans between each request. The parameter value is copied to a JavaBean's corresponding attribute (this JavaBean's name is "helper"), as shown in Example 3.6. Examples 3.6 Helper Properties - A Simple JavaBean Helperpublic class Helper {private String first; private String last; public String getFirst () {return first;} public void setFirst (String aString) {first = aString;} public String getLast () {return Last;} public void setlast (string astring) {last = astring;}} So how do you match? If the name and type of a request parameter are, like the properties of Helper Beans, they think they are matched. It is usually compared to the type of each parameter and the name of the name of each bean property and the type of the setter method of the bean property. Although this skill is very simple, it can produce some confusing things. First, pay attention to if the value of the request parameter is empty, how will it be? Many developers may think that when an REQUEST parameter is an empty string, if it and a bean property match, the bean property will receive an empty String value or NULL. In this case, the result is no modification of the matching bean property. Also, since the JavaBean Helper object is a cross-multiple request reuse, this vague approach will make the data value is incorrect and incorrect. Figure 3.1 shows the problems that will bring this skill.

************** Figure 3.1 *************** Request 1 contains a parameter value name "first" and "Last", and These values ​​set the corresponding bean properties. Request 2 only contains a value of the "Last" parameter, so only one of the parameters in the bean, and the value of the "first" parameter has no change. It is not set to an empty field string or NULL, which is because there is no value in the requesting parameters. As shown in Figure 3.1, if the value of the bean does not have a manual reset between the request, this is inconsistent with the problem. When designing your app, another question to consider is how the HTML FORM interface is handled when the project in the Form is not selected. For example, when an Form has multiple Checkbox, the value of the server is not emptied when no checkbox is selected. When the Request object is generated by this interface, there is no corresponding Checkbox parameter in the Request object. Therefore, these Checkbox-related parameter values ​​will not be sent to the server side (complete HTML specification, see http://www.w3.org/). Since there is no parameter value to transmit to the server, as described above, when using , the matching bean property will remain unchanged. In this case, the application will have inconsistent and incorrect data values ​​unless the developer manually modifies these values. A simple solution is to reset the status value of all javabeans between each request. Guidelines, including design issues, refactoring, and patterns, provide further information and better options. We will not discuss each bad habit very deeply. Here is just a point until, and interested can continue to study. A Summary Code Problem Summary Customized Tag Helper can be included at the top of the JSP view to perform access control and other types of checks. If a large number of views contain similar helper references, maintaining these code will become very difficult because modifications can happen in multiple places. Workaround: Modify the control code, use a Controller and related Command Helpers. When you need to include similar control code in multiple places, such as only part of the JSP view is required to prohibit some users from accessing this job through a reusable Helper class. The data structure of the commercial layer exposed the performance layer data structure, such as HTTPSERVLETREQUEST, should be limited to the performance layer. If these details are placed in the business layer or other layers, they will increase the coupling between the layers, which reduces the reuse of available services. If a business service method accepts an HTTPSERVLETREQUEST type parameter, this service is used to encapsulate their request status to an HttpservletRequest object. In addition, the service of the business layer needs to be known to interact with the data structure of these performance layers, so that the code of the business layer is complex and increases the coupling between the layers. Workaround: Do not allow the data structure of the performance layer to share, but a copy-related state to a more common data structure and share it. You can also choose to separate the relevant state in the performance layer data structure, as a separate parameter sharing. Data Structure Problem Data Problem Data Problem in the Domain Objects The request processing data structure and domain object sharing, such as HTTPSERVLETREQUEST, which will unnecessarily add two different aspects of the application.

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

New Post(0)