JS Javaservertm pages (JSP) technology provides us with a simple way to build dynamic Web pages, and also simplifies the work of constructing web programs. This article gives a comprehensive introduction to JSP technology from a developer's perspective (and provides some JSP instance programs).
Objective
JavaServer pages technology makes it easy for web developers and designers to create and maintain dynamic Web pages, especially current business systems. As part of the JAVATM technology, JSP can quickly develop web-based, platform-independent applications. JSP separates the user interface from the system content so that the designer can change the layout of the whole page without changing the underlying dynamic content.
So what's JavaServer page? Simply put, a JSP Web page contains executable application code that can generate dynamic content in an HTML Web page. This application may contain JAVABEANTM,JDBCTM objects, Enterprise Java Beantm (EJB) and remote Method invocation (RMI) objects, all of which can be easily accessed from JSP Web pages. For example, a JSP Web page can contain static text and images displayed by HTML code, or you can call a JDBC object to access the database, and when the page is displayed to the user interface, it will contain static HTML content and find the appropriate dynamic information from the database.
In JSP pages, separating the user interface from the application allows you to consider a very convenient authorization task between the Web designer and the developer. It also allows developers to build flexible code that makes it easy to update and reuse. Because JSP pages can be compiled automatically as needed, web designers can change the presentation code without recompiling the application logic. This also makes JSP a more flexible way to generate dynamic Web content than the Java servlet (which is an extension of the Javasever pages feature).
JSP and Servlet
If you've already used the Java servlet, you'll know that the servlet allows you to build dynamically generated Web pages that contain data from Java objects on the server side. But you also need to know that the servlet generates Web pages by embedding HTML tags and presentation code in Java classes. This means that changing the presentation code requires modifying and recompiling the servlet source file. Because designing an HTML page may not be the same person as the developer who writes the servlet code, updating a servlet-based Web application becomes a tricky business.
Enter JavaServer page is an extension of the Servlet API. In fact, JSP pages can be used before they are translated into a servlet, so they also have all the advantages of a servlet, including access to the Java API. Because JSPs are embedded in the servlet's general presentation code about the application, they can be viewed as a "thorough" servlet.
JSP Web pages provide a high-level approach to building a servlet, and it brings other benefits. Even if you've written a servlet for your Web application, there are still many advantages to using JSP:
JSP pages can be easily combined with static templates, including HTML or XML fragments, and code that generates dynamic content.
JSP pages can be dynamically compiled into a servlet when requested, so the designer of the Web page can easily update the presentation code. JSP pages can also be precompiled if needed.
In order to invoke the JavaBean component, the JSP tag can completely manage these components and avoid the Web designer complicating the application.
Developers can provide a customized JSP tag library.
Web designers can change and edit the fixed template portions of a Web page without affecting the application. Similarly, developers do not need to edit the page one at a while and only make reasonable changes to the component.
Typically, jsps allow developers to distribute functional applications to many web designers. These designers also don't have to know the Java programming language or any servlet code, so they can focus on writing HTML code, and programmers can focus on building objects and applications.
Create a JSP page
In rough view, JSP pages are very similar to HTML (or XML) pages--all contain text encapsulated in labels (defined between "<>"). When HTML tags are processed by the user's browser to display Web pages, JSP tags can generate dynamic content through the processing of Web servers. These JSP tags can define individual actions, such as using a method to invoke JavaBean, or include standard Java blocks of code (known as Scriptlet) that can be executed when a Web page is accessed.
To see how they work, here's a simple example of a JSP page that contains static HTML content and dynamic Data generated from JavaBean. When a user accesses this page, it can display the current date and increase the corresponding greeting (such as "Good Morning" and "afternoon") according to the current time. For simplicity, the JSP label is displayed in the BLOCK:
<%! int time = Calendar.getinstance (). get (CALENDAR.AM_PM); %>
<%
if (time = = calendar.am) {
%>
Good morning
<%
}
else {
%>
Good afternoon
<%
}
%>
<%@ include file= "copyright.html"%>
</HTML>
Is the JSP example above simple? We used several different components in the JSP Web page. These components include the following:
The JSP action (or JSP tag) uses a variety of functions and extends the functionality of the JSP. The JSP action uses an XML-like syntax and is used to process JavaBean components. In the above example, the Jsp:usebean action Initializes a javabean that JavaBean will be used in the back section of the page:
<jsp:usebean id== "Clock" class== "Jspcalendar"/>
If you need to get and set the properties of this bean in a Web page, another JSP action will be given, using the following syntax:
When a Web page is compiled into a servlet, directive is processed by the JSP engine. Directive is used to set page-level directives, insert data from external files, and develop custom tag libraries. The directive is defined between <%@ and%>. In the above example, Directive defines the language in which the page is used (Java), in order to associate Java classes with the need to embed code and specify an HTML file content that will be inserted at the bottom of the page:
A declaration is similar to a variable declaration in Java, and it also defines a variable for subsequent expressions or scriptlet needs. The declaration is defined in the <%! and%>. In the example above, "int" declares and gives an appropriate value for the current time (AM or PM):
<%! int time = Calendar.getinstance (). get (CALENDAR.AM_PM); %>
An expression can be a variable or constant, inserted in the data returned by the Web server and defined by lt;%= and%>. In the example above, the expression invokes the JavaBean component and inserts the resulting data into the page:
<%= Clock.getdayofmonth ()%>
<%= clock.getyear ()%>
Scriptlet will be embedded in the JSP page as a block of Java code. The Scriptlet code is inserted into the servlet generated by the page by one by one, which is defined between <% and%>. The scriptlet in the example above can produce the appropriate time and greeting according to the current user:
<%
if (time = = calendar.am) {
%>
Good morning
<%
}
else {
%>
Good afternoon
<%
}
%>
Annotation section and HTML ⑹ tow Hey tired bi-shi-cong-ne baking 騄 The SP engine removes the explanation for the annotation. This means that the JSP annotation does not return to the user's browser. Unlike HTML annotations, which are defined between <!-and--> tags, jsp annotations are defined between <%--and--%>. For example:
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.