Programming Tutorials

Writing your first JSP page

By: Bruce W. Perry in JSP Tutorials on 2008-11-23  

A JavaServer Pages (JSP) component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands. JSPs were originally designed around the model of embedded server-side scripting tools such as Microsoft Corporation's ASP technology; however, JSPs have evolved to focus on XML elements, including custom-designed elements, or custom tags, as the principal method of generating dynamic web content.

JSP files typically have a .jsp extension, as in mypage.jsp. When a client requests the JSP page for the first time, or if the developer precompiles the JSP, the web container translates the textual document into a servlet.

A JSP compiler (such as Tomcat's Jasper component) automatically converts the text-based document into a servlet. The web container creates an instance of the servlet and makes the servlet available to handle requests. These tasks are transparent to the developer, who never has to handle the translated servlet source code (although they can examine the code to find out what's happening behind the scenes, which is always instructive).

The developer focuses on the JSP's dynamic behavior and which JSP elements or custom-designed tags she uses to generate the response. Developing the JSP as a text-based document rather than Java source code allows a professional designer to work on the graphics, HTML, or dynamic HTML, leaving the XML tags and dynamic content to programmers.

The sample JSP program below, shows a JSP that displays the current date and time. The example JSP shows how to import and use a custom tag library,. The code also uses the jsp:useBean standard action, a built-in XML element that you can use to create a new Java object for use in the JSP page. Here are the basic steps for writing a JSP:

  1. Open up a text editor, or a programmer's editor that offers JSP syntax highlighting.

  2. If you are developing a JSP for handling HTTP requests, then input the HTML code just as you would for an HTML file.

  3. Include any necessary JSP directives, such as the taglib directive in example below , at the top of the file. A directive begins with the <%@s.

  4. Type in the standard actions or custom tags wherever they are needed.

  5. Save the file with a .jsp extension in the directory you have designated for JSPs. A typical location is the top-level directory of a web application that you are developing in your filesystem.

Example:  A JSP file that displays the date
<%-- use the 'taglib' directive to make the JSTL 1.0 core tags available; use the uri 
"http://java.sun.com/jsp/jstl/core" for JSTL 1.1 --%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<%-- use the 'jsp:useBean' standard action to create the Date object;  the object is set 
as an attribute in page scope 
--%>
<jsp:useBean id="date" class="java.util.Date" />

<html>
<head><title>First JSP</title></head>
<body>
<h2>Here is today's date</h2>

<c:out value="${date}" />

</body>
</html>





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JSP )

Latest Articles (in JSP)