Several common problems to be noticed in Java and JSP programming

Source: Internet
Author: User
Tags resource

1. When using the println () method for objects of type string, if the object is null, it will print null instead of raising the NullPointerException, which is an easy illusion, and can cause problems for subsequent operations on the string.

2. The main reason for the nullpointerexception exception is that there is no validation of the object's existence, often appearing in JSP programming: if (Request.getparameter ("username"). Equals ("xxx")), Out.println (Session.getattribute ("Record")) and so on. The solution to this problem is to perform a null comparison before use: if (Request.getparameter ("username")!=null) {IfIf (Request.getparameter ("username"). Equals (" xxx ")) ...}.

3. Throwing a NumberFormatException exception: The main reason is that the user entered other illegal characters when the user submitted content was converted to an integer or floating-point number. The main way to handle this is to catch this exception using the Try/catch block where the conversion is required and then prompt the user to enter the legitimate data.

4. Throwing a Stringindexoutofboundsexception exception: The main reason is to use a string of substring (), charAt (), and so on, and the length of the string is not enough to throw this exception ; NullPointerException can also be thrown when the string is null. The solution is to NULL, judge the length, or convert to a byte array. Given the large number of operations, they can be encapsulated into JavaBean.

5. Cause Noclassdeffounderror error, the main reason is the class path or class file placement error, the placement of class files to meet the requirements of the server.

6. Throws a Java.lang.Error error, the main reason is that the external resources accessed by the system are not closed, resulting in a large amount of waste of external resources, which may eventually result in the system not functioning properly, too many external resources being accessed by the system, the external system is not working properly, and the external resources accessed by the system are abnormal. The solution is to: before accessing external resources, first, check that the resource (such as the database) is properly connected or manipulated, and if a connection is made to an external resource, a shutdown operation is made, and only one shutdown is performed, and the external resources are shared as much as possible in the same operation to reduce the consumption of resources by the operation. Improve the efficiency of program execution.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.