Servlets and JSP Development Principles (on)

zhaozj2021-02-16  56

Servlets and JSP Development Principles (on)

Servlet and JSP technology are the main techniques for developing server-side applications in Java, which is the standard for developing business applications representing ends. Java developers like to use it for a variety of reasons, one is for developers who have been familiar with Java language, this technology is easy to learn; the second is that Java will bring "one writing, run everywhere", and brought into web applications. Implementation "One writing, everywhere". Moreover, more importantly, if you follow some good design principles, you can separate the representations and content, create high quality, readable, easy to maintain and modify and modify applications. For example, if you embed too much Java code (Scriptle) in the HTML document, it will lead to the development of the application, difficult to read, not easy to reuse, and it will cause difficulties to future maintenance and modification. In fact, in the JSP / Servlet Forum in 9CBS, you can often see some questions, the code is very long, but it is not very clear, and a lot of HTML and Java code are mixed together, people can see a fog. This is the drawback of random development.

If you have basically understand the techniques of JSP and Servlet (it is best to develop some web applications), then we can discuss how to develop some guiding principles for "good" applications. We first make a browse for servlet and JSP technology.

Servlet and JSP Overview

Early dynamic web pages mainly use CGI (CommON Gateway Interface, Public Gateway Interface) technology, you can write CGI programs in different languages, such as VB, C / C or Delphi, etc. Although CGI technology has developed mature and powerful, it has gradually replaced trends due to difficulty in programming difficulties, low efficiency, and complex complex. In all new technologies, JSP / servlets have more efficient, easier programming, stronger, safer, and good portability, thus considered many of the most promising dynamic website technology in the future.

Similar to CGI, servlet supports request / response model. When a customer submits a request to the server, the server gives the request to the servlet, servlet is responsible for processing the request and generates a response, and then gives the server, and then send it to the customer. Unlike CGIs, servlet does not generate new processes, but in the same process with HTTP Server. It reduces the overhead of the server by using thread technology. The process of servlet processing is like this: When receiving a request from the client, call the service method, what type of servlet is determined first in this method, and then calls the corresponding processing method. (Doget / Dopost / Dohead ...) and generate a response.

Don't look so complicated, in fact, the servlet is a Java class. The difference from the general class is that this class runs in a servlet container to provide session management and object lifecycle management. Thus when you use servlet, you can get all the benefits of the Java platform, including security management, using JDBC access to databases, and cross-platform capabilities. Moreover, servlet uses threads, thus developing a higher efficiency web application.

JavaServer Pages (JSP)

JSP technology is a key technique for J2EE, which abstracts servlet at higher levels. It allows conventional static HTML combined with dynamic content, which looks like an HTML web page, but is run as a servlet. There are now many commercial application servers support JSP technology, such as BEA WebLogic, IBM WebSphere, JRUN, and more. Using JSP is easier to use servlet. If you have a web server that supports JSP, there is a JSP file, you can put it on any static HTML file, you don't need to compile, don't pack it, you don't have to make ClassPath settings, you can access normal web pages That access it, the server will automatically help you do other work. JSP working principle

JSP files look like a normal static HTML file, but only contain some Java code. It uses .jsp's suffix to tell the server that this file requires special processing. When we visit a JSP page, this file will first be translated into a JSP engine as a JAVA source file, actually a servlet, compiled, and then processed by the servlet engine like a SERVLET. The servlet engine loads this class to handle requests from customers and return the results to customers, as shown below:

Figure 1: The process of calling the JSP page

When there is another customer to access this page, as long as the file has not changed, the JSP engine calls the already loaded servlet. If you have already modified, you will perform the above process, translate, compile and load it again. In fact, this is the so-called "first person punishment". Because of the first time to perform a series of processes, it will cost some time; after the visit will not be the same.

Development principle

This section we list some development principles and the focus is the JSP page. MVC on how to separate performance and content, because we will talk later because we should involve JSP and Servlet integration.

