Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

JavaServer Pages Technology

JavaServer Pages Technology (page 649) makes all the dynamic capabilities of Java Servlet technology available to the Web application developer but provides a more natural approach to creating static content. The main features of JSP technology are

A JSP page is a document that contains two types of text: static template data, which can be expressed in any text-based format, such as HTML, SVG, WML, and XML, and JSP elements, which construct dynamic content. A JSP page is translated into a servlet and compiled the first time a request is routed to it. For example, here is the JSP version of the application introduced in the previous section:

// greeting.jsp
<html>
<head><title>Hello</title></head>
<body bgcolor="white">
<img src="duke.waving.gif"> 
<h2>My name is Duke. What is yours?</h2> 
<form method="get">
<input type="text" name="username" size="25">
<p></p>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
<jsp:useBean id="userNameBean" class="hello.UserNameBean" 
  scope="request"/>
<jsp:setProperty name="userNameBean" property="name" 
  value="<%=request.getParameter('username')%>" />
<%
if ( userNameBean.getName() != null && 
    userNameBean.getName().length() > 0 ) {
%>
  <%@include file="response.jsp" %>
<%
}
%>
</body>
</html>

// response.jsp
<jsp:useBean id="userNameBean" class="hello.UserNameBean" 
  scope="request"/>
<h2><font color="red">Hello, ${userNameBean.name}!</font></h2> 

The username request parameter is used to set the name property of the JavaBeans component UserNameBean. Java scripting expressions are used to validate the property value and conditionally include the response if the property is valid.

Early versions of JSP technology placed an emphasis on generating dynamic content by using Java-based scripts (see Chapter 19). The latest version of JSP technology down plays this approach in favor of encapsulating such functions in custom tags (see Chapter 18). The next two sections describe two important standard tag libraries which minimize the need to use scripting in JSP pages.

In summary, the strengths of JSP technology are:

JSP pages typically play the role of the View in an MVC-based Web application and the Model objects are JavaBeans components. Usually, in MVC applications, the Controller creates most of the Model objects.

Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.