JSP介绍
原站著录信息
摘要
The Java 2 Enterprise Edition (J2EE) has taken the once-chaotic task of building an internet presence and transformed it to the point where developers can use Java to efficiently create multitier,server-side applications. Today,the Java Enterprise APIs have ex…
The Java 2 Enterprise Edition (J2EE) has taken the once-chaotic task of building an internet presence and transformed it to the point where developers can use Java to efficiently create multitier,server-side applications. Today,the Java Enterprise APIs have expanded to encompass a number of areas: RMI and CORBA for remote object handling, JDBC for database interaction, JNDI for accessing naming and directory services, Enterprise JavaBeans for creating reusable business components, JMS (Java Messaging Service)for message oriented middleware, JAXP for XML processing, and JTA (Java Transaction API) for performing atomic transactions. In addition, J2EE also supports servlets, an extremely popular Java substitute for CGI scripts. The combination of these technologies allows programmers to create distributed business solutions for a variety of tasks.
In late 1999, Sun Microsystems added a new element to the collection of Enterprise Java tools: Java Server Pages (JSP). Java Server Pages are built on top of Java servlets and are designed to increase the efficiency in which programmers, and even nonprogrammers, can create web content.
What Is Java Server Pages?
Put succinctly, Java Server Pages is a technology for developing web pages that include dynamic content. Unlike a plain HTML page, which contains static content that always remains the same, a JSP page can change its content based on any number of variable items, including the identity of the user, the user's browser type, information provided by the user, and selections made by the user.
A JSP page contains standard markup language elements, such as HTML tags, just like a regular web page. However, a JSP page also contains special JSP elements that allow the server to insert dynamic content in the page. JSP elements can be used for a variety of purposes, such as retrieving information from a database or registering user preferences. When a user asks for a JSP page, the server executes the JSP elements, merges the results with the static parts of the page, and sends the dynamically composed page back to the browser.
JSP defines a number of standard elements that are useful for any web application, such as accessing JavaBeans components, passing control between pages and sharing information between requests, pages, and users. Programmers can also extend the JSP syntax by implementing application-specific elements that perform tasks such as accessing databases and Enterprise JavaBeans, sending email, and generating HTML to present application-specific data. One such set of commonly needed custom elements is defined by a specification related to the JSP specification: the JSP Standard Tag Library (JSTL) specification. The combination of standard elements and custom elements allows for the creation of powerful web applications.
Why Use JSP?
In the early days of the Web, the Common Gateway Interface (CGI) was the only tool for developing dynamic web content. However, CGI is not an efficient solution. For every request that comes in, the web server has to create a new operating-system process, load an interpreter and a script, execute the script, and then tear it all down again. This is very taxing for the server and doesn't scale well when the amount of traffic increases.
Numerous CGI alternatives and enhancements, such as FastCGI, mod_perl from Apache,
NSAPI from Netscape, ISAPI from Microsoft, and Java servlets from Sun Microsystems,
have been created over the years. While these solutions offer better performance and scalability, all these technologies suffer from a common problem: they generate web pages by embedding HTML directly in programming language code. This pushes the creation of dynamic web pages exclusively into the realm of programmers. Java Server Pages, however, changes all that.
JSP tackles the problem from the other direction. Instead of embedding HTML in
programming code, JSP lets you embed special active elements into HTML pages. These
elements look similar to HTML elements, but behind the scenes they are actually
componentized Java programs that the server executes when a user requests the page. Here's a simple JSP page that illustrates this:
<%@ taglib prefix="c" url="http://java.sun.com/jstl/core" %>
<html>
<body bgcolor="white">
<jsp: useBean id="clock" class="java.util.Date" />
<c:choose>
<c:when test="${clock.hours < 12}">
<h1>Good morning! </h1>
</c: when>
<c: when test="${clock.hours < 18}">
<h1>Good day! </h1>
</c: when>
<c: otherwise>
<h1>Good evening! </h1>
</c: otherwise>
</c: choose>
Welcome to our site, open 24 hours a day.
</body>
</html>
This page inserts a different message to the user based on the time of day: "Good morning!" if the local time is before 12 P.M., "Good day!" if between 12 P.M. and 6 P.M., and "Good evening!" otherwise. When a user asks for this page, the JSP-enabled web server executes the logic represented by the highlighted JSP elements and creates an HTML page that is sent back to the user's browser. For example, if the current time is 8:53 P.M., the resulting page sent from the server to the browser looks like this:
<Html>
<body bgcolor="white">
<h1>Good evening! </h1>
Welcome to our site, open 24 hours a day.
</body>
</html>
In addition to the HTML-like JSP elements, a JSP page can also contain Java code embedded in so-called scripting elements. This feature has been part of the JSP specification from the very first version, and it used to be convenient for simple conditional logic. With the introduction of the new JSP Standard Tag Library (JSTL), however, Java code in a page is rarely needed. In addition, embedding too much code in a web page is no better than using HTML elements in a server-side program, and often leads to a web application that is hard to maintain and debug.
Using the Right Person for Each Task
As I alluded to earlier, JSP allows you to separate the markup language code, such as HTML, from the programming language code used to process user input, access a databases and perform other application tasks. One way this separation takes place is through the use of the JSP standard and custom elements; these elements are implemented with programming code and used the same way as page markup elements in regular web pages.
Another way to separate is to combine JSP with other J2EE technologies. For example, Java servlets can handle input processing, Enterprise JavaBeans (EJB) can take care of the application logic, and JSP pages can provide the user interface.
This separation means that with JSP, a typical business can divide its efforts among two groups that excel in their own areas of expertise: a Java web development team with programmers who implement the application logic as servlets, EJB and custom JSP elements, and page authors who craft the specifics of the interface and use the powerful custom elements without having to do any programming.
Integration with Enterprise Java APIs
Finally, because Java Server Pages are built on top of the Java Servlets API, JSP has access to all the powerful Enterprise Java APIs, including:
• JDBC
• Remote Method Invocation (RMI) and OMG CORBA support
• JNDI (Java Naming and Directory Interface)
• Enterprise JavaBeans (EJB)
• JMS (Java Message Service)
• JTA (Java Transaction API)
• JAXP (Java API for XML Processing)
• JavaMail
This means that you can easily integrate Java Server Pages with your existing Java Enterprise
solutions.
Java servlet template engines
A Java servlet template engine is another technology for separating presentation from processing. When servlets became popular, it didn't take long before developers
In late 1999, Sun Microsystems added a new element to the collection of Enterprise Java tools: Java Server Pages (JSP). Java Server Pages are built on top of Java servlets and are designed to increase the efficiency in which programmers, and even nonprogrammers, can create web content.
What Is Java Server Pages?
Put succinctly, Java Server Pages is a technology for developing web pages that include dynamic content. Unlike a plain HTML page, which contains static content that always remains the same, a JSP page can change its content based on any number of variable items, including the identity of the user, the user's browser type, information provided by the user, and selections made by the user.
A JSP page contains standard markup language elements, such as HTML tags, just like a regular web page. However, a JSP page also contains special JSP elements that allow the server to insert dynamic content in the page. JSP elements can be used for a variety of purposes, such as retrieving information from a database or registering user preferences. When a user asks for a JSP page, the server executes the JSP elements, merges the results with the static parts of the page, and sends the dynamically composed page back to the browser.
JSP defines a number of standard elements that are useful for any web application, such as accessing JavaBeans components, passing control between pages and sharing information between requests, pages, and users. Programmers can also extend the JSP syntax by implementing application-specific elements that perform tasks such as accessing databases and Enterprise JavaBeans, sending email, and generating HTML to present application-specific data. One such set of commonly needed custom elements is defined by a specification related to the JSP specification: the JSP Standard Tag Library (JSTL) specification. The combination of standard elements and custom elements allows for the creation of powerful web applications.
Why Use JSP?
In the early days of the Web, the Common Gateway Interface (CGI) was the only tool for developing dynamic web content. However, CGI is not an efficient solution. For every request that comes in, the web server has to create a new operating-system process, load an interpreter and a script, execute the script, and then tear it all down again. This is very taxing for the server and doesn't scale well when the amount of traffic increases.
Numerous CGI alternatives and enhancements, such as FastCGI, mod_perl from Apache,
NSAPI from Netscape, ISAPI from Microsoft, and Java servlets from Sun Microsystems,
have been created over the years. While these solutions offer better performance and scalability, all these technologies suffer from a common problem: they generate web pages by embedding HTML directly in programming language code. This pushes the creation of dynamic web pages exclusively into the realm of programmers. Java Server Pages, however, changes all that.
JSP tackles the problem from the other direction. Instead of embedding HTML in
programming code, JSP lets you embed special active elements into HTML pages. These
elements look similar to HTML elements, but behind the scenes they are actually
componentized Java programs that the server executes when a user requests the page. Here's a simple JSP page that illustrates this:
<%@ taglib prefix="c" url="http://java.sun.com/jstl/core" %>
<html>
<body bgcolor="white">
<jsp: useBean id="clock" class="java.util.Date" />
<c:choose>
<c:when test="${clock.hours < 12}">
<h1>Good morning! </h1>
</c: when>
<c: when test="${clock.hours < 18}">
<h1>Good day! </h1>
</c: when>
<c: otherwise>
<h1>Good evening! </h1>
</c: otherwise>
</c: choose>
Welcome to our site, open 24 hours a day.
</body>
</html>
This page inserts a different message to the user based on the time of day: "Good morning!" if the local time is before 12 P.M., "Good day!" if between 12 P.M. and 6 P.M., and "Good evening!" otherwise. When a user asks for this page, the JSP-enabled web server executes the logic represented by the highlighted JSP elements and creates an HTML page that is sent back to the user's browser. For example, if the current time is 8:53 P.M., the resulting page sent from the server to the browser looks like this:
<Html>
<body bgcolor="white">
<h1>Good evening! </h1>
Welcome to our site, open 24 hours a day.
</body>
</html>
In addition to the HTML-like JSP elements, a JSP page can also contain Java code embedded in so-called scripting elements. This feature has been part of the JSP specification from the very first version, and it used to be convenient for simple conditional logic. With the introduction of the new JSP Standard Tag Library (JSTL), however, Java code in a page is rarely needed. In addition, embedding too much code in a web page is no better than using HTML elements in a server-side program, and often leads to a web application that is hard to maintain and debug.
Using the Right Person for Each Task
As I alluded to earlier, JSP allows you to separate the markup language code, such as HTML, from the programming language code used to process user input, access a databases and perform other application tasks. One way this separation takes place is through the use of the JSP standard and custom elements; these elements are implemented with programming code and used the same way as page markup elements in regular web pages.
Another way to separate is to combine JSP with other J2EE technologies. For example, Java servlets can handle input processing, Enterprise JavaBeans (EJB) can take care of the application logic, and JSP pages can provide the user interface.
This separation means that with JSP, a typical business can divide its efforts among two groups that excel in their own areas of expertise: a Java web development team with programmers who implement the application logic as servlets, EJB and custom JSP elements, and page authors who craft the specifics of the interface and use the powerful custom elements without having to do any programming.
Integration with Enterprise Java APIs
Finally, because Java Server Pages are built on top of the Java Servlets API, JSP has access to all the powerful Enterprise Java APIs, including:
• JDBC
• Remote Method Invocation (RMI) and OMG CORBA support
• JNDI (Java Naming and Directory Interface)
• Enterprise JavaBeans (EJB)
• JMS (Java Message Service)
• JTA (Java Transaction API)
• JAXP (Java API for XML Processing)
• JavaMail
This means that you can easily integrate Java Server Pages with your existing Java Enterprise
solutions.
Java servlet template engines
A Java servlet template engine is another technology for separating presentation from processing. When servlets became popular, it didn't take long before developers
全文与附件
提交申请后,可在会员中心查看原件和附件的查找进度。
