Java web application development topic: more robust development

zhaozj2021-02-17  54

Foreword

In these months, we have several groups in WEB application development in Java for different customers. We have continuously encountered the abnormalities and errors of the system during the development system. Moreover, it seems that some abnormalities and errors feel very familiar (ie, it will appear!). Recently, I made a simple analysis of the log files of these groups; several exceptions often encountered in the Java web application development process, and some statistics were obtained.

What is the level of the system that the system can make an appropriate response in an unexpected environment such as a hardware failure, an input data is invalid or an operation error. The so-called more robust development means the development method based on enhancing robustness requirements.

This paper mainly discusses the "accidental environment in which the data is invalid or operational error in the input data" is discussed. The system can still adapt the development of the response while reducing the incorrect reference system external resources. Solution to discuss.

I. Log unusual statistics: Before we say that several groups continue to encounter the same exception or error during the development process (there are several old appearance!), I noticed some exception or error message. Keyword. Thus, I have written a simple shell script that analyzes the frequency of these exceptions or error keywords in the running of the server group. It is obviously only the statistical results obtained by statistics of the appearance of a word. Not a very accurate statist; even so, I think this result may also provide some beneficial clues to the improvement of system robustness.

Table 1-1 Server LOG analysis results

Exception or error name Item 1 Item 3 Item 3 mean Exception35581236128429-- java.sql.SQLException1780610066318.87 Java.lang.NullPointerException15957545736431.28 Java.lang.NumberFormatException232107341.77 Java.lang.StringIndexOutOfBoundsException113000.11 Java.lang.Exception172013161.70 Java .lang.noclassdefounderror417402.46 java.lang.error83012918304.74 tahers46713061722239.07

From the statistics of the above LOG analysis, we can easily see:

There are more than 30% of the wrong error; (this article discussions) is approximately two0% java.lang.sqlexception errors (this article does not discuss such JDBC related errors); nearly one-round mistake is Due to java.lang.noclassdeffounderror, java.lang.Error (mainly discussed the target) other errors due to java.lang.NumberFormatexception, java.lang.StringIndexoutofboundsexception caused. (This paper mainly discusses the target) Other types of exceptions or errors: this article is not discussed.

Second, more robust development methods, in order to achieve the purpose of developing a robust Java application system, it is necessary to consider "invalid or operational errors, etc.", so that more rules should be followed. Reduce the frequency of system abnormalities or errors, achieving higher system robustness. The development methods and demonstrations recommended in this paper are mainly for Java web application developers; and also apply to the General Java project you conduct.

Below, I started some analysis of these anomalies and errors in the resulting statistical results:

Empty pointer error java.lang.nullpointerexception The statistical results showed that our project results have nearly 30% or more antennae is an empty pointer exception, which shows that the empty pointer is very common. In fact, I believe that 100% of Java programmers will be very familiar with empty pointers. Using basic Java data types, the value of the variable is either a default value, and if there is no normal assignment, the program cannot be compiled, so the basic Java data type (Double, Float, Boolean, CHAR, INT, long) Will not cause an empty pointer anomaly. It can be seen that the empty pointer is mainly related to the operation of the object. Let's list several situations and corresponding solutions that may have an empty pointer: whether or not the object is empty. (JSP) code segment 1: Out.println ("UserName"); Description: The function of the code segment 1 is very simple, that is, the value of the form field "username" input by the user. Description: It seems that the above statement can't find something wrong, and in most cases, there is no problem. However, if a user does not provide a value of the form field "username" when entering the data, or bypass the form directly input, the value of Request.GetParameter ("UserName") is empty (not empty) String, is empty object null.), The PrintLn method of the OUT object is unable to move directly to the empty object, so the JSP page of the code segment 1 will throw "java.lang.nullpointRexception" exception. Even if the object may be empty, some methods of java.lang.object or Object object itself are also called to Tostring (), Equals (Object Obj). (Jsp) code segment 2:

