|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
POWERBUILDER LINKS YOU MUST CLICK ON PB & Java
Building JSP Applications with Jaguar CTS Part 4
By: Mike Barlotta
Digg This!
Welcome to JSP Corner, Part 4. We'll be adding some more features to our eMusic application, which we started putting together in Part 3 (PBDJ, Vol. 8, issue 12). We looked at the Logon process and built the Logon.jsp, validateLogon.jsp, and LogonError.htm pages. In Part 4 we complete the Logon process shown in Figure 1 by building our Home.jsp page and all related pages. This page contains the main viewable area, a search form, and a navigation bar that allows the user to move through the application (see Figure 2). The Home.jsp page will use HTML frames to divide the screen into its three respective parts, as this was how the original Sybase application was written. Another alternative is to use HTML tables or dynamic HTML. The Home.jsp page that defines the frames is shown in Listing 1. Nothing too special here if youre familiar with frames. As we can see from this listing, we have three JSP pages that need to be written to populate our frames:
The Search.jsp page uses an HTML form to collect user input and calls a Java servlet to find related CDs in the database. This area could be the topic of a future article, and is similar to both the Logon.jsp and validateLogon.jsp pages (covered in Part 3), as it relies on an HTML form to collect data. The Home.jsp, Search.jsp, and the eMusic.jsp pages could have just as easily been given an .htm extension, as none of them have any JSP code. In hindsight this should be done to avoid unnecessary compilation of these pages by the container. The Navigation.jsp page, however, does have JSP code, including some new JSP tags, and its the main focus of this article.
The Navigation.jsp Page
Instead of forcing a user to log in each time he or she uses the application, use a cookie that stores the user ID. The application could look for this cookie and use it without requiring the user to log in first. Currently it doesnt do this.
The checkLogon.jsp Page
This page uses the session object to see if the user logged in yet. Well cover the session object in the next article, so for now accept that fact that it holds data for a particular user across page requests and well get into the how next time. The validateLogon.jsp page is responsible for updating the session data based on whether the user ID and password are valid. The session data stored by the validateLogon.jsp page is used by the checkLogon.jsp page to determine whether theres a valid user loged into the application. The checkLogon.jsp page declares a Boolean variable isValid, which will hold true if the user is logged in and false otherwise.
The Include Directive
<%@ include file="checkLogon.jsp"> The file attribute of the directive specifies the name of the file to be included and the location of the file using a URL relative to the location of the JSP page containing the directive. If you remember from Part 1 (Vol. 8, issue 9), directives tell the container what to do before the JSP page is compiled. In this case, the include directive tells the container to include the contents of the specified file as part of the original page before its compiled into a servlet. Our Navigation.jsp page will look and act as if the checkLogon.jsp page contents were copied and pasted over the include directive and then the page deployed and compiled. The include directive allows us to reuse the same code or static text in several pages in our application with a single tag. This not only makes code reuse easy but also allows us to keep the shared code in a single file, making it easier to maintain. By including a JSP page with a directive, we have access to the variables that are declared on the included page. For example, the isValid variable declared in checkLogon.jsp can be used in the Navigation.jsp page to determine how to set the NavPage String variable that will be used to display the correct menu:
<% This is possible because the static contents, not the output of the included page (checkLogon.jsp), are placed in the source page (Navigation.jsp) before the source page is compiled. An important consideration when deciding to use the include directive is how often the contents of the included file changes. If the contents change often enough it might not be a good design choice to use the include directive, because the container recompiles a JSP page only if there was a change made to the requested page. Because the source page (Navigation.jsp) is the page thats requested, not the included page, the updated contents of the included file arent detected and therefore not copied into the source page. The only way to get the updated contents from the included page into the source page is to make a change to the source page, forcing the container to recompile it. Another important consideration to keep in mind when using the include directive is that because the contents are copied at compile time, the source page cant change which file is included at runtime. Last, the included file doesnt have to be a JSP page, but remember its only the static contents of the file, not the dynamic output, thats placed into the source file.
Changes to the Include Directive
NavigationVLogon.jsp and NavigationVOK.jsp
The Include Action Tag
<jsp:include page="NavigationVOK.jsp" flush="true"/> The include action tag uses an XML syntax and must be properly ended with the /> instead of the typical > used to close an HTML tag. The page attribute of the tag specifies the name and location of the file to be included. Unlike the include directive, the include action tag is handled at runtime instead of at compile time. Including a page using this action tag involves temporarily transferring control from the source page (Navigation.jsp) to the included page. The included page is executed (if its an executable resource like a JSP or servlet) on the server and its generated output is placed into the source page where the include tag was originally located. The source page regains control and continues processing (see Figure 4). The flush attribute of the tag is required and must be set to true (in JSP 1.1). It tells the container to flush the buffer of its contents before transferring control to the included page. The buffer stores all the generated output and holds it before its sent to the client. This allows the JSP page to add HTTP headers and cookies, as well as discard the generated content before its sent to the client. Since the include tag flushes the buffer of the source page before control is transferred to the included page, any generated content written into the buffer by the source page is committed and sent to the client. From a practical perspective this means that the JSP page cant set any more HTTP headers or add any more cookies, nor can the source page forward the request to another page. These restrictions also apply to the included page. Because the included file is actually run by the container, any changes made to the page are detected by the container and cause the page to be recompiled. This allows changes to be picked up quickly and easily. Using the include action tag allows us to easily add more menu options without making any changes to the Navigation.jsp page. The include action tag allows the Navigation.jsp page to dynamically decide, based on different runtime conditions, which JSP page it should include to display the correct menu. In the Navigation.jsp page a String variable, NavPage, is set to the name of the page that should be included based on the results returned by checkLogon.jsp. The value of the String can be used as the input to the page attribute using the JSP expression. The following is the syntax for this: <jsp:include page="<%= NavPage %>" flush="true" /> Unlike the include directive, the source page cant use variables defined in the included file using the include action tag. Any data that must be shared between the included file and the source page must be handled using the session object (or some other data source accessible by the source page). The include action tag allows data to be passed from the source page to the included page. This is done using the param action tag, which allows data to be sent in name/value pairs. The parameters are added to the original request object of the source page, which is sent to the included page. These new values are accessible only from the included page; theyre not stored in the request object when the included page returns control to the source page. The syntax of the include action tag with the param action tag is shown below:
<jsp:include page="url" flush="true" > The param action tags are part of the body of the include action tag. There can be as many param tags as needed to send the data. Notice that the include action tag no longer ends with the /> delimiter. It ends with the </jsp:include> tag. Using the include action tag is the same as using the RequestDispatcher. A reference to the RequestDispatcher object can be acquired using the getRequestDispatcher method on the request object. Once we have a reference to the RequestDispatcher object, we call the include method that passes the request and the response objects to actually transfer control to the target.
<%
Changes to the Include Action Tag
Wrapping Up
Heres a recap of the include directive:
PBDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING POWERBUILDER / SYBASE NEWS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||