Java Server Pages (JSP) Basics

What is JSP? Java Server Pages (JSP) is a technology that lets you mix regular, static HTML with dynamically-generated HTML. Many Web pages that are built by CGI programs are mostly static, with the dynamic part limited to a few small locations. But most CGI variations, including servlets, make you generate the entire page via your program, even though most of it is always the same. JSP lets you create the two parts separately. Here's an example:
							
<html> <head> </head> <body> <% // jsp sample code out.println(" JSP, ASP, CF, PHP - you name it, we support it!"); %> </body> </html>
Save this file with the extension .jsp - for example, "onetest.jsp" - in the JSP webapps folder and then view it by pointing your browser to it - for example, http://yourdomain.com/webapps/onetest.jsp . You should see something like this:

JSP, ASP, CF, PHP - you name it, we support it!

If you view the source code, you'll see only the generated html (just like an ASP file):

							
<html> <head> </head> <body> <b> JSP, ASP, CF, PHP - you name it, we support it!</b> </body> </html> </font>
In JSP-lingo, this example is a "scriptlet" - a block of code executed by the JSP engine when the user requests a JSP page. All scriptlets are enclosed within <%...%> tags (similar to ASP and PHP code), like this:
							
<% ... JSP code ... out.println(" JSP, ASP, CF, PHP - you name it, we support it!"); ... JSP code ... %>
Every JSP statement ends in a semi-colon - this convention is identical to that used in Perl, and omitting the semi-colon is one of the most common mistakes newbies make.

Need to include a time or date stamp on a web page? Just use:
							
<html> <head> </head> <body> // jsp sample date Hello! The time is now <%= new java.util.Date()%> </body> </html>
Want to demonstrate a visitor's IP address? Try:
							
<html> <head> </head> <body> // jsp sample ip Hello! Your IP address is <%out.println( request.getRemoteHost());%> </body> </html>
To configure your JSP-enabled account for JSP use, see JSP Manager. There are many more uses for JSP - see JSP & MySQL for database connections, and JSP Resources for links to more sample code and tutorials.