Troubleshoot web development system exceptions

Source: Internet
Author: User

Http://www.uml.org.cn/j2ee/201109143.asp

In web application development and design, system error reporting, error prompts, and rule verification are indispensable. Different people have different solutions. Here I will list common practices:

1. No prompts

2. directly on the JSP page: Alert ('Operation failed ');

3. In the server console, system. Out. println ('failed to insert data! ');

4. Throw new runtimeexception ("failed to save") in the background ");

5. An error occurred while redirecting to 500.jsp.

I think there are some shortcomings in the above five practices. I will explain them as follows:

1. For any successful project, such a user experience can directly make people vomit. Using the word "product" to name this system is an insult to the word "product, no response. I thought you were drawing a prototype!

2. In the JSP page, alert makes a little progress, but still cannot guide the customer correctly. Is the quotation time not yet reached or the price quoted too high? Without specific business instructions, customers who are puzzled immediately pick up the phone and call your customer service to let the customer service serve the customer. Then, this problem is generally not modified as a bug...

3. This is a typical "programmer" error reporting method. The people who write this Code are still stuck in the school, that is, the coding debugging habit at the time of graduation. They do not need debugging at all and start from printf completely ,,, you said to him here: "My process cannot go down." He will not be busy and will explain: "You wait. I will go to the console. Oh, you know, you haven't made any operations on xxx yet. "It's speechless. Is this system used for customers ,,,

4. programmers who can write if (true) throw new Exception Code. Congratulations, you have already been away from the cainiao level and have a certain understanding of code, design, and product experience, however, for various reasons, it is impossible to perfectly pass this business prompt to the front-end for display. I will focus on this issue later.

5. The most standard practice. This practice can solve most of the problems. However, in the face of the popularity of Ajax today, it is not a complete solution to prompt errors by redirecting to the page.

Next I will give some experience on design exceptions. Here, I don't want to talk about empty theories. All of them will involve some technologies and frameworks. The front-end framework technology can be used in a similar way.

To solve the exception, take the following steps:

1. Design exception

2. Throw an exception in a proper place

3. display the exception prompt in a proper place

4. How to "automatically" error messages without the need to manually compile the display Exception Code in JSP (here we need to integrate the front and back-end framework and modify the source code)

Design of 1st problem solutions exception:

Because the topic of this article is the J2EE exception design, I will not discuss in depth the common topics that have been checked for exceptions or not checked for exceptions.

My approach is to design exception classes. First, design an exception superclass.

