Using Java to develop Web applications (ii) server-side implementation

Source: Internet
Author: User
Tags html page html tags

On the server side, if implemented in the Java language, it is JavaServer Pages (JSPs) and servlets that deal directly with browsers. Behind them are the data Beans, DB Beans, and commands that are implemented through Java.

1. Java Server Pages (JSPs)

JavaServer pages (JSPs) is another extension of HTML that allows you to insert dynamic information into a page with special tags. If you can add a Java snippet using the <% and%> tags, write the value of the expression to the page with the <%= expression%>, and use the <jsp:bean> tag to refer to Java in a range (request, session, or context) Bean.

In addition, the JSP standard tag extension mechanism allows developers to write their own tags and the corresponding implementation methods. In this way, some business logic can be encapsulated into a JSP label, so that fewer Java code in the JSP file, more like an HTML file.

When a browser requests a JSP file from the server, the JSP file is first compiled into a servlet by the Web application server and executed, and the resulting result is passed to the browser as an HTML file. As long as the JSP file to add some control, it can easily achieve the dynamic display of data. Later, if there is another request for this JSP file, if the file is not modified, it will not be compiled, but directly execute the compiled servlet. However, these processes on the Web application server require time and resources, which will inevitably increase the load on the Web application server and the corresponding extended response time. In particular, the entire system has just started or a large number of JSP files made changes. For specific details about JSP calls and lifecycles, see the specification article for the Sun's JSP.

The function of JSP is quite powerful, some people say it can even complete almost all functions. There is no mistake, but when we take into account the reusability of the code and the ease of implementation, I am afraid that all of the functionality through the JSP file is not a good choice. More, we still use JSP file as a response, to dynamically display the data in a state. Another feature of JSP is that it is very much like an HTML file and is made up of a number of tags, so we can also edit it using WYSIWYG editors such as Pagedesinger in WebSphere Studio. Moreover, it is more conducive to collaboration among different developers. JSP files are like a template, waiting for different data to fill, and the implementation of this dynamic mechanism is those embedded HTML tags JSP tags. Based on this idea, the editor of a Web page can first design a rough template (the static part of the page) and then add the dynamic control code to the template by the developer. When it's all done, you can use some kind of debugging tool from JSP to debug. The debugging of JSP file is divided into two stages, compile stage and run state stage. It should be said that the JSP file in the compile phase of debugging is more difficult, because any grammatical errors will lead to the JSP file compilation does not pass, but there is no information to explain exactly what is wrong. Sometimes it takes a long time for a bracket to be missing. Once the compiler passes, there are ways to solve the problem of the running state. I think the best run-time debugging tool is to count the JSP execution monitor in VisualAge for Java's WebSphere Test environment, which can step through the code snippets in the JSP file, making it easy to locate and modify errors.

2. Servlets

The servlet is no longer a strange noun for most people. It is often used as a substitute for CGI in the architecture of the Web site to collect data from HTTP requests and perform some action on that data. In general, we can also use the chained servlet and filtered serlvet to simplify and modularize the servlet. Although it can also be used to generate HTTP responses dynamically from the output stream as an HTML page to the browser, but this practice will lead to the servlet program is very long, and not very adaptable, so we should still try to give the display of work to JSP to do. Compared to Perl, Shell script and other CGI, the servlet has the advantage that it resides only once, can handle multiple HTTP requests, and other CGI programs create a process for each request. As a result, the servlet can save a certain amount of resource consumption.

It is easy to conclude that, like the JSP, the servlet faces the same problem, which is that it takes time to load. When a Web application is started, the first call to a servlet is often slow, and later calls are much better. This issue is determined by the way the servlet is invoked and the lifecycle in the engine (see Sun's servlet specification article for specific content). If a site is highly valued for Web application performance, it may be useful to write a simple program that simulates HTTP requests for all servlet and JSP files to initialize the entire Web application so that it loads all the functionality before it is actually applied.

The servlet is also a Java program, so it inherits all the features of the Java language. If all the servlet of a Web application has a common function, such as a connection to a back-end database, we can write a generic servlet to accomplish these functions, while the other servlet inherits from the common servlet. Of course, this is just one example of the advantages of a servlet, and if you look at it carefully, you can find many. Also, there are many Java Ides, such as IBM's VisualAge for Java, Borland JBuilder, and Semantic visualcafe, which can be used to develop a servlet. It is relatively easy to debug.

3. Java Commands

The Java command is another Java program that is called by the servlet to perform a relatively independent function, such as inserting, modifying, or deleting a data. A transaction often involves multiple functions. We take the transfer to say that this transaction includes at least two operations: deducting money from one account and adding money to another account. Each command can complete a function by using different combinations of command to complete all transactions. command is less restrictive than a servlet and is closer to a typical Java program. Similarly, it can be developed and debugged with the development tools mentioned above. The difference, however, is that because it needs to be invoked by a servlet, the servlet above must be completed at the same time if you want to debug it. Alternatively, create another Java application to construct the parameters passed through the servlet, and then call it.

4. Data Bean

The data Bean, as its name suggests, is dealing with it. More strictly, it is a kind of encapsulation of data. Although there are beans, but unlike JavaBeans, it does not have the JavaBeans event handling. It can be said that the data bean is the shortest part of the entire programming model, because it is a get and set method that operates on these data fields in addition to the data field. The data bean is actually a mapping of the data structure. Generally, if the database schema of a Web application is determined, the number and interface of the data bean are basically determined. The development of the data bean, however, is much simpler. In many Java development environments, a data bean can be generated by wizard, and what the developer needs to do is simply tell wizard how many attributes the data bean has, and what type of each attribute. In fact, if you write the data bean manually, it is not a laborious thing.

5. DB Object

On the server side, the last element we want to talk about is DB Object. Its purpose is to construct the SQL statements required for the operation of the database and to get the result set after execution. It is needed because each time we operate on the database differently, such as lookup, there are many situations that can arise because of different query conditions. This requires different SQL statements to be constructed according to the circumstances. Only in this way can our programs achieve higher utilization and reduce the amount of development effort.

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.