JAVATM MASTER

JSPTM Lesson

Back to Table of Contents

JSPTM (JavaTM Server Pages) is like a combination of Active Server Pages and Java Servlets. A Servlet is a java application that can receive HTTP requests and returns HTTP responses, just like other servers. The difference is that Servlets are written in Java and they run on a JVM (Java Virtual Machine). This JVM runs on the server machine, specific to the host machine, which can be Linux or Windows (NT or 2000) or Solaris Unix. This JVM servlet engine, such as Tomcat or Resin, which are freeware versions, converts the JSP page into a Servlet application when it is first called.

The JSP java code is converted into the code of the servlet and the HTML code in the JSP page is outputted to the client, usually a web browser, like Firefox, Safari, or Internet Explorer. Special tags are used to define what is java code and what is to be interpreted as html or text to be printed out. The following are a list of the most important tags:

<!-- This is a HTML comment, the JSP Servlet prints this out into
the outgoing HTML document -->

<%-- This is a JSP Comment. Whatever is written within these
tags is ignored by the Servlet Compiler and is not printed 
in the outgoing HTML document.
 --%>

<%@ page import="java.util.*" %>
<%-- The above is a demo of how to import packages into the Servlet. --%>

<%!  
//In these tags are put all global variables, methods, and inner classes
//Because the JSP compiler only goes through the JSP once, all methods,
//variables, and inner classes have to be declared before they
//are used. Regular classes that you compile with the javac command,
//however, don't have this restriction.
//Example:
double PI = Math.PI;
String NoNull(String S){ //returns an empty String if String is null.
	return S == null ? "" : S;
} 
%>

<%  
//this is java code that would be put into the main method that
//is executed when the JSP is called. 
//Example of getting a value from a field called 'Name' from the calling
//form or client.:
String Name = request.getParameter("Name");
out.println("Hello "+Name);

%>

<P>This is HTML text that is imbedded within the JSP page.

<%= "Another way of printing a variable: "+Name %>

You can also use java loops (for, while, and do while) in JSP to repeat blocks of HTML code within the JSP page. Also, you can use if, else if, and else blocks to skip HTML code or conditionally enter HTML code.

To send your request to another page you can use the forward tag. Note that the forward tag sends the request to another jsp page that does not return to the sender:

<jsp:forward page="mydestiny.jsp">
<jsp:param name="javaemerald.com" value="64.49.195.135"/>
<jsp:param name="UserName" value="Eric"/>
<jsp:param name="Password" value="Java"/>
</jsp:forward>

<%-- Notice the forward slashes. All tags that don't start
with <% are treated like XML tags, which means they
have to end with a slash '/' if they constitute a single tag, 
or if they occur as a pair, the second tag must begin 
with a slash. --%>

<%@ include file="../common/common.inc"%>
<%-- This is a useful tag to include other JSP code from other
files into the JSP java code. In this case, the java code is 
contained in a file called common.inc which is located in
the common directory one directory up from where this jsp
page is located. This prevents hacker from easily getting your
sensitive data. This file would normally just contain
declaration tags containing declarations of variables, 
methods, or inner classes.--%>

And that sums up the JSP lesson of the day.

*OracleTM and JavaTM are registered trademarks of Oracle and or its affiliates. Other names may be trademarks of their respective owners.*