Saturday, 17 December 2011

What Is a JSP Pages?


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
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.
    <%@ 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>
     
    
To build, deploy, and execute this JSP page:
  1. Go to j2eetutorial/examples and build the example by executing ant date.
  2. Create a J2EE application called DateApp.
    1. Select FileNewApplication.
    2. In the file chooser, navigate to j2eetutorial/examples/src/web/date.
    3. In the File Name field, enter DateApp.
    4. Click New Application.
    5. Click OK.
  3. Create the WAR and add the Web components to the DateApp application.
    1. Select FileNewWeb Component.
    2. Select DateApp from the Create New WAR File In Application combo box.
    3. Enter DateWAR in the WAR Display Name field.
    4. Click Edit.
    5. Navigate to j2eetutorial/examples/build/web/date. Select index.jspdate.jspMyDate.class, and MyLocales.class and click Add. Then click Finish.
    6. Click Next.
    7. Click JSP In The Web Component radio button, and then click Next.
    8. Select index.jsp from the JSP Filename combo box. Click Finish.
  4. Enter the context root.
    1. Select DateApp.
    2. Select the Web Context tab.
    3. Enter date.
  5. Deploy the application.
    1. Select ToolsDeploy.
    2. Click Finish.
  6. Invoke the URL http://<host>:8000/date in a browser.


No comments:

Post a Comment