Writing your first JSP page

By: Bruce W. Perry Emailed: 1586 times Printed: 2041 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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>

JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

hiiiii

View Tutorial          By: hiiiiii at 2010-07-28 14:08:50
2. View Comment

Hii I m Ravi. This is a good example.
Here we are using taglib. Don't we need to write XML file?


View Tutorial          By: Ravi at 2011-02-17 09:09:06
3. View Comment

hi, this is a good example and this rely helpful in my java program.

View Tutorial          By: Dhaval PATOLIYA at 2015-09-08 06:55:38

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Bruce W. Perry
Embedding MP3 audio file in a JSP
Embedding a QuickTime movie in a JSP
Getting HTTP Request Headers in a JSP
Using a DataSource from WebLogic in a JSP
Creating a JavaBean to Connect with Google API
Deploying an Individual JSP on Tomcat
Writing your first JSP page

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
JSP Alert Example
What are the different scopes in JSP?
JSP CheckBox Example
JSP Program for display Date
Sending Email using JSP
Comparison operators in JSP
Arithmetic Evaluation Using the Expression Language in JSP
Enable/Disable Scripting Elements in JSP
Querying Data with the JSTL in JSP
Automatically Refreshing a JSP
Uploading an Image to a Database using JSP
Writing your first JSP page
Tags using in jsp
Form processing in JSP
Most Emailed Articles (in JSP)
Click to Activate and Use this control
Embedding java codes in jsp sciptlets
HttpClient
NotifyUtil::java.net.ConnectException: Connection refused: connect
Comparison operators in JSP
Document Object
Form processing in JSP
JSP Program for display Date
The Servlet Life Cycle
Enable/Disable Scripting Elements in JSP
Steps to get a Free SSL certificate for your Tomcat
Cookies using JSP or Java Bean
The JSP Program running first Time.
JSP pages in servlet
JSP Directives