Tutorial
Menus

JSP - JSP Tutorial

Jsp Overview

Rating: 4.0/5 (2 votes cast)

Level   : Beginners
Author : Arunkumar S
Download Source : Not Avaliable

Java Server Page (JSP) Overview Tutorial

JavaServer Pages (JSP) lets you separate the dynamic part of your pages from the static HTML. You simply write the regular HTML in the normal manner, using whatever Web-page-building tools you normally use. You then enclose the code for the dynamic parts in special tags, most of which start with "<%" and end with "%>". For example, here is a section of a JSP page that results in something like "Thanks for ordering Core Web Programming" for a URL of http://localhost:8080/OrderConfirmation.jsp?title=Core+Web+Programming:
Thanks for ordering 
<I><%= request.getParameter("title") %></I>
You normally give your file a .jsp extension, and typically install it in any place you could place a normal Web page. Although what you write often looks more like a regular HTML file than a servlet, behind the scenes, the JSP page just gets converted to a normal servlet, with the static HTML simply being printed to the output stream associated with the servlet's service method. This is normally done the first time the page is requested, and developers can simply request the page themselves when first installing it if they want to be sure that the first real user doesn't get a momentary delay when the JSP page is translated to a servlet and the servlet is compiled and loaded. Note also that many Web servers let you define aliases that so that a URL that appears to reference an HTML file really points to a servlet or JSP page.

Aside from the regular HTML, there are three main types of JSP constructs that you embed in a page: scripting elements, directives, and actions. Scripting elements let you specify Java code that will become part of the resultant servlet, directives let you control the overall structure of the servlet, and actions let you specify existing components that should be used, and otherwise control the behavior of the JSP engine. To simplify the scripting elements, you have access to a number of predefined variables such as request in the snippet above.

Note that this tutorial covers version 1.0 of the JSP specification. JSP has changed dramatically since version 0.92, and although these changes were almost entirely for the better, you should note that version 1.0 JSP pages are almost totally incompatible with the earlier JSP engines.


Syntax Summary


JSP Element Syntax Interpretation Notes
JSP Expression
<%= expression %>
Expression is evaluated and placed in output. XML equivalent is
<jsp:expression>
expression
</jsp:expression>
. Predefined variables are request, response, out, session, application, config, and pageContext (available in scriptlets also).
JSP Scriptlet
<% code %>
Code is inserted in service method. XML equivalent is
<jsp:scriptlet>
code
</jsp:scriptlet>
.
JSP Declaration
<%! code %>
Code is inserted in body of servlet class, outside of service method. XML equivalent is
<jsp:declaration>
code
</jsp:declaration>
.
JSP page Directive
<%@ page att="val" %>
Directions to the servlet engine about general setup. XML equivalent is
<jsp:directive.page att="val"\>. Legal attributes, with default values in bold, are:
  • import="package.class"
  • contentType="MIME-Type"
  • isThreadSafe="true|false"
  • session="true|false"
  • buffer="sizekb|none"
  • autoflush="true|false"
  • extends="package.class"
  • info="message"
  • errorPage="url"
  • isErrorPage="true|false"
  • language="java"
JSP include Directive
<%@ include file="url" %>
A file on the local system to be included when the JSP page is translated into a servlet. XML equivalent is
<jsp:directive.include
  file="url"\>
.
The URL must be a relative one. Use the jsp:include action to include a file at request time instead of translation time.
JSP Comment
<%-- comment --%>
Comment; ignored when JSP page is translated into servlet. If you want a comment in the resultant HTML, use regular HTML comment syntax of <-- comment -->.
The jsp:include Action
<jsp:include
page="relative URL"
flush="true"/>
Includes a file at the time the page is requested. If you want to include the file at the time the page is translated, use the page directive with the include attribute instead. Warning: on some servers, the included file must be an HTML file or JSP file, as determined by the server (usually based on the file extension).
The jsp:useBean Action <jsp:useBean att=val*/> or
<jsp:useBean att=val*>
...
</jsp:useBean>
Find or build a Java Bean. Possible attributes are:
  • id="name"
  • scope="page|request|session|application"
  • class="package.class"
  • type="package.class"
  • beanName="package.class"
The jsp:setProperty Action
<jsp:setProperty att=val*/>
Set bean properties, either explicitly or by designating that value comes from a request parameter. Legal attributes are
  • name="beanName"
  • property="propertyName|*"
  • param="parameterName"
  • value="val"
The jsp:getProperty Action
<jsp:getProperty
name="propertyName"
value="val"/>
Retrieve and output bean properties.
The jsp:forward Action
<jsp:forward
page="relative URL"/>
Forwards request to another page.
The jsp:plugin Action
<jsp:plugin
attribute="value"*>
...
</jsp:plugin>
Generates OBJECT or EMBED tags, as appropriate to the browser type, asking that an applet be run using the Java Plugin.

Template Text: Static HTML

In many cases, a large percent of your JSP page just consists of static HTML, known as template text. In all respects except one, this HTML looks just like normal HTML, follows all the same syntax rules, and is simply "passed through" to the client by the servlet created to handle the page. Not only does the HTML look normal, it can be created by whatever tools you already are using for building Web pages. For example, I used Allaire's HomeSite for most of the JSP pages in this tutorial.

The one minor exception to the "template text is passed straight through" rule is that, if you want to have "<%" in the output, you need to put "<\%" in the template text.


1 | 

Discussion about this tutorial

  Start a new Discussion | Read All Discussion
Subject RepliesLast Post
Javaorigin.com contact@javaorigin.com