Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Introduction to JSP (Java Server Pages)

This article will tell you about JSP basic concepts. We learn about requirements for running JSP, lifecycle of JSP, directives, declaration, scriptlet, expression and finally about comments.

What is JSP?

JSP (Java Server Pages) can be used for making dynamic websites. It allows developers to make use of Java codes inside JSP file which broaden the functionality of Web application. You can make a database driven application too with JSP using JDBC. The best thing about JSP is its platform independence.

Requirement for running JSP?

You will need the following:

  1. An IDE (Eclipse or Netbeans)
  2. A web server to run your JSP file. There are many server available like Apache Tomcat, Glassfish(Comes along with Netbeans), etc.

Lifecycle of JSP

  1. compilation: Parses the file
  2. Initialing
  3. Execution
  4. CleanUP: Used for cleaing up

Now we will discuss the different basic tags in JSP. Please understand them carefully, since these are most basic.

Directive

There are 3 directives which we may use:

  1. page: This can be used to define several attributes of our current JSP page. We will discuss its attribute through an example.
  2. include: This can be used to include another JSP page in our current page. This can help reduce lots of coding and hence saves lots of time.
  3. taglib: This is used to introduce custom tags into our JSP page.

Listing 1: J2EE Example.

<%@page language="java" session="true" errorPage="error.jsp"  %>
<%@include file=”header.jsp” %>
<%@ taglib uri="uri" prefix="prefixOfTag" >

This is very simple example in which we used 3 directives which are:

Page: We have set the various attributes of the page. We list some attribute with there explaination.

  1. language: Tells the programming language used in JSP
  2. contentType: Tells the character encoding scheme.
  3. errorPage: Defines the error page. In case of any exception in current page, the server will redirect client to error page.
  4. extends: Used for exending other class
  5. import: Used for importing class
  6. session: Tells whether session is working on current page or not

include:We used one attribute ie file to include another jsp file into our current Jsp page.

taglib:It is used to include a remote library and we define a prefix which provides a way to use this tag.

Declaration

Declaration are used for declaring variables and methods in Jsp which will be used later in the program. They are used with tags <%! Your Declaration %>

Listing 2: J2EE Simple Declaration Example

<%! int i = 0; %> 

Above, we have declared a variable I which is initialized with zero. Now this variable I can be used anywhere ie it's global

Scriptlet

Scriptlet can be used to number of Java statements, methods, variable declaration. The variable declaration is not the same as we learnt above. Here the variables are local in nature ie have limited scope. They are used making use of tag <% Statements %>

Listing 3: J2EE Simple Scriptlet Example

<html>
<head><title>csanuragjain</title></head>
<body>
Hello !<br/>
<%
out.println(“This is easy.”);
%>
</body>
</html>

We make a JSP page and we use the scriptlet to display the message ‘This is easy’. So this text also gets written on the screen.

Expression

It can be used for evaluation purpose.

Listing 4: JSP Expression Example

<html> 
<head><title>Anurag Jain</title></head> 
<body>
<p>
Date: <%= (new java.util.Date()).toLocaleString()%>
</p>
</body> 
</html> 

Comments

JSP has a simple way of including comments, the tag is <%-- JSP comment --%>

Listing 5: JSP Comments Example

<html>
<head><title>csanuragjain</title></head>
<body>
Hello !<br/>
<%--
out.println(“This is easy.”);
--%>
</body>
</html>

We included the tag comment this, is very important for maintainence.

We show overall example:

<html> 
<head><title>Anurag Jain</title></head> 
<body>

<%@page language="java" session="true" errorPage="error.jsp"  %>
<%@include file=”header.jsp” %>

<%--
out.println(“Example.”);
--%>
<%! String name=”Anurag”<%>

<%
out.println(“This is fun.”);
%>
 <%=name%>
<p>Date: <%= (new java.util.Date()).toLocaleString()%></p>
</body>
</html>

We will discuss more about J2EE features in coming articles. Hope you like this article. See you soon with a new article



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login