SERVLETS and JSP Development Principles (below)

zhaozj2021-02-16  46

SERVLETS and JSP Development Principles (below)

Don't "reinvent the wheel", don't start from beginning to: improve your reuse through custom components, but custom components still need to write, test, and debug programs. The problem is that this thing may have already achieved, and your implementation is not necessarily better than others. This is what the JSP Standard Tag Library (JSTL) (JSTL, please refer to the JSTL official website). JSTL provides various tags such as loop, read properties, traverses various data structures, conditional expressions. It also provides some complex tags, even as marks that the XML document is marked. So if you want to use a tag, it is best to see if anyone else has implemented, but you will start from the beginning, you can engage it.

Use JSTL expression to make the language (JSTL Expression Language): The data passed to the JSP page is generally performed by JSP scope attributes or request parameters. Specially designed for web developers (Expression Language, EL) transfers information using scope properties as a standard way to transfer information from business logic to JSP pages. It should be noted here that EL is just a key aspect in JSP technology, is not a general programming language. Instead, it is just a data access language that simplifies access to the application of the application, and the data can be accessed without Scriptlet and the request value. In JSP, the web designer should use an expression syntax <% = name%> or JavaBean component to obtain certain variables or attribute values, for example:

>>>>

or

<% = acuStomerBean.getaddress (). getCountry ()%>

Expression makes language allow web designers to access information using a simplified syntax. If you just want to access a simple variable, you can use this syntax:

If you have to access a nested JavaBean property, you can do this:

Expression Language (EL) borrows JavaScript grammar, so if you are very familiar with JavaScript, you will feel huge.

Using a filter: The filter is an object that can transmit requests or modify responses. It can prepare it before the request arrived to servlet / JSP, and it can be processed after the response leaves the servlet / JSP. So if you have a few servlet / jsp need to perform the same data conversion or page processing, you can write a filter class, then use the filter with the corresponding servlet / JSP in the deployment description file (web.xml). Link.

Creating a filter is actually easy, you only need to implement the Javax.Servlet.Filter interface and its three methods:

Public void init (FilterConfig Config)