String Username = Request.getParameter ("UserName");

UserName.equals ("root"))

{

// Actual operation….

} Description: The function of the code segment 2 is to detect the username provided by the user. If it is a user name "root", some special operations are performed. Description: In the code segment 2, if the user does not provide a value of the form field "username", the string object username is a null value, which cannot be directly compared to another object, the same, the code segment 2 JSP page will throw (java.lang.nullpointerException) empty pointer error. (Jsp) code segment 3: String username = session.getattribute ("session.username"). TOSTRING (); Description: The function of code segment 3 is to take the value of session.username in the session and assign the value to the character. String object Username. Description: In general, if the user has already made a session, if there is any problem, if the application server restarts, the user has not logged in, (may also be the user to close the browser, but Still open the original page.) So, at this time the value of the session will fail while causing the value of session.username in the session. For a direct execution of a direct execution of a NULL, it will cause the system to throw an empty pointer anomaly. Solution: To ensure operation or reference objects are not empty, if we want to operate or reference an object, we first check if the object is instantiated and not empty; and adding to an object in the system The processing of the situation. Such as: String object saves the result of the user submit; When the object is empty, the setting object value is an empty string or a default value; how to process mode 2) When the object is empty, do not perform an operation, directly jump to other processing. Processing 3) When the object is empty, it is prompted that the user operates an error. Rewriting the code segment 2 in the above manner, obtained: mode 1:

String Username = Request.getParameter ("UserName");

// When the variable is empty, convert it to the default empty string

IF (username == null)

Username = "";

UserName.equals ("root"))

{

// Actual operation….

} Method 2:

String Username = Request.getParameter ("UserName");

// When the variable is empty, convert to the default empty string, do not perform the relevant operations.

IF (usrename! = Null)

{

UserName.equals ("root"))

{

// Actual operation….

}

} Method 3:

String Username = Request.getParameter ("UserName");

// When the variable is empty, convert to the default empty string, do not perform the relevant operations.

IF (usrename == null)

{

// Tip the user input information is empty} In practice, the above provides three processing methods also applies to other exception processing:

Abnormal processing mode 1) Check to abnormality, setting the object value is an empty string or a default value; exception processing mode 2) detects an abnormality, do not perform an operation, directly jump to other processing. Abnormal processing mode 3) Check the abnormality, prompting the user to operate an error. Format Number Error Java.lang.NumberFormatexception (JSP) Code Segment 3:

String s_memberid = request.getParameter ("memberid");

INT I_MEMBERID = INTEGER.PARSEINT (S_MEMBERID);

// .... Other operation description: The role of the above code segment is to convert the value of the table single field MEMBERID submitted into an integer. Description: If the user enters the correct number such as: 1082, there will be no problem. However, if the user inputs T1082, Java cannot convert it into a suitable number because T1082 is not a legal digital format, causing the java.lang.NumberFormatexception digital formatting exception. Solution: When any string is converted to a number, capture an abnormality, process the abnormal situation Press Outvention Press: Check the abnormality, that is, assign a variable to a variable; (may result in some cases Other programs errors [Compared to the other modules do not process the default values ​​you give to the defaults, some exceptions or errors can occur.]) Press the exception handling mode 3: Check the abnormality, prompting users to enter the correct digital format . (Realize a little more trouble, but block the error before your module [that is, you provide the value to other modules].) This method is rewritten on the program, which is slightly troublesome when programming, but this is inde Will your module is more robust. Rewriting the code segment 3 is rewritten by the above requirements.

String s_memberid = request.getParameter ("memberid");

INT i_MEMBERID;

Try

{

i_memberid = integer.parseint (s_memberid);

// .... Other operations

}

Catch (Numberformatexception NFE)

{

// mode 1: (Simple, directly to the number one default 0;)

i_memberid = 0;

// mode 2: (very simple practice, it is recommended to use more friendly prompts)

Out.println ("