Monday, July 13, 2009

Notes JSP

Notes JSP

JSP Directives
JSP Server - Apache Tomcat
JSP Syntax
JSP Directory Structure
JSP Expression Language and Implicit Objects
JSP output to HTML
JSP Attributes
JSP Standard Action Tags
Tag library
Example of web.xml file

JSP are
- HTML pages that have Java code embedded.
- server side programs

- JSP have 3 sets of data
1. JSP directives
2. Action Elements
3. Implicit Objects

JSP Directives
Aim: provide information to the JSP container about the page.
<%@ page attributes %>
<%@ include attributes %>
<%@ taglib attributes %>

Page attributes = {import, session, isThreadSafe, Info, errorPage,
isErrorPage, contentType, pageEncoding}

Include attributes = {file}

Custom Action = tag in the JSP file. Eg. <prefix:name />
Tag Handler = the java class that provides the behaviour of
custom action at run time.

JSP Server - Apache Tomcat
(see NotesTomcat, NotesGridsphere)

JSP Syntax
<%-- blah comments --%>
Directives Elements  (JSP style | XML style)
<%@ page attributes %>    | < attributes />
<%@ include attributes %> | <jsp:directive.include attributes />
Scripting Elements
<%! declaration %>        | <jsp:declaration> declaration </jsp:declaration>
- can be used to define methods
- variables declared here are available to all code in the same JSP page
<% scriplet code %>       | <jsp:scriplet> code fragment </jsp:scriplet>
- variables declared here are only available in the codelet fragment
<%= expression %>         | <jsp:expression> expression </jsp:expression>
Action Elements (JSP 2.0 specs)
<jsp:useBean>      (creates the bean from a Java Bean class)
<jsp:getProperty>  (uses the getXXX method of JavaBean)
<jsp:setProperty>  (uses the setXXX method of JavaBean)
<jsp:plugin>, <jsp:params>, <jsp:fallback> (used to include JavaBeans or applets)
<jsp:invoke>   (valid for tag libraries)
<jsp:doBody>   (valid for tag libraries)

JSP Directory Structure
     footer.jspf    (a jsp code fragment #included in code)
     errorPage.jsp  (JSP diverts here if there is error)
        (tlds files)
        (archives used by application)

JSP Expression Language (EL) and Implicit Objects
Instead of using JSP scripting elements (see JSP Syntax section),
JSP can use EXPRESSION LANGUAGE (EL) which has a much simpler syntax.
Examples are:
${"Single quotes in 'double quotes' need not be escaped"}

EL also has implicit objects available to it:

JSP output to HTML
Two ways JSP can output onto html page:

Hello!  The time is now <%= date %>


Hello!  The time is now
    // This scriptlet generates HTML output
    out.println( String.valueOf( date ));

JSP Attributes
Each JSP page can have a set of attributes related to the particular page.
Example (developing a tag handler) shows how to interact between a
ClassBean with the member: "topic"
JSP page with attribute: "qid", "question"

1. Structure

2. Define the class bean:
package ch04
   public class SimpleList extends SimpleTagSupport{
       private String topic;
       public void setTopic(String s) { topic = s; }
       public String getTopic() { return topic; }
       public void doTag() throws JspException{
          getJspContext().setAttribute("qid", topic ....);
          getJspContext().setAttribute("question", topic ....);
3. define simplefaq.tld
<?xml ....>
  <short-name>  .... </short-name>
    <attribute>         // "topic" member of classsBean
4. define web.xml
<?xml ......>

5. write the JSP file
<%@ taglib uri="/simplequestions" prefix="faq" %>
  <faq:simplelist topic="${param.topic}">
    <a href="Questions.jsp?qid=${qid}"> ${qid} </a>
    ${question} </p>

faq - tag library name
simplelist - name of tag
topic - an attribute of the simplelist tag
param.topic - obtained from the bean              

The following are available in the standard

Interface  <-----  Base Class (Implementing the interface)
=========          ========================================
SimpleTag          SimpleTagSupport

Simple Tag Handlers = {SimpleTag, JSPFragment} since JSP2.0
Classic Tag Handlers = {Tag, BodyTag, IterationTag} since JSP1.1,1.2

Tag Library Descriptor (TDL) is an xml file
- to inform container which tag handlers are available to JSP pages,
  after creating your own classes that implement a tag.
- configuration file is:
- descriptor.tld is inside a jar file jarTLD.jar, then store in:

JSP Standard Action Tags
since JSP 2.0
xxx = {useBean, setProperty, getProperty, param, include, forward,
plugin, params, fallback, attribute, body, invoke, doBody} 

Tag library
to create own Tag library
- create a Tag Handler class
    - extends SimpleTagSupport
    - contain get / set methods for data eg. "topic"
    - eg. name of class is SimpleList in package Ch04
- create TLD and place in /WEB_INF/tlds/, call it simplefaq.tld
        <name> simplelist </name>
        <body-content> scriptless</body-content>
- create Deployment Descriptor for Tag Library
if using Tomcat, create web.xml with this portion


- create JSP file TopicList.jsp
  <%@ taglib uri="/simplequestions" prefix="faq"%>
     <faq:simplelist topic="${param.topic}">
       ...... ${question}       

using Tag Library:
  create a TLD (Tag Library Descriptor) xml file and fill in details of the tag or class using:
       <tag> ..... </tag>

importing a Tag Libray to use:
   to use a tag library,
       <%@ taglib uri="URI_of_Library" prefix="tag_prefix" %>

- Deploying tag libraries involve:
  i)copy jars and tlds
  ii) add mapping to web.xml
  iii) add a taglib directive to the JSP page, eg:
      <% taglib uri="/portletUI" prefix="ui" %>

- Tag libraries include:
  - JSTL(Sun)
  - Struts(Jakarta) for MVC
  - JNDI (Jakarta) for using Java Naming and Dir Interface
  - BEA Weblogic portal JSP Tag Lib - for working with BEA
  - Coldjava bar Charts - for making graphs
  - Orion EJB - for making EJBs


1.  Put location of the class in Tag Library Descriptor (TLD)
file in WEB-INF\tlds\portletui.tld

2. Specify location of TLD in:   WEB-INF\web.xml

3. In JSP file, add the location of the taglib:  
<%@ taglib uri="/portletUI" prefix="ui" %>
<%@ taglib uri="" prefix="portlet" %>
    Hello, <ui:text beanId="nameTB"/> !
    <ui:textfield size="20" beanId="nameTF"/>
    <ui:actionsubmit action="showName" value="Say Hello!"/>

Example of web.xml file

General part of web.xml
welcome-file: in general tries to load index.html or index.jsp, if not it
    will load from this list of welcome files.
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
   PUBLIC "-//Sun Microsystems, Inc. //DTD Web Application 2.3//EN"

    <display-name>This is project TomcatTestProject</display-name>
This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide.

      <!-- Loads these files as default if no index.html or index.jsp -->
      <welcome-file> welcome.jsp </welcome-file>
where welcome.jsp is a file in the application.

servlet mappings of general servlet Name, to servlet Class to URL pattern


The requested resource (/<name>/) is not available.
- make sure that "name" is under webapp subdirectory
- make sure that in "name"'s WEB-INF directory, there is a web.xml
- manually type the URL into the browser like:
  then go back to Tomcat Manager, undeploy, then start app again

No comments: