Topics in Java Learning (eight)

Source: Internet
Author: User

Finally officially into the details of the Java EE, the first of course is the servlet and JSP, the previous mentioned that the Java-EE is just a specification and guide, defines a set of interfaces that must be followed, the core concept is the components and containers. Once some people asked the author servlet's class file is where? He thought it was provided by the official Java EE, I gave a simple counter example: a little check Tomcat5.0 inside the Servlet.jar file and JBoss inside Servlet.jar file size, obviously is not the same, at least it has been explained that they do not originate from the same root. In fact, the servlet is implemented by the container according to the interface of Java EE, the way of implementation can be different, as long as all comply with the Java Code and guidelines.

This is just a common misunderstanding, tell us to compile the running servlet, is to rely on the implementation of its container, or even the jar file is not, the compilation can not be done. So what about JSP? Java Server page abbreviation, is for the development of Dynamic Web pages and the birth of the technology, its nature is JSP, after the completion of the container when the launch of a corresponding servlet. We just take advantage of the many new features of JSP, we can focus on the separation of the former backstage, Early JSP to do the front is full of popular, after all, the support of HTML code, which allows the foreground artists can more efficient to complete their work. The JSP then forwards the request to the background servlet, which handles the business logic by the servlet and forwards it back to another JSP, which is displayed in the foreground. This seems to have become a common pattern, when the author of the first study of Java, a lot of time is also writing such code.

Although there are more and more technologies for the foreground, such as Flash, Ajax and so on, many people no longer think that JSP is important. I think the JSP brings not only the design concept of the separation of the front and back, it's another technology to achieve our today's many frameworks, that is tag tag technology. So instead of learning JSP, it's better to tell yourself that you are constantly understanding the meaning and nature of tag tags.

1.Servlet and JSP lifecycle

The servlet is the essence of the JSP, although the container differs in their handling. The servlet has the init () method initialization, the service () method for Web services, the Destroy () method for the destruction, from the birth to the kill is controlled by the container, so these methods unless you want to implement the servlet itself, otherwise it is rarely exposed. It is because very little contact, it is easy to be ignored by the vast number of beginners, I hope you at least remember that the servlet lifecycle method is a callback method. The idea of a callback is simply to inject yourself into another class, call your method by it, and the other class is the Web container, which only recognizes the interface and the interface, and what kind of object it is injected into, regardless of which method is defined in the interface according to the object you want to call it. The initialization, service, and destruction methods of the Servlet object invoked by the container, so called callbacks. This concept of learning other Java technology is quite critical!

So what about JSP? The ability to be a servlet, or something different, is the life cycle:

A a client request arrives at the server->

b to determine whether the first call to-> is to compile a JSP into a servlet

c) No, then judge if this JSP has any changes-> is the words also recompile JSP into a servlet

D) has compiled the other class E required for the most recent version of the servlet load to publish the servlet, that is, the service () method that invokes it

So JSP claims that the first load slow, will soon run. From its life cycle is really not difficult to see this feature, the client's operation will rarely change the source code of the JSP, so it does not need to compile the second time has been able to provide services to clients. Here a little explanation of HTTP stateless, because found that a lot of people misunderstand, HTTP stateless means that every time a page is displayed, the connection with the server is actually disconnected, when there is another submission action, will be again with the server to make connection requests to provide services. Of course, there are now more popular Ajax and server asynchronous through the XML interaction technology, in the field of the foreground to do a huge potential, the author is not a master of Ajax, here can not explain for everyone. The nature of 2.Tag labels

The author said before, JSP itself is the original intention of the Web application before and after the development can be separated from the coupling effectively, but the idea of the contribution is not as it brings the tag technology to the Java EE contribution to the big. Maybe a lot of people have started using tag technology and don't know it. Therefore, we suggest that you learn from the beginning of Java must carefully learn JSP, in fact, the most important thing is to understand the nature of the tag.

HTML tags are familiar to us, there are

To be clear, let me give you a few simple examples to illustrate this:

<jsp:include> where to find the implementation of what class? First of all, this is a JSP class library tag, of course, to go to the JSP class library to find the corresponding class, but also it is provided by the Web container, such as Tomcat should go to install the directory of the Lib folder below the Jsp-api.jar inside find, interested can go to find Ah!

Where is <c:forEach> going to find it? This is the content of the core tag library, recommended by the Jsp2.0 version, such as <c:if>, a broken Java code that corresponds to the ability to do if judgment on the page. Its class file in the Jstl.jar class library, often also need to import with a Standard.jar class library, placed in the specific Web Project Web-inf Lib directory can be used below.

By the way, the directory structure of WEB project is relatively fixed, because the container will follow a fixed path to find the configuration files and resources it needs, which is available in any one of the introductory books of Java, this is not covered here. To understand the nature of tag and to understand how it works, so you go to the Java EE API to find and study this package: Javax.servlet.jsp.tagext. It has some interfaces, and some implementation classes, specialized language development tag, only to write a few different functions of the label, it is really understand the principle of the label. Do not forget the development of their own label to complete the configuration file, the container is only integrated to where to find the JSP tag corresponding class path, the tag library of course to tell the container where to find it.

Why do we use labels when we say so much? Completely in the JSP to a <%%> can write any Java code inside, but the long-term practice found that the page code is unified with the HTML and the same style of markup language to help the art staff to develop the foreground, it does not need to understand Java, As long as the Java programmer to give a list of what tags can be completed what the logic function, he can focus on the art, but also as a further isolation of the former backstage work!

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.