Public Void Dofilter (ServletRequest Req, ServletResponse Rep,

FILTERCHAIN ​​chain

Public void destroy () This, you can complete your filter.

Using portable security models: Most application servers provide security models, but they are all proprietary for a server or a vendor. If your application needs to be transplanted, then your app is best to use the secure model that can be portable. If your app has some predefined fixed users, you can use from Verification and Basic validation. But if you want to generate a customer (usually this), you may need to create and manage users using server-specific APIs. This way you may touch the API incompatibility when your application is ported to another server. In this case, the best solution is to use the Adapter mode (if you are not familiar with the design pattern, please see the "Design Mode" book for GOF).

Save Persistence Data with a database: The HTTPSession object can be used in the servlet / jsp is also a session object to save the user's temporary data. However, if you want to save your persistence data, you should use the database, data saving data is safer, and there is no requirement for your browser. This makes your data are still good even if your application server has collapsed because of some reason.

Cache page: There are always some things in the application to be relatively fixed, while others often change. You should use a static HTML document to store those relatively fixed content so that the client can make a cache, and each customer has access to your application, just access the part of the changed. This can speed up the customer's access speed.

Use the connection pool: If you want to write the database yourself to access the code, I think I should learn how to use the database connection pool technology. Each server has a configuration document for database connection pools, and you have to learn how to use it. The database connection pool can accelerate your application's speed, and because the server manages the database connection for you, this saves you a lot of work.

Cache Database Access Result: If your application is accessible frequently, you can use an object to cache your data so you can save a lot of access to the database. On the "J2EE Core Mode" and "Practical J2EE Design Mode Programming Guide" two books on Value Object Pattern, you can refer to these two books to get the appropriate knowledge.

Using Data Access Object Mode: If your application needs to access multiple database systems or may be ported to other storage systems, then your optimized code you may have failed. There is a problem with the implementation efficiency using the general code, and the use of optimized code has existential problems. Therefore, Data Access Object Pattern (DAO) has produced both the adaptability of each database manufacturer, but also uses the unique benefits they offer. In accordance with the principle of object-oriented separation tasks, this mode will isolate logically to its own class with the logic of Enterprise Information System (ENTERPRISE Information System, ES). In this way, things objects, such as servlet / JSP components, JavaBeans can use Data Access Objects (DAO) to process all transactions related to EIS.

It is best to use JSP XML syntax: there are often two syntax to complete the same task in JSP technology. One is a conventional JSP syntax, one is a corresponding XML syntax. Although both grammar effects, you'd better use XML syntax. There are two grammar because JSP syntax can be compatible with previous code, while J2EE uses XML as the core of its exchange of data, so it provides an XML syntax. With the development of J2EE, XML's role will get more and more, so I suggest you use XML syntax. Studying Sun Provided J2EE Blueprints: Sun Enterprise BluePrints provides a large number of guiding principles, design patterns and good examples (pet shop, PET Store). You can study these content, which improves your design and development level.

Integrated Servlet and JSP

The JSP technical specification gives two ways to use JSP development web applications. These two ways can be summarized as model one and model. The main differences between these two models are different from their processing of services. Model One, as shown in the figure below, called JSP JavaBeans model. In this model, the JSP page responds to the request and returns the processing result to the customer, and all the data is processed by javabean, and the JSP implementation page performance.

Figure 2: JSP model one

As can be seen from the above figure, the model has also achieved page performance and business logic phase separation. However, in this way, you should use a large number of Java code in the JSP page. This situation will become very bad when the business logic needs to be processed. A large number of embedded code makes the entire page program are abnormal. This is a nightmare for a web developer designed for front-end interface. Therefore, the model can not meet the needs of large applications, but for small applications, because the model is simple, it is easy to involve many elements, so that the need for small applications can be met, so in simple applications, model one can be considered.

Model II, as shown in the figure below, called JSP Servlet JavaBeans model. This model combines JSP and Servlet technology to make full use of the original advantages of JSP and Servlet. This model uses JSP technology to express the page, using servlet technology to complete a large amount of transaction, use bean to store data. Servlets are used to handle the requested transactions, act as a controller's role and is responsible for sending a request to the customer. It creates the bean and objects required by JSP, and then decides which JSP page to the customer according to the behavior of the user request.

Figure 3: JSP model two

From the development point of view, model II has clearer page performance, clearly developing roles, and makes full use of web designers and Java developers in the development team. These advantages are particularly prominent in large projects, and web designers can give full play to their own art and design to fully express page. The programming staff can give full play to their business logic to process thinking and implement business processing in the project.

In addition, from the design structure, this model fully reflects the design architecture of the model view controller (MVC). In fact, many current development frameworks are based on this model, fully implementing the MVC, such as the Apache Struts framework and JavaServer Faces framework (About JavaServer Faces framework, I translated an introductory article "Development Web with JavaServer Faces Application, you can refer to it; for Struts, you can refer to the Apache website).

other:

We summarize some principles for Web development with JSP / servlet technology. By implementing these principles, we can develop a well-used web application. Of course, the summary of us is still far less, hoping to get greater improvement in the future work and learning. If you want to learn more about the technique mentioned in detail, you can refer to:

1, "Servlet and JSP Authoritudes", its website is http://www.moreservlets.com/, there is another book of the author "Core Servlet and JSP" PDF version for free download. This book details the technology of servlet, JSP, TAG tag, JSTL, MVC, and Servlet container models, is a comparable book about the use of Java development web applications. Its defect is that in order to be compatible with past programs, it uses JSP1.1 syntax in many places, of course, this is not impact on readers who are familiar with JSP history.

2, "Java2 Web Development Authentication Learning Guide" This is a certified book, but it is also in detail in detail, the servlet model, the servlet container model, Java Authorization and Authentication Policy (JAAS), JSP, and design mode. You can also look at it.

3, "Design mode: can be used for object-oriented software" This is a classic book of design mode, and it will not be said here.

4, "J2EE Core Model" introduces 15 J2EE design patterns, which is very helpful to how to design a good application, but he has some techniques used by some models.

5, "Practical J2EE Design Mode Programming Guide" This is a new book, which has 15 J2EE design patterns in "J2EE core mode", but also involves 17 of the THESERVERSIDE.COM mode warehouse (reader can access the mode warehouse) 17 J2EE design pattern.

6. For Struts, please refer to the Apache Struts website.

7. For JavaServer Faces, please refer to Sun's JavaServer Faces website.

Please refer to the rest:

Http://www.9cbs.net/develop/read_article.asp?id=18787 Servlets and JSP Development Principles (on)

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

New Post(0)