JSP technology is an extension of servlet technology, combined with HTML, Java code on a file. Java Servlet technology focuses on using the PrintWriter.println () statement to generate HTML output Java classes, JSP abstract this concept to a higher level. Using JSP, web developers can write static HTML and add Java code snippets to the page you need to generate, so that this flexible technology makes quick development of simple web applications. However, it can be abused to form unnecessary complex applications that are difficult to maintain, reuse and improve.
Skills following the following tips can avoid this unnecessary complex application.
1, separation HTML and Java
2, put business logic in Javabean
3. Separate common behavior from the label custom manager class
4, more Java code in JSPS, more inclined to HTML in the Java Manager class
5, use the appropriate included mechanism
6, use the JSP template mechanism
7, use CSS style sheet
8, use MVC mode
9, use a valid label custom library.
10. Determine the appropriate XML dependency
11, use JSP comments as much as possible
12, follow the best practice of HTML
13, using JSP abnormal mechanism
These can help you write a reusable, easy-to-maintain JSP
First, Isolate HTML and Java
Put all the necessary Java, HTML code of a web page in a JSP file is attractive. This approach makes the initiator positioning the code and understand how they interact with each other. However, when the application is more complicated, the developer changes more difficult to change, so that it will become more heavy and expensive.
Combined with HTML and Java on the same source of code makes the program very unreadable. In order to enhance readability, many developers use a constituent format, but the files that mix HTML and Java fragments make beneficial constraint formats extremely difficult to maintain.
Many web development methods and mechanisms emphasize the separation of HTML and Java code, so different developers can concentrate their energy in their aspects they are good at. Separating the Java code and HTML, including JSP tags, custom tags, can make web designers, HTML writers, while Java developers work in the logical processing section of the application. When the Java developer implements a JSP custom label, the business logic is business logic, and the web designer uses these custom tags like using the ordinary HTML tag.
Appropriate applications that separate Java code from HTML will be more reusable because Java components are not binding to a particular browser while using other parts of the application software. At the same time, the modularity of the Java / HTML separation is also improved, and the maintenanceability of the application is improved.
Second, put business logic in Javabean
The Java code directly contained in JSP is not as easy as other JSP pages, universal behavior and business logic can be applied to other JSPs, but also other JSPs, but also other parts of the JAVABEAN can be used. This is because JavaBean is only a Java class that meets some basic conventions (such as constructors that do not include parameters, setting the set / get method for the private class attribute), can also be used as any other class. It is worth noting that EJB is also useful when encapsulates all components common to all components in applications.
Third, separating common behaviors from the label custom manager class
As a Java class for custom tag manager, custom labels are customized, are not like JavaBean, which cannot be used as the normal Java tool class, but the custom tag manager category specific interface or inheritance provides basic implementation of these interfaces. . Because they are not easy to use outside the JSP, custom tag manager categories should only include specific behaviors that cannot be used outside of JSP outside of custom tags. Customized tags often need support for universal behavior and business logic, and utilize JavaBeans and EJBs that provide universal behavior
Fourth, more Java code in JSP, more inclined to HTML sometimes separated from the Java Manager class to separate HTML, JSP tags, and such as custom tags from Java, need unnecessary code, based on this, you Or put the Java fragment and the expression in the JSP, or put the HTML code into the Java tab manager class.
It is more willing to see a small part of the HTML code in the Java class in JSP as the JAVA in JSP. Since the custom tag manager is specific to the custom labels achieve (simultaneous use of JSP outside the JSP), there will be no trouble in some HTML code, and Sun's J2EE Blue Book has a more in-depth discussion.
There are also exceptions to this standard: If you contain a row or two lines of Java code snippets in JSP, the Java HTML code resolved in the Java Manager class, then the Java code that is allowed in JSP should be wise.
5. Use the appropriate included mechanism
The mechanism is less design in code reuse. Separating generic JSP and HTML code from multiple pages can increase maintainability (only change in one change) and reusability only.
There are two including mechanisms to narrow down code redundancy promoted code reuse. To ensure that the appropriate include mechanism can be used, it is important to understand the difference between them. Unless I can prove that INCLUDE action is just right, I usually use the include directive. The seventh problem in the Blue Book "Web layer" section, understanding the two include mechanisms and determining which one is used in a particular situation provides a good resource.
6. Using JSP template mechanism
A template mechanism allows a public file to control the web page, JSP, page layout. So, when you want to change the page layout, you just need to modify a file, all other pages will reflect the change of the page layout. This is not only makes the code more maintainability, the page layout template mechanism is more beautiful and friendly for users who have coordinated all application software pages.
I use the custom label template library for a template mechanism, David Geary's article "JSP Templates" provides a good starting point for the use of the template mechanism in your JSP.
Seven, use CSS style sheet
As the template allows developers to put the page layout control, the style sheet can put the developer to control the appearance. I use the CSS style sheet to control items such as font format, size, and table features. Like a template, the style sheet allows developers to change at a change, which will immediately map to all appearance pages, which promotes maintainability and giving the user.