Oracle JavaServer Pages Developer's Guide and Reference
Release 8.1.7

Part Number A83726-01





Go to previous page Go to beginning of chapter Go to next page

Introduction to JavaServer Pages

JavaServer Pages(TM) is a technology specified by Sun Microsystems as a convenient way of generating dynamic content in pages that are output by a Web application (an application running on a Web server).

This technology, which is closely coupled with Java servlet technology, allows you to include Java code snippets and calls to external Java components within the HTML code (or other markup code, such as XML) of your Web pages. JavaServer Pages (JSP) technology works nicely as a front-end for business logic and dynamic functionality in JavaBeans and Enterprise JavaBeans (EJBs).

JSP code is distinct from other Web scripting code, such as JavaScript, in a Web page. Anything that you can include in a normal HTML page can be included in a JSP page as well.

In a typical scenario for a database application, a JSP page will call a component such as a JavaBean or Enterprise JavaBean, and the bean will directly or indirectly access the database, generally through JDBC or perhaps SQLJ.

A JSP page is translated into a Java servlet before being executed (typically on demand, but sometimes in advance), and it processes HTTP requests and generates responses similarly to any other servlet. JSP technology offers a more convenient way to code the servlet.

Furthermore, JSP pages are fully interoperable with servlets--JSP pages can include output from a servlet or forward to a servlet, and servlets can include output from a JSP page or forward to a JSP page.

What a JSP Page Looks Like

Here is an example of a simple JSP page. (For an explanation of JSP syntax elements used here, see "Overview of JSP Syntax Elements".)

<HEAD><TITLE>The Welcome User JSP</TITLE></HEAD>
<% String user=request.getParameter("user"); %>
<H3>Welcome <%= (user==null) ? "" : user %>!</H3>
<P><B> Today is <%= new java.util.Date() %>. Have a nice day! :-)</B></P>
<B>Enter name:</B>
<INPUT TYPE="text" NAME="user" SIZE=15>
<INPUT TYPE="submit" VALUE="Submit name">

In a JSP page, Java elements are set off by tags such as <% and %>, as in the preceding example. In this example, Java snippets get the user name from an HTTP request object, print the user name, and get the current date.

This JSP page will produce the following output if the user inputs the name "Amy":

Convenience of JSP Coding versus Servlet Coding

Combining Java code and Java calls into an HTML page is more convenient than using straight Java code in a servlet. JSP syntax gives you a shortcut for coding dynamic Web pages, typically requiring much less code than Java servlet syntax. Following is an example contrasting servlet code and JSP code.

Servlet Code

import javax.servlet.*;
import javax.servlet.http.*;

public class Hello extends HttpServlet
   public void doGet(HttpServletRequest rq, HttpServletResponse rsp)
      try {
         PrintWriter out = rsp.getWriter();
         out.println("<P>Today is "+new java.util.Date()+".</P>");
      } catch (IOException ioe)
        // (error processing) 

(See "The Servlet Interface" for some background information about the standard HttpServlet abstract class, HttpServletRequest interface, and HttpServletResponse interface.)

JSP Code

<P>Today is <%= new java.util.Date() %>.</P>

Note how much simpler JSP syntax is. Among other things, it saves Java overhead such as package imports and try...catch blocks.

Additionally, the JSP translator automatically handles a significant amount of servlet coding overhead for you in the .java file that it outputs, such as directly or indirectly implementing the standard javax.servlet.jsp.HttpJspPage interface (see "Standard JSP Interfaces and Methods") and adding code to acquire an HTTP session.

Also note that because the HTML of a JSP page is not embedded within Java print statements as is the case in servlet code, you can use HTML authoring tools to create JSP pages.

Separation of Business Logic from Page Presentation--Calling JavaBeans

JSP technology allows separating the development efforts between the HTML code that determines static page presentation, and the Java code that processes business logic and presents dynamic content. It therefore becomes much easier to split maintenance responsibilities between presentation and layout specialists who may be proficient in HTML but not Java, and code specialists who may be proficient in Java but not HTML.

In a typical JSP page, most Java code and business logic will not be within snippets embedded in the JSP page--instead, it will be in JavaBeans or Enterprise JavaBeans that are invoked from the JSP page.

JSP technology offers the following syntax for defining and creating an instance of a JavaBeans class:

<jsp:useBean id="pageBean" class="mybeans.NameBean" scope="page" />

This example creates an instance, pageBean, of the mybeans.NameBean class (the scope parameter will be explained later in this chapter).

Later in the page, you can use this bean instance, as in the following example:

Hello <%= pageBean.getNewName() %> !

(This prints "Hello Julie !", for example, if the name "Julie" is in the newName attribute of pageBean, which might occur through user input.)

The separation of business logic from page presentation allows convenient division of responsibilities between the Java expert who is responsible for the business logic and dynamic content--this developer owns and maintains the code for the NameBean class--and the HTML expert who is responsible for the static presentation and layout of the Web page that the application user sees--this developer owns and maintains the code in the .jsp file for this JSP page.

Tags used with JavaBeans--useBean to declare the JavaBean instance and getProperty and setProperty to access bean properties--are further discussed in "JSP Actions and the <jsp: > Tag Set".

JSP Pages and Alternative Markup Languages

JavaServer Pages technology is typically used for dynamic HTML output, but the Sun Microsystems JavaServer Pages Specification, Version 1.1 also supports additional types of structured, text-based document output. A JSP translator does not process text outside of JSP elements, so any text that is appropriate for Web pages in general is typically appropriate for a JSP page as well.

A JSP page takes information from an HTTP request and accesses information from a data server (such as through a SQL database query). It combines and processes this information and incorporates it as appropriate into an HTTP response with dynamic content. The content can be formatted as HTML, DHTML, XHTML, or XML, for example.

For information about XML support, see "OracleJSP Support for XML and XSL".

Go to previous page
Go to beginning of chapter
Go to next page
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.