• No results found

Displaying live web pages with servlets using InternetBeans Express

In document Web Application Developer s Guide (Page 85-90)

Most servlets should use an IxPageProducer component. This enables the servlet to generate the entire response from a pre-designed web page, inserting dynamic data as spans of text or in controls in a form on that page. This has some advantages:

• You know what the response will look like. The page can contain dummy data, which will be replaced.

• You can change that look by changing the page, without having to touch the code.

IxPassword Represents a password field.

XHTML: <input type="password" />

IxPushButton Represents a client-side button.

XHTML: <input type="button" />

IxRadioButton Represents a radio button.

XHTML: <input type="radio" />

IxSubmitButton Represents a form submit button.

XHTML: <input type="submit" />

If the button that matches this component was the button

that submitted the form, the IxSubmitButton’s

submitPerformed() event fires.

IxTextArea Represents a text area.

XHTML: <textarea>

IxTextField Represents an input field.

XHTML: <input type="text" />

Table 7.1 InternetBeans Express classes (continued)

U s i n g I n t e r n e t B e a n s E x p r e s s w i t h s e r v l e t s

For example, when using InternetBeans Express with a servlet, you can open the servlet in the designer. A Database and QueryDataSet from the DataExpress tab of the palette can provide the data for the servlet. You can add an IxPageProducer from the InternetBeans tab of the palette. Set the IxPageProducer’shtmlFile property to the file name of the pre-designed web page. When the servlet is run, the internal HtmlParser is invoked by the IxPageProducer to locate all replaceable HTML controls.

The simplest way to replace HTML controls with controls containing dynamically generated data is to use IxControls. You should add one IxControl for each HTML control on the template page which will contain data. Set each IxControl’spageProducer property to the IxPageProducer. Set the IxControl’scontrolName property to match the name attribute of the appropriate HTML control. Setting the dataSet and columnName properties of the IxControl completes the data linkage.

The IxPageProducer.servletGet() method is the one you will normally call to generate the page for display. This method should be called within the servlet’s doGet() method. The body of a servlet’s doGet() method can often be as simple as:

ixPageProducer1.servletGet(this, request, response);

This single call sets the content type of the response and renders the dynamic page into the output stream writer from the response. Note that the default content type of the response is HTML.

Internally, the IxPageProducer.render() method generates the dynamic version of the page, replacing the controls that have an IxControl assigned to them by asking the component to render, which generates equivalent HTML with the data value filled in from the current row in the dataset. You could call the render() method yourself, but it is simpler to call the servletGet() method.

Some situations where you wouldn’t use an IxPageProducer in a servlet include:

• When you don’t need the database session management and posting features of the IxPageProducer and simply want the page template engine, you can use the PageProducer instead.

• When you’re using specific individual components to render HTML. For example, you can create an IxComboBox containing a list of countries, and use it in a servlet with hand-coded HTML.

Remember that when using InternetBeans in a servlet, usually you should use an IxPageProducer. When you are using IxControls, you must use an IxPageProducer.

U s i n g I n t e r n e t B e a n s E x p r e s s 7-5

U s i n g I n t e r n e t B e a n s E x p r e s s w i t h s e r v l e t s

Posting data with servlets using InternetBeans Express

Processing an HTTP POST is simple with the IxPageProducer.servletPost() method:

ixPageProducer1.servletPost(this, request, response);

This method should be called within the servlet’s doPost() method, along with any other code that should be executed during the post operation. At design-time, you should add an IxSubmitButton for each Submit button on the form. Add a submitPerformed() listener for each of the

IxSubmitButtons. The listener should call code that is to be executed when the button is pressed. For example, a Next button should do

dataSet.next(), and a Previous button should do dataSet.prior(). At runtime, when the servletPost() method is called it writes the new values from the post into the corresponding InternetBeans components and transmits those values from the client side to the server side. It then fires the appropriate submitPerformed() event for the button that submitted the form. To actually post and save changes to the underlying dataset, you should call the dataset’s post() and saveChanges() methods within the submitPerformed() method. The servlet’s doPost() method can then call doGet() or call IxPageProducer.servletGet() directly to render the new page.

Parsing pages

