Contents | Previous | Next


5. Working with JSPsTM and ServletsTM

5.1 Create a Project

Start JBuilder. Click File -> New -> Project in the Object Gallery window. Click Project. Click OK. In the Project Wizard | Step 1 of 3 specify a Directory Name and a Project Name. Leave the Template as (Default Project) and Finish.

Note: For this exercise, the project directory is C:\JBuilderSun ONE and Project Name is WebSample.

5.2 Attach Sun ONE Application Server to the Current Project

You can attach Sun ONE Application Server to the current project. From the JBuilder menu bar, click Project -> Project Properties. Choose the Server tab. See Figure 5 for details.

Figure 5: Project Properties window allowing users to select application server of choice

Note: JBuilder provides users with the option of choosing one server for all services in the project or multiple servers providing different services. When choosing Modular Services provided by different servers, the users can configure different services to be run by different servers.

For example: users can select Tomcat for JSP/servlet, Borland Enterprise Server for EJB and another server for Connector.

For simplicity, select Sun ONE Application Server 7 from the combo box and select Single server for all services in the project.

Click OK to choose Sun ONE Server for the current project.

Attach the Sun ONE Application Server to All the Projects

If you decide to use Sun ONE Application Server 7 for all of your projects, you can assign Sun ONE to Default Project Properties. To assign Sun ONE Application Server as the default server

  1. From JBuilder menu bar click the Project -> Default Project Properties -> Server tab.
  2. Select Single server for all services in project and choose Sun ONE Application Server 7 from the combo box containing all the configured servers.
  3. Go to Paths tab under Project -> Default Project Properties, click on Required Libraries tab, click Add and choose Sun ONE Application Server 7 Client library from the pop up Select One or More Libraries list and press Enter. This library is required in order to build the web and enterprise applications in the project using Sun ONE Application Server 7.

From now on, Sun One Application Server 7 will be the default server for all your projects.

5.3 Create a Web Application

JBuilder allows you to create a Web application, which can host applets, Web Start launchers, JSPs and servlets.

From the JBuilder menu bar, click the File -> New -> Web tab in Object Gallery -> Web Application. Enter a name and directory for the Web application. For this exercise, the Web application name is WebSampleApp and directory is also WebSampleApp.

Note: JBuilder can recognize that the target server is Sun ONE Application Server 7, so it automatically creates web.xml and sun-web.xml as the deployment descriptors for this Web application.

5.4 Create a JSP

From the JBuilder menu bar, click on the File -> New -> Web tab in Object Gallery -> JavaServer Page. Enter a JSP name. For this exercise, the JSP name is SampleJSP. Click Next and enter a package name in JSP Wizard | Step 3 of 5. For this exercise, the package name is borland.jbuilder.sunone.demo.web. Click Next and then Finish.

This wizard creates a sample JSP and sample JSP bean in the specified package.

Note: The JSP wizard provides many options to users, like creation of a Sample Bean, creation of an Error Page, entering the package name, creation of a runtime configuration etc.

5.5 Make the Project

To make the project, press Ctrl+Shift+F9 or click the Make icon from the JBuilder toolbar. JBuilder may show some warnings because of the use of deprecated methods. To hide the deprecation, click Project -> Project Properties -> Build. Uncheck Show warnings and Show deprecation.

As a result of make, JBuilder automatically creates the WAR file for the Web archive. The WAR file for this exercise is WebSampleApp.war.

See Figure 6 for details.

Figure 6: JBuilder showing the generated sources, WAR file, and DDs for Sun ONE Application Server

Note: For editing a web.xml the Editor is available where in you can specify the values that go into the web.xml but there is no such Editor available for sun-web.xml. The user needs to manually edit the sun-web.xml.

5.6 Building the Web Application

To build the web application, right-click on the web application node and do a make or rebuild. A war file is created with the same name as the web Application containing the Deployment descriptors (web.xml and sun-web.xml) and JSP.

Note: If you find that the .war file contains all the jar files and other class files present in the project, right- click on the web application and select Properties. Go to the classes tab and check the specified only options for Classes and Resources.

If your web application contains a Servlet or a Bean, include it in your web application by clicking on Add Classes. A pane pops up showing all the class files in the current project. Select the servlet you want to include. Make sure that you have done a make or build on the servlet before including it in the war file. Also go to Dependencies tab and click on each of the libraries and check the "Never include any class or resources" option for each of the required libraries. Do not remove these libraries from the project settings, as these are required in order to build the web application.

5.7 Deploy and Run the JSP on Sun ONE Application Server

To deploy and run the JSP, simply right-click SampleJsp.jsp from the JBuilder Project Pane and click Web Run using SampleJsp. This single click, starts the Sun ONE Application Server 7, deploys WebSampleApp.war to Sun ONE, runs the JSP in Sun ONE, and shows the output content of the JSP in JBuilder AppBrowser.

Figure 7: Sun ONE Server is running, and AppBrowser is displaying the running JSP in Sun ONE Server

Note: This JSP is a simple JSP, created with JBuilder JSP Wizard, which accepts a string in the Text Box and displays it. For more details of the source code see the source code SampleJSP.jsp and SampleJSPBean.java.

5.8 Debug the Application

5.8.1 Local Debugging

