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:
- An IDE (Eclipse or Netbeans)
- 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
- compilation: Parses the file
- CleanUP: Used for cleaing up
Now we will discuss the different basic tags in JSP. Please understand them carefully, since these are most basic.
There are 3 directives which we may use:
- page: This can be used to define several attributes of our current JSP page. We will discuss its attribute through an example.
- 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.
- 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.
- language: Tells the programming language used in JSP
- contentType: Tells the character encoding scheme.
- errorPage: Defines the error page. In case of any exception in current page, the server will redirect client to error page.
- extends: Used for exending other class
- import: Used for importing class
- 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 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 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.
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>
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