• No results found

Writing a data-aware servlet

In document Web Application Developer s Guide (Page 67-72)

JBuilder web development technologies include the InternetBeans Express API to simplify the creation of data-aware servlets. InternetBeans Express is a set of components that read HTML forms and generate HTML from DataExpress data models, making it easier to create data-aware servlets and JSPs. The components generate HTML and are used with servlets and JSPs to create dynamic content. They feature specific hooks and

optimizations when used in conjunction with DataExpress components but can be used with generic Swing data models as well.

For more information on InternetBeans Express, see Chapter 7, “Using InternetBeans Express.” You can also refer to Chapter 19, “Tutorial: Creating a servlet with InternetBeans Express.”

D e v e l o p i n g J a v a S e r v e r P a g e s 6-1

C h a p t e r

6

Chapter6

Developing JavaServer Pages

Web Development is a feature of JBuilder Enterprise

JavaServer Pages (JSP) technology allows web developers and designers to rapidly develop and easily maintain information-rich, dynamic web pages that leverage existing business systems. As part of the Java family, the JSP technology enables rapid development of web-based applications that are platform independent.

In theory, JavaServer Pages technology separates the user interface from content generation, enabling designers to change the overall page layout without altering the underlying dynamic content. In practice, it takes a little planning and some coding standards to ensure that the HTML is cleanly separated from the Java code in the JSP, since they both reside in the same file. Web designers handling the HTML portion should have a minimal understanding of which tags denote embedded Java code to avoid causing problems when designing the UI.

JSP technology uses XML-like tags and scriptlets written in the Java programming language to encapsulate the logic that generates the content for the page. Additionally, the application logic can reside in server-based resources (such as JavaBeans component architecture) that the page accesses with these tags and scriptlets. Any and all formatting (HTML or XML) tags are passed directly back to the response page. By separating the page logic from its design and display and supporting a reusable

component-based design, JSP technology makes it faster and easier than ever to build web-based applications.

JSP technology is an extension of the Java Servlet API. JSP technology essentially provides a simplified way of writing servlets. Servlets are platform-independent, 100% pure Java server-side modules that fit seamlessly into a web server framework and can be used to extend the capabilities of a web server with minimal overhead, maintenance, and support. Unlike other scripting languages, servlets involve no

D e v e l o p i n g J a v a S e r v e r P a g e s

technology and servlets provide an attractive alternative to other types of dynamic web scripting/programming. JSP technology and servlets offer platform independence, enhanced performance, separation of logic from display, ease of administration, extensibility into the enterprise and most importantly, ease of use.

JSPs are very similar to ASPs (Active Server Pages) on the Microsoft platform. The main difference between JSPs and ASPs is that the objects being manipulated by the JSP are JavaBeans, which are platform independent. Objects being manipulated by the ASP are COM objects, which ties ASPs completely to the Microsoft platform.

All that is required for a JSP is a JSP technology-based page. A JSP technology-based page is a page that includes JSP technology-specific tags, declarations, and possibly scriptlets, in combination with static content (HTML or XML). A JSP technology-based page has the extension .jsp; this signals to the web server that the JSP technology-enabled engine will process elements on this page. A JSP can also optionally use one or more JavaBeans in separate .java files.

When a JSP is compiled by the JSP engine on the web server, it gets compiled into a servlet. As a developer, you usually won’t see the code in the generated servlet. This means that when compiling JSPs in the JBuilder IDE, you may see error messages that refer directly to code in the

generated servlet, and only indirectly to the JSP code. Keep in mind that if you get error messages when compiling your JSP, they could refer to lines of code in the generated servlet. It’s easier to determine the problem in your JSP if you have an understanding of how JSPs get translated into servlets. To achieve this, you need to understand the JSP API.

When JBuilder compiles the JSP, it translates the locations in the generated servlet back into the locations in the originating JSP. This means that clicking on an error in the structure pane or message view takes you to the right place in the JSP. When the web server compiles the JSP, however, it does not do this translation. When you see a stack trace in the browser or the Web View or a stack trace in the web server’s output, the file name and the line numbers are not translated.

Chapter 18, “Tutorial: Creating a JSP using the JSP wizard,” shows you how to create a JSP using the JSP wizard as a starting point.

For links to web pages that contain more information on JavaServer Pages technology, see “Additional JSP resources” on page 6-13.

D e v e l o p i n g J a v a S e r v e r P a g e s 6-3

J S P t a g s

JSP tags

A JSP usually includes a number of specialized tags which contain Java code or Java code fragments. Here is a list of a few of the most important JSP tags:

The JSP specification also includes standard tags for bean use and manipulation. The useBean tag creates an instance of a specific JavaBeans class. If the instance already exists, it is retrieved. Otherwise, it is created. The setProperty and getProperty tags let you manipulate properties of the given bean. These tags and others are described in more detail in the JSP specification and user guide, which can be found at

http://java.sun.com/products/jsp/techinfo.html.

It’s important to realize that most Java code contained within JSP tags becomes part of the servlet’s service() method when the JSP is compiled into a servlet. This doesn’t include code contained in declaration tags, which become complete method or variable declarations in their own right. The service() method is called whenever the client does a GET or a POST.

Table 6.1 Common JSP tags

tag syntax description

<%code fragment%> Scriptlet tag. Contains a code fragment, which is one or more lines of code that would normally appear within the body of a method in a Java application. No method needs to be declared, because these code fragments become part

of the service() method of the servlet when the JSP is

compiled.

<%!declaration%> Method or variable declaration. When declaring a method in this tag, the complete method must be contained in the tag. Gets compiled into a method or variable declaration in the servlet.

<%--comment--%> Comment. This is a JSP style comment that doesn’t get passed to the client browser. (You could also use HTML comments, but these do get passed to the client browser.)

<%=expression%> Expression. Contains any valid Java expression. The result is displayed at that point on the page.

<%@ page[attributes]%> Page directive. Specifies attributes of the JSP page. Directives like this and the taglib directive should be the first lines in the JSP. One of the most common attributes to specify in the page directive is an import statement.

Example: <%@ page import="com.borland.internetbeans.*" %>

<%@ taglib uri="path to tag library" prefix="tag prefix" %>

Taglib directive. Makes a tag library available for use in the JSP by specifying the location of the tag library and the prefix to use in its associated tags. Directives like this and the page directive should be the first lines in the JSP.

J S P t a g l i b r a r i e s a n d f r a m e w o r k s

In document Web Application Developer s Guide (Page 67-72)