If the Sun ONE Application Server 7 is installed on the same machine as JBuilder8, the user can debug the applications at runtime. To debug a web application, assign breakpoints in the source code wherever necessary. To assign a breakpoint, simply open source file and click the mouse on that line and press F5 or go to Run and click Add a Breakpoint. Build the web application again and deploy it on the Default Server Instance.

Note: For this exercise, breakpoints are assigned on the 7th line of Sample JSP and at 11th line of SampleJSPBean.java. For more on debugging see JBuilder Help, type debugging as a keyword in the index of JBuilder help and click on JSP, which is one of the subheadings displayed under debugging.

To debug a JSP, simply right-click SampleJsp.jsp from the JBuilder Project Pane and click Web Debug using "SampleJsp". When you click Debug Project, JBuilder starts the Sun ONE Server in debug mode. Now users can perform debug operations from JBuilder, such as console output, step over, step into, step out, checking thread information, breakpoint information, etc. See Figure 8 for details. Click Resume to go to the next breakpoint in the SampleJSPBean.

Figure 8: JBuilder Debugger stopped at break point in JSP. The JSP is running in the Sun ONE Application Server 7.

5.8.2 Remote Debugging

If the user needs to debug an application on a remote Sun ONE Server Instance, go to the Admin Tool of Sun ONE Application Server 7 and start the Default Server Instance in the debug mode. The user needs to look for the port at which the Sun ONE debugger has started. Find the following Entry in server.xml of Default Server instance.

Note: server.xml is located in config folder in the default server instance folder in that particular domain under the domains directory in <INSTALL_DIR> of Sun ONE Application 7 e.g. if default server instance is server1 in domain1 then server.xml will be located in <INSTALL_DIR>/domains/domain1/server1/config

<java-config java-home="C:/Appservers/Sun/AppServer7/jdk" 
server-classpath="C:/Appservers/Sun/AppServer7/lib/appserv-rt.jar; " env-classpath-ignored="true" 
debug-enabled="true" javac-options="-g" rmic-options="-iiop -poa -alwaysgenerate 
-keepgenerated -g" debug-options="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1186 
-Xnoagent -Djava.compiler=NONE">

Note the value of address Attribute of debug-options. In this example the address is 1186.

Go to Run-> Configurations and edit Run Time Configuration SampleJSP.

Go to the Debug tab. Enable remote debugging and check Attach Option. In the host name text field enter the hostname of the Default Server Instance. In the transport select dt_socket from the Type List and in the address text field add the address specified in the address Attribute in server.xml.

Figure 9: RunTime configurations to attach a debugger to a Sun ONE Server debugee.

Right-click on the web application and select Debug using "SampleJSP". Go to Web Viewer and press Enter in the URL pane.

The debugger is designed to stop at breakpoints in the JavaBean. After the debugger stops at a breakpoint, the user can perform the debug operations like step into, step over, step out, etc.

5.9 Working with Servlets

Create another web application in the same Project SampleWeb. To get help on how to create a web application, refer to Create a Web Application.

Note: For this exercise the name of the other Web application is SampleServlet.

To create a servlet, from the JBuilder menu bar, click the File -> New -> Web tab in Object Gallery -> Servlet. Enter a package name and servlet name in Servlet Wizard | Step 1 of 5. Click Next.

Note: For this exercise the package name entered is borland.jbuilder.sunone.demo.web, the servlet name is ExampleServlet and the web application name is SampleServlet. In Servlet Wizard | Step 2 of 5, enter the methods that you want to use in the servlet. Click Next.

Note: For this exercise, the implement methods options doget() and doPost() are checked.

In Servlet Wizard | Step 3 of 5, you can enter the servlet name and URL pattern. Leave the defaults and click Next.

In Servlet Wizard | Step 4 of 5, you can enter the Servlet Request Parameters. Click Next.

Servlet Wizard | Step 5 of 5 allows you to create a runtime configuration for the servlet. Click the checkbox Create a Runtime Configuration and leave the default configuration name as ExampleServlet and Finish.

Make the project. To get help on how to make a project, refer to Make the Project.

To deploy and run the servlet, stop the Sun ONE Server. Right-click on the ExampleServlet in the Project pane, then click Web Run ->Use ExampleServlet.

See Figure 10 for details.

Note:You can also deploy and run the ExampleServlet by clicking the run icon from the JBuilder toolbar and selecting ExampleServlet.

You don't have to stop Sun ONE Server to deploy and run another application. You can right-click on SampleServlet.war. Click Deploy Options for SampleServlet.war and hit Deploy. After deployment, run the link

http://<hostname:portnumber>/SampleServlet/exampleservlet

from any browser, and the servlet should respond.

Figure 10: A servlet running in Sun ONE Application Server 7 and displaying the results in JBuilder AppBrowser

5.10 Troubleshooting

Error 1: Log File not found. Please check Permissions

Error description: When you run the web application and you get a message in the Message pane saying Could not Read the Server Log File with the log file path.

Reason: Either there is no read permission in order to read the file or the instance created is a new one and log file for it is not created by the Sun ONE Application server or the user is running the application from a remote machine.

Solution: If you are deploying the archive on the local machine and you ge this message, check the permissions of the log file or try running the server again, as this may happen because of the server instance being a new instance and not having created the log file when the user did a Run.


Contents | Previous | Next