Nearby lessons
23 of 30JSP - Program Examples
- 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
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.
I) JspPage(I)
Based on our requirement we can override jspInit() method in the jsp to define our own
initialization activities...
test.jsp:
I) JspPage(I)
Note:
We cannot place init(ServletConfig config) in the JSP b'z it is declared as final in HttpJspBase class.
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.
I) JspPage(I)
test.jsp:
I) JspPage(I)
Note: We cannot write destroy() method directly in the JSP b'z it is declared as final in HttpJspBase
class.
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.
Pre Compilation of JSP
| Template Text | JSP Elements | ||
|---|---|---|---|
| Directives | Actions | Scripting Elements | |
| page | Standard Actions | Custom Actions Traditional | Modern |
| include | <jsp:useBean> | <mine:mytag/> | |
| taglib | <jsp:setProperty> | Scriptlet | EL 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
- Every example is complete and compiles as-is
- Examples are grouped by topic
- Typing programs is the fastest way to learn JSP