1 / 12

Java Server Pages

Java Server Pages. A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format, such as HTML , SVG , WML , and XML; and JSP elements, which construct dynamic content.

beau-mckay
Download Presentation

Java Server Pages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format, such as HTML, SVG, WML, and XML; and JSP elements, which construct dynamic content. A syntax card and reference for the JSP elements are available at: http://java.sun.com/products/jsp/technical.html#syntax

  2. Your First JSP Page • The Web page in Figure 11-1 is a form that allows you to select a locale and displays the date in a manner appropriate to the locale. • Figure 11-1 Localized Date Form • The source code for this example is in the j2eetutorial/examples/src/web/date directory created when you unzip the tutorial bundle. • The JSP page index.jsp used to create the form appears below; it is a typical mixture of static HTML markup and JSP elements. If you have developed Web pages, you are probably familiar with the HTML document structure statements (<head>, <body>, and so on) and the HTML statements that create a form (<form>) and a menu (<select>).

  3. The source code briefing • The source code for this example is in the j2eetutorial/examples/src/web/date directory created when you unzip the tutorial bundle. The JSP page index.jsp used to create the form appears below; it is a typical mixture of static HTML markup and JSP elements. If you have developed Web pages, you are probably familiar with the HTML document structure statements (<head>, <body>, and so on) and the HTML statements that create a form (<form>) and a menu (<select>). The lines in bold in the example code contain the following types of JSP constructs: • Directives (<%@ page ... %>) import classes in the java.util package and the MyLocales class, and set the content type returned by the page. • The jsp:useBean element creates an object containing a collection of locales and initializes a variable that points to that object. • Scriptlets (<% ... %> ) retrieve the value of the locale request parameter, iterate over a collection of locale names, and conditionally insert HTML text into the output. • Expressions (<%= ... %>) insert the value of the locale name into the response. • The jsp:include element sends a request to another page (date.jsp) and includes the response in the response from the calling page.

  4. The code listing <%@ page import="java.util.*,MyLocales" %> <%@ page contentType="text/html; charset=ISO-8859-5" %> <html> <head><title>Localized Dates</title></head> <body bgcolor="white"> <jsp:useBean id="locales" scope="application" class="MyLocales"/> <form name="localeForm" action="index.jsp" method="post"> <b>Locale:</b> <select name=locale> <% String selectedLocale = request.getParameter("locale"); Iterator i = locales.getLocaleNames().iterator(); while (i.hasNext()) { String locale = (String)i.next(); if (selectedLocale != null && selectedLocale.equals(locale)) { %> <option selected><%=locale%></option> <% } else { %> <option><%=locale%></option> <% } } %> </select> <input type="submit" name="Submit" value="Get Date"> </form> <jsp:include page="date.jsp"/> </body> </html>

  5. How to build, deploy, and execute this JSP page? 1/2 • Go to j2eetutorial/examples and build the example by executing ant date (see How to Build and Run the Examples). • Create a J2EE application called DateApp. • Select FileNewApplication. • In the file chooser, navigate to j2eetutorial/examples/src/web/date. • In the File Name field, enter DateApp. • Click New Application. • Click OK. • Create the WAR and add the Web components to the DateApp application. • Select FileNewWeb Component. • Select DateApp from the Create New WAR File In Application combo box. • Enter DateWAR in the WAR Display Name field. • Click Edit. • Navigate to j2eetutorial/examples/build/web/date. Select index.jsp, date.jsp, MyDate.class, and MyLocales.class and click Add. Then click Finish. • Click Next. • Click JSP In The Web Component radio button, and then click Next. • Select index.jsp from the JSP Filename combo box. Click Finish. • (Continue on next slide…)

  6. How to build, deploy, and execute this JSP page? 2/2 • Enter the context root. • Select DateApp. • Select the Web Context tab. • Enter date. • Deploy the application. • Select ToolsDeploy. • Click Finish. • Invoke the URL http://<host>:8000/date in a browser. • You will see a combo box whose entries are locales. Select a locale and click Get Date. You will see the date expressed in a manner appropriate for that locale.

  7. Another Example of JSP pages (Duke Bookstore) Table 11-1 Duke's Bookstore Example JSP Pages  • To illustrate JSP technology, this chapter rewrites each servlet in the Duke's Bookstore application introduced in The Example Servlets in as a JSP page. Table 11-1 lists the functions and their corresponding JSP pages.

  8. Code Analysis • The data for the bookstore application is still maintained in a database. However, two changes are made to the database helper object database.BookDB. • The database helper object is rewritten to conform to JavaBeans component design patterns as described in JavaBeans Component Design Conventions (page 270). This change is made so that JSP pages can access the helper object using JSP language elements specific to JavaBeans components. • Instead of accessing the bookstore database directly, the helper object goes through an enterprise bean. • The advantage of using an enterprise bean is that the helper object is no longer responsible for connecting to the database; this job is taken over by the enterprise bean. • Furthermore, because the EJB container maintains the pool of database connections, an enterprise bean can get a connection quicker than the helper object can. • The relevant interfaces and classes for the enterprise bean are the database.BookDBEJBHome home interface, database.BookDBEJB remote interface, and the database.BookDBEJBImpl implementation class, which contains all the JDBC calls to the database. • The implementation of the database helper object follows. The bean has two instance variables: the current book and a reference to the database enterprise bean.

  9. Code Analysis public class BookDB { private String bookId = "0"; private BookDBEJB database = null; public BookDB () throws Exception { } public void setBookId(String bookId) { this.bookId = bookId; } public void setDatabase(BookDBEJB database) { this.database = database; } public BookDetails getBookDetails() throws Exception { try { return (BookDetails)database. getBookDetails(bookId); } catch (BookNotFoundException ex) { throw ex; } } ... } • Finally, this version of the example contains an applet to generate a dynamic digital clock in the banner. See Including an Applet for a description of the JSP element that generates HTML for downloading the applet. • The source code for the application is located in the j2eetutorial/examples/src/web/bookstore2 directory created when you unzip the tutorial bundle (see Downloading the Examples).

  10. How to build, deploy, and run the example: 1/2 • Go to j2eetutorial/examples and build the example by running ant bookstore2. • Start the j2ee server. • Start deploytool. • Start the Cloudscape database by executing cloudscape -start. • If you have not already created the bookstore database, run ant create-web-db. • Create a J2EE application called Bookstore2App. • Select FileNewApplication. • In the file chooser, navigate to j2eetutorial/examples/src/web/bookstore2. • In the File Name field, enter Bookstore2App. • Click New Application. • Click OK. • Add the Bookstore2WAR WAR to the Bookstore2App application. • Select FileAddWeb WAR. • In the Add Web WAR dialog box, navigate to j2eetutorial/examples/build/web/bookstore2. Select bookstore2.war. Click Add Web WAR. • Add the BookDBEJB enterprise bean to the application. • Select FileNew Enterprise Bean. • Select Bookstore2App from the Create New JAR File In Application combo box. • Type BookDBJAR in the JAR Display Name field. • Click Edit to add the content files. • In the Edit Archive Contents dialog box, navigate to the j2eetutorial/examples/build/web/ejb directory and add the database and exception packages. Click Next. • Choose Session and Stateless for the Bean Type. • Select database.BookDBEJBImpl for Enterprise Bean Class. • In the Remote Interfaces box, select database.BookDBEJBHome for Remote Home Interface and database.BookDBEJB for Remote Interface. • Enter BookDBEJB for Enterprise Bean Name. • Click Next and then click Finish.

  11. How to build, deploy, and run the example: 2/2 • Add a resource reference for the Cloudscape database to the BookDBEJB bean. • Select the BookDBEJB enterprise bean. • Select the Resource Refs tab. • Click Add. • Select javax.sql.DataSource from the Type column. • Enter jdbc/BookDB in the Coded Name field. • Save BookDBJAR. • Select BookDBJAR. • Select File Save As. • Navigate to the directory examples/build/web/ejb. • Enter bookDB.jar in the File Name field. • Click Save EJB JAR As. • Add a reference to the enterprise bean BookDBEJB. • Select Bookstore2WAR. • Select the EJB Refs tab. • Click Add. • Enter ejb/BookDBEJB in the Coded Name column. • Select Session in the Type column. • Select Remote in the Interfaces column. • Enter database.BookDBEJBHome in the Home Interface column. • Enter database.BookDBEJB in the Local/Remote Interface column. • Specify the JNDI Names. • Select Bookstore2App. • In the Application table, locate the EJB component and enter BookDBEJB in the JNDI Name column. • In the References table, locate the EJB Ref and enter BookDBEJB in the JNDI Name column. • In the References table, locate the Resource component and enter jdbc/Cloudscape in the JNDI Name column. • Enter the context root. • Select the Web Context tab. • Enter bookstore2. • Deploy the application. • Select Tools Deploy. • Click Finish. • Open the bookstore URL http://<host>:8000/bookstore2/enter.

  12. Result

More Related