Introduction   |  Mobile Engine Structure  |  Developing with Eclipse

 

Basics of Java Server Pages (JSP)

JSP can have following elements:

JSP Syntax Overview

 
Expressions

Syntax:

<%= Expression Here %>

Behavior:

Example:

<%= request.getRemoteHost() %>

 

Scriptlets

Syntax:

<% Java Code Here %>

Behavior:

Example 1:

<%
   String queryData = request.getQueryString();
   out.println("Attached GET data: "+ queryData);
%>

Example 2:

<%
     String bgColor = request.getParameter("bgColor");
     boolean hasExplicitColor;
     if (bgColor != null) {
       hasExplicitColor = true;
     } else {
       hasExplicitColor = false;
       bgColor = "WHITE";
     }
%>
<BODY BGCOOLOR ="<%= bgColor %>">

 

Declarations

Syntax:

<%! declaration code %>

Behavior:

Example:

...
<BODY>
  <%! private int accessCounter = 0; %>
  <H2>This page has had
     <%= ++accessCounter %>
     Hits
  </H2>
</BODY>

 

Directives

Syntax:

<%@ attribute="value" %>

Behavior:

Type of Directives

page

include

taglib – to define custom tags

 

Other JSP Tags

JSP comment

<%-- This is a JSP Comment --%>

XML type tag for expressions

<jsp:expression>

Java Expression

</jsp:expression>

XML type tag for declarations

<jsp:declaration>

Code

</jsp:declaration>

XML type tag for directives

<jsp:directive.directiveType attribute="value"/>
<jsp:directive.page language="java"/>

 

Examples

Using Expressions

<BODY>
   <%@ page language="java" %>
   <%! String title = "My First Java Server Page"; %>
   <H2>JSP Expressions</H2>
   <UL>
    <LI>Page Title: <%= title %>
    <LI>Current time: <%= new java.util.Date() %>
    <LI>Your hostname: <%= request.getRemoteHost() %>
    <LI>Your session ID: <%= session.getId() %>
    <LI>Your First Name : <%= request.getParameter("firstName")    %>
    <LI>Your Last Name : <%= request.getParameter("lastName")    %>
   </UL>
</BODY>

Using Declarations - import

Compare:
<%
java.util.Calendar rightNow = java.util.Calendar.getInstance();
int hour = rightNow.get(java.util.Calendar.HOUR_OF_DAY);
%>
With:
<%@ page import="java.util.*" %>
<%
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
%> You do not have to type the full package name all the time, when you use the import declaration

Conditional Content Type

<%
   String format = request.getParameter("format");
   if ((format != null) && (format.equals("excel"))) {
//  Content type based on parameter.
       response.setContentType("application/vnd.ms-excel");
   }
%>

<TABLE BORDER=1>
   <TR><TH></TH><TH>Apples<TH>Oranges
   <TR><TH>First Quarter<TD>2307<TD>4706
   <TR><TH>Second Quarter<TD>2982<TD>5104
   <TR><TH>Third Quarter<TD>3011<TD>5220
   <TR><TH>Fourth Quarter<TD>3055<TD>5287
</TABLE>

 

JSP Programming Examples in the MDK

The JSP in the Example 2 of the "Getting Started" section demonstrates the usage of a gridlayout for better positioning of the graphical user interface elements and places an input field and a button in the grid layout

Example 2: welcome.jsp

The tableView.jsp of Example 2 demonstrates a layout to represent data in a tabular form. It uses variables supplied by a bean to keep the rows and columns of the table flexible.

Example 2: tableView.jsp

The menu.jsp in the persistence example uses a table structure similar to tableView.jsp but has additional checkboxes in the first row.

Persistence example: menu.jsp