Public class wsdexception extends runtimeexception {

/** Serialversionuid */
Private Static final long serialversionuid = 1l;

/** Default_layout default exception tiles template */
Public static final string default_layout = "basesidelayout ";

Private string layout = default_layout;

Public wsdexception (string MSG ){
Super (MSG );
}

Public wsdexception (string MSG, string layout ){
Super (MSG );
This. layout = layout;
}

The purpose of layout is explained later. Remember that there is such a thing.

Then each business template inherits this wsdexception class. When throwing a Business Code exception, you can write throw new bargainexception ("your offer is too high! "), Some people worry that the exception class should be placed under the package path

Is it com. AA. BB. exception or com. AA. BB. Action or com. AA. BB. domain? I personally prefer to place it in the same level as the service where the exception is thrown. For example, bargainexception can be placed under the com. AA. BB. service directory,

For example, in spring source code, such a rule is used to place exception classes. You do not have to place exception classes in a separate directory unless it is a common exception base class of your core package.

2nd Question exception design: how to throw an exception in a proper place?

Here, the successful operation content prompts me not to talk about it, and there is no need to talk about it. Here we will consider how to handle it when the error fails. Throwing an exception is obviously in the background Java code, if the condition is not true, an exception occurs during running. For example

If (select count (ID) from user) = 0

Throw new userexception ("the user does not exist! ");

The exception thrown in this way is an elegant way. I will elaborate on Step 3 and 4 to learn how to detect NULL pointer exceptions, insert too many fields, and handle exceptions. '

Where are the 3rd questions displayed with an exception prompt?

Today, when Ajax is widely used, an error message is displayed when you jump to the 500 page. You may say, "It's very simple. I won't use alert (JSON) in the Ajax callback method ?", This is not a problem. You are right. Don't worry. Let me analyze it slowly:

Assume that AJAX is used to query User List data. If this function is called normally, the value in the callback function (JSON) JSON is a group of users, but what happens if the Ajax request fails to be called? The database connection may fail, or the permission may be insufficient,

Does your "alert (JSON)" still work? This JSON parameter indicates whether it is an array or an error prompt string or unknown. Maybe you tell me that I can write typeof (JSON) if... else alert ('json. It is rare for you to think so much.

You are already a Web Developer. You don't have to worry about the number of functional bugs you write, the number of system pages, and the number of features, can you ensure that most coder jobs are written by new graduates? The current impetuous IT environment in China is: fewer or fewer people will write code, and no code will be written.

I can't guarantee that every brother-in-law will have the statement if (to determine the JSON type). This is easy to omit and won't be written. Do you know whether you are using synchronous or asynchronous calls?

If you reference the same page, some are $ (). load (), include JSP, IFRAME, pop-up window, or synchronous or asynchronous requests. How do you intelligently judge and prompt? Of course, this can be done: the synchronization request jumps to error 500.jsp,

Asynchronous requests can automatically prompt alert ("Exception error prompt thrown in the background"). How nice is this? Remember, I am talking about automatic prompts, instead of writing the prompt code at the front-end, many new users will forget this alert. Generally, when the project is in a hurry, it will be forgotten.

4th questions how to "automatically" prompt for exception errors without the need to manually compile the display Exception Code in JSP

Problem 4 is the solution to problem 3. First, the exception information should be automatically prompted by the Framework Code during asynchronous call at the front-end, how can we intelligently determine whether the browser is a synchronous or asynchronous request?

Synchronous requests place errors in beans. asynchronous requests output JSON data, for example, {success: false ,. faile: 'true', 'result': 'database connection failed '}.

The following is my solution:

4.1 Use the simplemappingexceptionresolver class of spring to solve exceptions in a unified manner. Let's look at dispacherservlet. Spring MVC will have a unified exception parser in the request process, we can write our own exception parser to judge,

If it is a synchronous request, an error is returned. in the modelandview of JSP, if it is an asynchronous request, we will output a string in JSON format, instead of returning an error page. How can we determine synchronous Asynchronization? Let's see the head information in the request.

Someone may ask: I use jquery's $ (DIV ). load ('url. do ") the returned result is a page. Strictly speaking, you cannot regard it as Asynchronous, because it loads a page to fill a div. At this time, you can use $ (DIV ). load ('url. do? Isajax = true ")

Of course, $. getjson $. Ajax is a typical Asynchronous Method.

The problem comes again. I threw out an exception. I also used my own exception parser to output a JSON string for asynchronous calling. Then, if the front-end does not use alert, why does it automatically prompt? The key to the problem is: Do you always use the Ajax framework when writing asynchronous calls? Don't tell me about native

Xmlhttprequet, No matter you are using ptototype ext jquery motools, you can do a little bit in the source code of the Ajax framework: There is a callback function in the source code of the Ajax request, your background returns a {success: false, 'result': 'database connection failed '}., we can

In the Framework source code, if (JSON. faile = 'true'), and then alert (JSON. result). You don't need to write an error message in your JSP. How nice is this? (jquery is recommended. message). The prompt is more friendly than alert.

You don't need to write alert the day before yesterday. The prompt is "automatically prompt", no matter whether the database field cannot be found or the file fails to be saved in the background!

Finally, this article explains layout. If you are interested in discussing it, you will use it when creating the tiles template. Will the pop-up page always change the Template Name? Haha

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.