Web application development is usually a feature of JBuilder Enterprise.
JavaServer PagesTM (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 JavaTM family, the JSP technology enables rapid development of web-based applications that are platform independent. JavaServer Pages technology separates the user interface from content generation enabling designers to change the overall page layout without altering the underlying dynamic content.
JavaServer Pages 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 JavaBeansTM 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.
JavaServer Pages technology is an extension of the JavaTM Servlet API. 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 platform-specific consideration or modifications; they are Java application components that are downloaded, on demand, to the part of the system that needs them. Together, JSP technology and servlets provide an attractive alternative to other types of dynamic web scripting/programming that offers platform independence, enhanced performance, separation of logic from display, ease of administration, extensibility into the enterprise and most importantly, ease of use.
For links to Web pages that contain more information on JavaServer Pages technology, see the topic "For more information."
JSPs are very similar to ASPs (Active Server Pages) on the Microsoft platform. The main differences between JSPs and ASPs are that the objects being manipulated by the JSP are JavaBeans. Objects being manipulated by the ASP are COM objects, which ties ASPs completely to the Microsoft platform.
JBuilder provides a complete development system for JSPs, including a JSP wizard for creating a new JSP, CodeInsight for JSP-specific tags, debugging within the JSP file, and testing and running the JSP on the Tomcat servlet engine from within the JBuilder development environment.
In this chapter, a JSP is considered to be:
.java
file. The .java
file contains one or more JavaBeans that are used by the JSP file.
.jsp
; this signals to the web server that the JSP technology-enabled engine will process elements on this page.
The JSP specification 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 object. These tags 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.
In the tutorial that follows, a simple example of a JSP tag is used. When the JSP file contains the line
<BR> This page has been hit <%= myBean.count() %> times. <BR>
The HTML that appears in the browser would be
This page has been hit 123 times.
See "For more information" at the end of this chapter for links to web sites that provide more information on developing JavaServer Pages.
The JSP wizard is a good starting point for developing JSPs. It will not generate a complete application, but it will take care of all the tedious details required to get your application up and running. The tutorial that follows shows how to modify the generated JSP to complete your application.
Use the JSP wizard to make the following choices regarding your JSP:
useBean
tag will be placed in the JSP file for each bean selected.
This topic walks you through developing a JSP using JBuilder's JSP wizard. This JSP takes a text input, displays the text as output when the Submit button is clicked, and uses a JavaBean to count the number of times the Web page is visited.
For development testing purposes, we use Tomcat 3.1. Tomcat is the reference implementation of the Java Servlet 2.2 and JavaServer Pages 1.1 Specifications. This implementation can be used in the Apache Web Server as well as in other web servers and development tools. For more information about Tomcat, check out http://jakarta.apache.org.
To create the JSP,
The project is created, and the JSP wizard displays.
The JSP is created.
The main class of the project is set to the generated JSP file, and the following files are added to the project:
JspWithCountingBean.jsp
- an HTML file that contains special JSP tags
JspWithCountingBeanBean.java
- a sample JavaBean called by the JSP file
The Tomcat library has been added to this project. When the JSP is run, the Tomcat process tab will display in the Message pane.
At this point, a JSP and a JavaBean that can be used by the JSP have been created. When a browser requests a file with a JSP extension, the Web server will process the JSP and execute any Java expression or JavaBean property, then render the resulting HTML to the browser.
The next step in this tutorial is to define the JavaBean, and then add the appropriate JSP tag to the JSP file to call the JavaBean. For this example, we will create a method to count the number of hits to a Web page, and then write the JSP code to use the JavaBean.
JspWithCountingBeanBean.java
in the Project pane. Modify the source code as follows, entering the code in bold to the existing sample JSP code:
package samplejsp; public class JspWithCountingBeanBean { /**initialize variable here*/ private int myCount=0; private String sample = "Start value"; /**Access sample property*/ public String getSample() { return sample; } /**Access sample property*/ public void setSample(String newValue) { if (newValue!=null) { sample = newValue; } } /**New method for counting number of hits*/ public int count() { return ++myCount; } }
JspWithCountingBean.jsp
in the Project pane, select the Source tab. You can use CodeInsight and JSP source highlighting to help with coding. Modify the generated file as follows, adding the code in bold. JBuilder's CodeInsight will activate to help with the syntax.
<HTML> <HEAD> <jsp:useBean id="JspWithCountingBeanId" scope="session" class="samplejsp.JspWithCountingBeanBean" /> <jsp:setProperty name="JspWithCountingBeanId" property="*" /> <TITLE> JspWithCountingBean </TITLE> </HEAD> <BODY> <H1> JBuilder Generated JSP </H1> <FORM method="post"> <BR>Enter new value: <INPUT NAME="sample"><BR> <BR><BR> <INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit"> <INPUT TYPE="RESET" VALUE="Reset"> <BR> Value of Bean property is: <jsp:getProperty name="JspWithCountingBeanId" property="sample" /> <P>This page has been visited :<%= JspWithCountingBeanId.count() %> times.<BR> </FORM> </BODY> </HTML>
The runtime properties will be set by the JSP wizard. To view the current runtime configuration,
When the properties are set correctly for the project,
The project compiles. Compilation errors are displayed in the Message pane. If there are errors, refer to the topic "Debugging and testing the JSP".
To run the JSP,
If there are no errors, Tomcat, a servlet engine that supports servlets, SHTML files, and JSP files, is started, and two new tabs appear in the content pane. These tabs are the Web View and Web View Source tabs. The Web View tab becomes a Web browser and displays output from the running JSP. The Web View Source tab displays formatted HTML. If successful, the running JSP looks like this:
The Web View page of the content pane displays the JSP. For local testing, the URL points to localhost:8080, which is where Tomcat is running. To test the JSP, enter a value in the text field, then click the Submit button. The value you entered is displayed below the buttons and the page counter is incremented.
The output/log data from Tomcat will appear on a new tab in the Message pane. Output from servlets or beans, as well as HTTP commands and parameter values, are echoed to this pane. Run-time properties of the Web server may be set by selecting Project|Project Properties, and selecting JSP/Servlet on the Run tab. The port number is the port on which the Web server will run. The default is 8080. If port 8080 is in use, by default JBuilder will search for an unused port.
The Web View tab of the content pane displays the JSP file after it has been processed by the JSP engine, in this case the JSP engine is Tomcat. The Web View pane will behave differently than the View pane. In the Web View pane, there may be a delay between when the JSP file is edited, and when the change is shown in the Web View pane. To see the most recent changes to a JSP file, select the reset button, just as you would in any Web browser.
When the JSP is being debugged, press F9 to return the display to the Web View of the JSP node.
JSP's are compiled to servlets. Starting with JBuilder 4, you can debug Java code snippets in the original JSP file, as opposed to debugging the corresponding generated Java servlet.
To debug the JSP, first we set a breakpoint, as follows:
JspWithCountingBean.jsp
, find the line of code: <P>This page has been visited :<%= JspWithCountingBeanId.count() %> times.<BR>
.
The text of the JSP displays in the browser, and should look like JSP in Web View mode.
Now that the JSP is running, you can change some code in the JSP file, select Reload or Refresh from your browser, and the JSP will attempt to reload. At the designated breakpoint, JBuilder will display with the code at the breakpoint highlighted. When you select Resume, the JSP reloads in the browser window. To see how this works,
For development purposes, JBuilder uses Tomcat to execute the JSP. For deployment onto a production Web server, consult the documentation for that Web server for information on how to deploy JSPs to it.
According to the JSP FAQ at http://www.java.sun.com/products/jsp/faq.html, there are a number of JSP technology implementations for different Web servers. The latest information on officially-announced support can be found at www.java.sun.com.
For more information on developing JavaServer Pages, point your browser to the following Web sites. These Web addresses and links were valid as of this printing. Inprise does not maintain these Web sites and can not be responsible for their content or longevity.
borland.public.jbuilder.servlet
. All JBuilder newsgroups can be accessed from the Borland Web site at http://www.borland.com/newsgroups/#jbuilder
Your Full Name
or visit the JavaSoft Web site for information.