Do not embed excess Java code in the JSP page: For very simple or test code, there is no problem in placing all Java code directly into the JSP page. However, this method should not be excessive, otherwise a large pile of HTML and Java mixed code is difficult to read and understand. The solution is to write a separate class to perform related calculations. Once this type of test passes, it can be placed in any case where the same calculation is performed. This can promote the multiplexing of the code.

Select the appropriate included mechanism: If each page is lined up and bottom in an app, or there is a navigation bar, then put them in a separate file, then use it in each page. The mechanism adds them to this page:

Include instruction: <% @ include file = "filename"%> or equivalent XML syntax

Include Action:

The include command is an additional file when the JSP page is translated to servlet, and the include action is the output of another file when the request is requested. If the file being included is not often changed, I recommend using the include directive, which is faster. If the included files need to be changed from time to time or if you know the request, you should use the include an include action. If you use the JSP Standard Tag Library (JavaServer Pages Standard Tag Library, then there is also the third included mechanism , which can be used to include local or remote resources. For example:

Do not mix business logic and representation together: Complex applications involve a large number of code, thus separation of business logic and front-end representation is particularly important, which can make any party change will not affect the other party. Therefore, all JSP code should be limited to the representation, but if this is, how do you implement your business logic? This is what JavaBean is doing. JavaBean technology is an independent component model that allows developers to write. After testing through a component, it can be used everywhere, improvement. In JSP technology, JavaBean implements the business logic part, which returns the data to the JSP page, which is responsible for formatting data and outputs the browser to the client by the JSP page. The benefits of using JavaBean components in the JSP page are:

Components that can be multiplexed: Any application can use these components to separate business logic and representations: You can modify data display mode without considering business logic. The results of this can also clearly clarify the division of labor in the work, and web developers can put energy on how to display data, and Java developers pay more attention to business logic.

For JavaBean, you don't have to provide source code, so your code will not be easily obtained by people from the browser page, protect your labor results.

If you use the EJB component in your application, the business logic should be placed in EJB because the EJB model provides lifecycle management, transaction management, and multi-client access domain objects (Entity Beans). You can take a closer look at the example in Enterprise BluePrints, which is doing this.

Using customized tags: Above we have discussed, it is not appropriate to embed all Java code into the JSP page, because web developers do not necessarily understand Java language, more difficult to understand Java syntax. JavaBean can encapsulate a lot of Java code, but use JavaBean in the JSP page to still ask page developers to learn about some Java syntax. The JSP technology contains the function of custom marker. Java developers can generate their own tag libraries so that web designers can use these tags using syntax similar to HTML. Writing and using your customized tag library can maximize business logic and the separation of the representation. The use of custom tag libraries mainly have the following benefits:

You can eliminate the use of scriptlets in the JSP page. Any parameters used by the tag can be incorporated by attribute, so that you can achieve your hopes without using Java code.

It can be simplified. Web designers don't need to learn to use Java syntax, they can use tags similar to HTML syntax.

I don't understand Java web designers can use the tag library to complete tasks that HTML cannot be done separately.

Improve the reuse. The tag library is fully reused, which can save time development and testing. The Scriptlet code can only be "multiplexed" on the "copy paste" level. Simply put, you can use the tag library to complete a very complex task as using the tag library as the HTML building representation. Here is some of the payment of the table marker library:

1. Keep your simpleness: If a tag requires several properties, then divide it as several tags as much as possible.

2. Keep multiplexability: That is more exchanged with the user (web design person), try to develop a tag library that can be highly reused.

3. Don't start with your head: There are already some tag libraries that can be used free of charge, such as Jakarta Taglibs. If you want to use a tag, first see if there is already ready-made available.

Please refer to the rest:

http://www.9cbs.net/develop/read_article.asp?id=18788

SERVLETS and JSP Development Principles (below)

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

New Post(0)