Unlike XML, which is strict, the HTML parser is lax. In particular, HTML elements (tag) and attribute names are not case-sensitive. However, XHTML is case-sensitive; the standard names are lowercase by definition. To make things faster, HTML element and attribute names are converted to the XHTML-standard lowercase for storage. When searching for a particular attribute, use lowercase.

When InternetBeans Express components are matched with HTML controls in the page, properties set in the InternetBeans Express

component take precedence. When setting properties in the designer, you should think about whether you actually want to override a particular HTML attribute by setting its corresponding property in the component. For example, if the web page contains a textarea of a certain size, you probably don’t want to override that size when that control is dynamically generated.

U s i n g I n t e r n e t B e a n s E x p r e s s w i t h J S P s

Generating tables

A fairly common and complex task is the display of data in a table using a particular format. For example, there may be certain cell groupings and alternating colors for each row.

The web page designer need only provide enough dummy rows to present the look of the table (for alternating colors, that’s two rows). When the replacement table is generated by the IxTable component, that look will be repeated automatically.

You can set cell renderers by class or assign each column its own IxTableCellRenderer to allow customization of the content; for example, negative values can be made red (preferably by setting an appropriate cascading style sheets (CSS) style, not by hard-coding the color red). For a tutorial on using InternetBeans in a servlet, see Chapter 19, “Tutorial: Creating a servlet with InternetBeans Express.”

Using InternetBeans Express with JSPs

The key to using InternetBeans Express with JSPs is in the InternetBeans Express tag library, defined in the file internetbeans.tld. This tag library contains a set of InternetBeans tags that can be used in your JSP file whenever you want to use an InternetBeans component. These tags require very little coding, but when the JSP is processed into a servlet, they result in full-fledged InternetBeans components being inserted into the code.

To use InternetBeans Express in a JSP, you must always have one

important line of code at the beginning of your JSP. It is a taglib directive, which indicates that the tags in the InternetBeans Express tag library will be used in the JSP and specifies a prefix for these tags. The taglib directive for using the InternetBeans tag library looks like this:

<%@ taglib uri="/internetbeans.tld" prefix="ix" %>

If you want to instantiate classes in your scriptlets, and don’t want to type the fully-qualified class name, you can import files or packages into your JSP using a page directive. This page directive can specify that the

com.borland.internetbeans package should be imported into the JSP. The page directive should look something like this:

<%@ page import="com.borland.internetbeans.*,com.borland.dx.dataset.*, com.borland.dx.sql.dataset.*" %>

Remember that directives such as the taglib directive and the page directive must always be the very first lines in your JSP.

JBuilder’s JSP wizard inserts a taglib directive and a page directive for you if you select the internetbeans tag library in the Edit JSP File Details step of

U s i n g I n t e r n e t B e a n s E x p r e s s 7-7

U s i n g I n t e r n e t B e a n s E x p r e s s w i t h J S P s the wizard. The JSP wizard also completes the other necessary steps for setting up your JSP to use InternetBeans Express. These steps are as follows:

1 It adds the InternetBeans Express library to your project.

2 It sets the dependencies for the InternetBeans Express, dbSwing, and DataExpress libraries to Include All for your WebApp. This means the required jar files are copied to the WebApp’s WEB-INF/lib directory when the project is compiled.

3 It adds a tag library mapping between internetbeans.tld and WEB-INF/ lib/internetbeans.jar to the web.xml file.

You need to do these steps yourself if you are setting up your JSP to use InternetBeans Express without using the JSP wizard.

Here’s the JSP wizard with the internetbeans tag library selected:

Here is an example of how an InternetBeans tag looks when used in your JSP: <ix:database id="database1"

driver="com.borland.datastore.jdbc.DataStoreDriver" url="jdbc:borland:dslocal:..\\guestbook\\guestbook.jds" username="user">

</ix:database>

This example uses the database tag. If you were actually using the database tag in a JSP, in most cases you will want to nest other tags within this tag, such as the query tag. This isn’t required, but it makes the JSP code more readable.

Note The ix prefix could be any text. It all depends on what prefix is specified in the taglib directive. The JSP wizard uses the ix prefix for the internetbeans tag library.

For a tutorial on this topic, see Chapter 20, “Tutorial: Creating a JSP with InternetBeans Express.”

U s i n g I n t e r n e t B e a n s E x p r e s s w i t h J S P s

In document Web Application Developer s Guide (Page 85-90)