Servlets and JSP, Best Practices

zhaozj2021-02-16  53

Servlets and JSP, Best Practices

Author: Qusay H.Mahmoud

March 2003

Java servlet technology and JSP technology make Java server-side technology, currently controlling the entire server-side Java technology market, and gradually becoming standards for building commercial web applications. Java developers like these technologies are due to a lot of reasons, including: these technologies are easy to learn, write, run, run, run anywhere. More importantly, if more efficient uses the following practice, servlet and JSP can help separate the Web representation and content. "Best Practice" is a good way to develop servlet and JSP-based web applications based on high quality, reusable and easy-to-maintain. Corresponding to this is that Java code is mixed in HTML, which is easy to produce low efficiency, not easy to reuse, and difficult to maintain. Best practices will change these drawbacks.

In this article, I will describe the importance of the best practices for servlets and JSP; here I want to readers to understand the basic working principle of both. This article will cover the following:

l Briefly introduction Java Servlet and JavaServer Pages (JSP)

l Provide some tips, techniques and rules for the development of servlets and JSP.

l Provides best practices for servlet and JSP

Overview: About Servlets and JSP Pages

Similar to the common gateway interface (CGI) script, servlet provides a programming model for request / response. When the client submits a request to the server, the server submits this request to the servlet, and the servlet constructs a response (response), which is returned by the server to the customer. Unlike the CGI script, servlet is running in the same process with the HTTP server.

When the client's request is established, the method service method is called and passed to the Request class and the response class. Servlet first checks the type of the request as get or post, and then call the Doget or Dopost based on the situation. If the type of request is GET, call the Doget method, otherwise call Dopost. Both methods accept two parameters: Request (httpservletRequest) and response (httpservletResponse).

In the simplest case, Servlets and Java classes can use the print method to generate a dynamic web page. For Servlet, they must run in the container, and the container provides management of various types of life cycles. Therefore, writing servlets can get all Java benefits from the Java platform, such as a "sandbox model (secure)", using JDBC to operate, cross-platform characteristics, etc.

JavaServer Pages (JSP)

JSP technology is a higher level of abstraction of servlet technology. It is SUN development, open technology, is a technique similar to Microsoft's ASP dynamic web technology, and it is a key component of Java2 Enterprise (J2EE). Currently, many commercial application servers (such as BEA WebLogic, IBM WebSphere, Live Jrun, Orion, etc.) support JSP.

How does JSP work?

A JSP page basically comes from a traditional HTML mixture and some Java code mixed, and with .jsp as a file name suffix. This tells the server that this page requires special processing to implement a dynamic web page.

When a JSP page is read, he will first compile (JSP engine to do this) is a servlet. At this time, this servlet is handed over to the servlet engine like other servlets. The servlet engine then reads the class (ClassLoader) corresponding to the servlet and execute it, producing a dynamic HTML page (Figure 1). This servlet creates some required components and then writes these components as a string to outputstream and is displayed in the browser. figure 1

The next time this page is requested, the JSP engine only runs the generated servlet unless this page is changed. If the page changes, the JSP engine automatically compiles this JSP file into a servelt and runs.

Best Practices

In this section, I will describe best practices in Servelt, particularly JSP. Emphasis JSP is because JSP is more widely used than servlet (perhaps because JSP technology has promoted the separation of logic). Best Practice for Integrated Servlet and JSP is "Model-Display-Controller" design mode (MVC), which will be discussed in the back of the article.

l Do not use Java code in the HTML page: Put all Java code directly in the JSP page, there is no problem for small items, but excessive use will lead to code, difficult to read, difficult to understand. The way to reduce Java code is to write a stand-alone Java class to implement logic.

l Adopt suitable include mechanism: static data, such as header, page corner, navigation bar, etc., it is best to be placed in separate files and do not need to be automatically generated. Once they are independent, you can include one of the following two mechanisms, refers to a consistent look in all pages: 1. Use the include directive: <% @ include file = "filename"%>%> 2. Using behavior: first mechanism, when the JSP is converted to servlet, the content of the specified file will be included, this operation is completed by the servlet engine. Second mechanism, when the page is executed, the page contains the content generated with Response. When the page containing is not changed, I recommend using the first way, this way is faster; when the included files are often changed (where there is also dynamic content), the second method is used.

Another containing mechanism is a behavioral tag using in the JSP Standard Tag Library (JSTL). You can use this way to include local or remote files, as follows:

l Do not mix logic with the representation: In more complex applications, and more code is introduced, it is important to make logic and representation in the same file. Separate logic and representation make it not to affect the other party when any of these needs to be changed. JSP is only represented as a front desk. So how do you implement business logic? This is the land of Javabeans. JavaBeans technology is a lightweight, platform-independent formation model that enables developers to write components and can run everywhere. In the JSP environment, the JavaBeans component handles business logic and returns data to the JSP page. The JSP page operates the properties of the bean by calling the GET method of the component and the SET method. The benefits of using JavaBeans technologies are as follows: 1 Reuse: Different applications can use the same component. 2 Separation logic and representation: You can change the display appearance of the data on the JSP page without affecting business logic. In other words, web designers only need to focus on design, Java developers only need to pay attention to business logic. 3 Protect your intellectual property. If you use Enterprise JavaBeans (EJB) components in your project, business logic must be retained in the EJB component, providing lifecycle management, transaction support and control of multi-user access. About this, you can find more detailed information in Enterprise BluePrints. l Use custom tags

l Do not reinvent the wheel: For ordinary markers, the marks and sufficient of JSTL are sufficient. You can use it. Developing custom tags means more testing and debugging.

l Use the JSTL description language

l If possible, use a filter: Filter is a new feature of JSP technology.

l Use a lightweight privacy model

l Use database for persistence information

l Cache content:

l use the connection pool

l Cache database query Result: Do not use the ResultSet to cache data, copy the data in the resultset to Vector or Rowset

l The new JSP XML syntax may be used.

l Learning and app Enterprise BluePrints

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

New Post(0)