Nearby lessons

23 of 30

JSP - Program Examples

📌 What You Will Learn
  • Practise complete, runnable JSP code
  • See the generated servlet code
  • Type each program yourself to learn faster

A collection of complete JSP programs and tag-handler examples from every topic — copy any program, deploy it in Tomcat, and watch it run.

JSP API and Life Cycle

The process of Translating JSP Page (.jsp file) into corresponding Servlet (.java file) is called

Translation Phase.

JSP Servlet

demo.jsp demo_jsp.java

This can be done by JSP Engine. In Tomcat this component is called JASPER.

demo.jsp:

<h1>The Server time is:<%= new java.util.Date() %></h1>

The generated .java file is available in the following location in Tomcat

D:\Tomcat 7.0\work\Catalina\localhost\jspdapp1\org\apache\jsp\demo_jsp.java

Example01
JCode Cell
1 
2Translation phase
3JSP Page Compilation
4Load the class
5Create an instance
6Call jspInit() method
7call _jspService() method
8call jspDestroy() method
9

I) JspPage(I)

public void jspInit()

This method will be executed only once at the time of first request to perform initialization

activities.

Web container always calls init(ServletConfig) of HttpJspBase class which internally calls

jspInit() method.

Example02
JCode Cell
1 
2jspInit()
3jspDestroy()
4jspInit():
5

I) JspPage(I)

Based on our requirement we can override jspInit() method in the jsp to define our own

initialization activities...

test.jsp:

Example03
JCode Cell
1 
2public abstract class HttpJspBase ..
3{
4public final void init(ServletConfig config)..
5{
6jspInit();
7}
8}
9

I) JspPage(I)

Note:

We cannot place init(ServletConfig config) in the JSP b'z it is declared as final in HttpJspBase class.

Example04
JCode Cell
1 
2<%!
3public void jspInit()
4{
5System.out.println("jsp initialization activities");
6}
7%>
8<h1>The Server Time is:<%= new java.util.Date() %></h1>
9
Output

jsp initialization activities
      

I) JspPage(I)

public void jspDestroy()

This method will be executed only once to perform cleanup activities just before taking jsp

from out of service.

We can override jspDestroy() method in our jsp to define our own cleanup activities.

Web container always calls destroy() method available in HttpJspBase class which

internally calls our jspDestroy() method.

Example05
JCode Cell
1 
2jspDestroy():
3

I) JspPage(I)

test.jsp:

Example06
JCode Cell
1 
2public abstract class HttpJspBase ..
3{
4public final void destroy()
5{
6jspDestroy();
7}
8}
9

I) JspPage(I)

Note: We cannot write destroy() method directly in the JSP b'z it is declared as final in HttpJspBase

class.

Example07
JCode Cell
1 
2<%!
3public void jspDestroy()
4{
5System.out.println("jsp cleanup activities");
6}
7%>
8<h1>The Server Time is:<%= new java.util.Date() %></h1>
9
Output

jsp cleanup activities
      

II) HttpJspPage(I)

At the time of translation, JSP Engine will place this method in the generated servlet class.

Q1.In the JSP is it possible to write _jspService() method explicitly?

No b'z this method already generated by JSP Engine automatically.

If we write explicitly then generated servlet will contain two _jspService() methods,which causes

compile time error.

Q2. What is the significance of _ symbol in _jspService() method?

It indicates that this method will be generated automatically by JSP Engine and we cannot write

explicitly.

Q3. Is it possible to write service() method explicitly in the JSP?

No b'z this method declared as final in HttpJspBase class.

Note:

JspPagejspInit(),jspDestroy()

HttpJspPage_jspService()

Q.In our JSP, which of the following methods we can write explicitly?

1.init()

2.jspInit()

3.destroy()

4.jspDestroy()

5.service()

6._jspService()

1,3,5 are final methods in parent class

6th method automatically generated by JSP Engine.

Example08
JCode Cell
1 
2public abstract class HttpJspBase extends..
3{
4public final void service(HSR request, HSR response) throws SE, IE
5{
6_jspService(request, response);
7}
8}
9

Pre Compilation of JSP

Template TextJSP Elements
DirectivesActionsScripting Elements
pageStandard ActionsCustom Actions TraditionalModern
include<jsp:useBean><mine:mytag/>
taglib<jsp:setProperty>ScriptletEL Elements

<jsp:getProperty>

<jsp:include>Expression${X}

<jsp:forward>

<jsp:param>Declaration

<jsp:plugin>

<jsp:fallback>Comments

<jsp:params>

JSP Comments

HTML Comments

Java Comments

Example09
JCode Cell
1 
2Template Text
3Scripting Elements
4Standard and Custom actions
5Expression Language ElementsJSP Page
6
📝 Key Takeaways
  • Every example is complete and compiles as-is
  • Examples are grouped by topic
  • Typing programs is the fastest way to learn JSP