JSP NOTES
Agenda:
Unit1: The JSP technology Model
> JSP Api & JSP Life Cycle (Imp)
> JSP Element/Tag
> JSP Directives
> JSP Implicit Objects (9)
> JSP Scopes
> Jsp document
> JspWriter vs PrintWriter
Unit2: JSP Standard Actions/Tags
> <jsp:useBean>
> <jsp:setProperty>
> <jsp:getProperty>
> <jsp:include>
> <jsp:forward>
> <jsp:param>
Unit3: EL(Expression Language)
> Implicit object(11)
> EL operators
> EL functions
Unit4: JSTL (Popular)
> There five JSTL lib
Unit5: Custom Action Tags
> Classic tag model(JSP1.1)
> Simple tag (JSP 2.0)
> Tag files (JSP2.0)
![]() |
| JSP CONTAINEND |
What is the purpose of JSP?
> The main purpose JSP technology is to providing support for web designer.
> If any web designer want to switch as web developer then JSP is a better platform.
Imp
> The main purpose of jsp is to avoid writing the complex java code.
What is JSP?
ans:
> JSP stands for java server pages
> JSP is a server side dynamic web components which is managed by the jasper/jsp engine (web server).
> JSP is a sub specification (open specification) of JEE provided by the sun/oracle for developing dynamic web page.
What is JSP api?
> JSP api defined as a part of javax.servlet.jsp package.
> In tomcat server javax.servlet.jsp package available inside jsp-api.jar file
JSP specification: jsp-api.jar
JSP implementation: jasper.jar
> Apache has provided jsp implementation in the form of jasper.jar file (Loc: Home\lib\*.jar)
> JSP is also web technology
Note: jsp source file must be saved using .jsp extension
> Inside servlet only java code is allowed, we cant write html tags directly inside servlet.
> But inside jsp page we can write java code (servlet) as well as html code directly
JSP Page = Servlet code + HTML tags
Q: Inside web folder where we should place jsp page?
Ans: JSP should placed as public web resource.
helloworld.jsp
=> Welcome
JSP life cycle: (view.jsp)
1. Translation phase:
> Whenever jsp page first time requested by the client then jsp container translates this jsp page into servlet(*.java)
> This translation will be performed by the web container every time whenever jsp page got modified
> For checking the changes performed inside jsp page, jsp container will takes the help of araxis tool.
(view_jsp.java)
2. Compilation phase:
> In this case *.java file compiled by the java compiler based on instructions provided by jsp engine
(view_jsp.class)
3. Loading phase
4. Instantiation
5. Initialization(constructor)
6. calling jspInit()
7. calling _jspService()
8. calling jspDestroy()
> excepting calling _jspService() method remaining jsp phase performed by the container only once. These all phase again repeated whenever any changes done inside jsp page.
![]() |
| Life Cycle |
Note: After applying changes inside *.jsp file there is no need of reloading the web application.
Q: Is it possible to jsp translated/compiled code?
Ans: Yes (home\work\catlina\localhost\projectname\org\apache\jsp)
helloworld_jsp.java
package org.apache.jsp;
public class helloworld_jsp extends HttpJspBase...{
jspInit(){}
_jspService(SReq,SRes){
.............
out.write("Welcome");
............
}
jspDestroy(){
}
}
What are the jsp life cycle methods?
ans:
jspInit()
_jspService()
jspDestroy()
What is jasper engine(jsp container)?
Ans: jasper engine is a jsp container(tool) which is responsible for handling jsp life cycle phase.
> Whatever performed by the web container (servlet container) can performed by the jsp container. apart from that jsp container also performs page translation and compilation
> servlet container is a sub module of jsp engine. servlet container is a part of jsp container.
==================================================================
> Every jsp document translated as servlet class.
Q: Is it possible to say jsp is a servlet?
Ans: Yes
Q: Inheritance relation b/w jsp translated class?
JSP scripting elements:
> The main working of scripting element is an allowing java code directly inside jsp page.
There are four types of scripting elements are available in jsp
1. expressions scritpting elem
<%= ... %>
2. scriptlet
<% ... %>
3. declarative scripting elem
<%! ... %>
4. jsp comments
<%-- ... %>
Jsp Expressions or expression scripting elements:
syntax:
<%= expression %>
equivalent java code:
out.print( expression);
example: <%=Hello%>
out.print(Hello); invalid
<%="hello"%> valid
out.print("hello");
view.jsp
===========
<%= 10+5%>
view_jsp.java
===================
public final class view_jsp extends HttpJspBase{
void _jspInit(){
}
void _jspDestroy(){
}
void _jspService(HttpServletRequest req,HttpServletResponse res){
..........................
..........................
}
}
case1:
<%=Hello%> invalid
case2:
<%=10+5;%>
> During page translation phase expression elements are becomes part/body of _jspService() method.
> Result of jsp expression scripting element are displayed by using out.print() method.
Ex:
<%=new java.util.Date()%>
Ex:
<%= 10+5%><br/>
<%= 10>5 %><br/>
> inside expression we can call any methods excepting method having return type as void
<%= Math.sqrt(10)%>
<%= new java.util.ArrayList().size()%>
2. Scriptlet:
syntax:
<% java code %>
<%
.........
.........
%>
> scriptlet are pure java statement, during page translation scriptlets are directly inserted inside _jspService() method.
<%= int x = 10%> invalid
out.print(int x = 10)
<%
int x = 10; valid
out.print(x*x);
%>
void _jspService(.....){
........
int x = 10;
out.print(x*x);
........
}
Ex:
<%
int x=10;
out.print(x);
%>
Ex:
<%
int x=100;
%>
<%=x%>
> scripting elements are cant be write in nested form
case1: invalid
<%=
<%= %>
%>
case2: invalid
<%
<%=10%>
out.print("hello");
%>
case3: invalid
<%
out.print("start");
%>
<%=10%>
<%
out.print("end");
%>
3. jsp declarative elements:
syntax:
<%!
int a = 10; //inside class: instance variable
%>
<%
int x=10; //inside service: local variable
%>
Ex:
<%!
int square(int no){
return no*no;
}
%>
<%
String no = request.getParameter("no");
Integer x = Integer.parseInt(no);
%>
<%= square(x)%>
Ex:
<%!
public void jspInit(){
}
%>
Ex: invalid
<%!
public void _jspInit(){
}
%>
Ex:
<%!
public void init(){
}
%>
Scripting Elements:
<%
........
........
body
%>
1. Expression
<%= expression%> => _jspService()
=> out.print(expression);
<%= request.getParameter("x")%>
2. Scriptlet
<%
body
int x = 10;
out.print("hello");
%>
_jspService(){
int x = 10;
}
3. Declarations
<%!
int x = 10; instance variable
static int y = 20;
void m1(){
out.print("Hello");
}
DECLARATION(var+method+inner)
%>
> JSP implicit objects are avaiable for scriptlet or expression. It is non visible to declarative scripting elements.
> Using declarative scripting element we cant declare jsp methods.
Identify which method is not allowed inside jsp declarations?
init()
destroy()
service()
_jspInit()
_jspDestroy()
_jspService()
jspInit() valid
jspDestroy() valid
jspService() valid
Jsp Comments:
> Comments are ingonered by the jsp translator/compiler.
There are three types of comments allowed inside jsp document/page.
1. jsp comments
It is visible only within a jsp document.
It is neither visible inside .java file nor visible inside html resource source code(view page source)
<%--
...............this is comment
--%>
2. html/xml comments
> visible within a jsp file and html response source code.
<!--
-->
3. Java comments
> visible inside jsp page as well as .java file.
<%!
//
/* */
/** */
%>
Ex:
<%--This is JSP comment--%>
<!--This is HTML comment-->
<%!
//This is single line java comment
/*This is multi line java comment*/
%>
> It is not recommended to use jsp traditional scripting elements inside our jsp document. Because it will allow the java code inside our jsp document.
JSP implicit objects:
=====================
By default for every jsp page there are 9 implicit objects are available.
Name and Type of JSP implicit object:
Name Type
==========================
request HttpServletRequest
response HttpServletResponse
application ServletContext
config ServletConfig
session HttpSession
out JpsWriter
page Object
exception Throwable (Applicable for only error page)
pageContext PageContext(class)
> By default these implicit objects are available within a _jspService() method. Outside _jspService() method we cant access jsp implicit objects.
> We can make available these implement objects anywhere in the jsp page by using methods of PageContext class.
More about JspWriter: (JspWriter vs PrintWriter)
> By default jsp api will provides default implicit object of JspWriter class
> Implicit object name is 'out'
> JspWriter is an abstract class of jsp api which is inherited from java.io.Writer class.
> JspWriter is a character oriented output stream which is connected with jsp response.
> JspWriter is buffered stream.
JspWriter vs PrintWriter:
=========================
JspWriter PrintWriter
------------------------------------------------------------------
JspWriter is a part of PrintWriter is part of io api
jsp api
It is buffered It is non-buffered
It is connected with it is also connected with response
response
It is by default available By default it is not available to
to every jsp page the jsp page.
How to make available PrintWriter object inside _jspService()/jsp page?
> PrintWriter out = response.getWriter();
Q: Among PrintWriter and JspWriter which type of output stream is recommended to use?
ans:
> It is highly recommended to use JspWriter. Because it is specialy designed for jsp api. PrintWriter is a part of different api.
> JspWriter is a buffered stream therefore performancewise it is better than PrintWriter.
> We should not use PrintWriter and JspWriter within a same jsp page.
> either we should use PrintWriter or JspWriter but not both simulteneously.
PageContext:
> PageContext is a jsp api specific class which is declared as abstract.
> PageContext class extends from JspContext
> By default for every jsp page PageContext type implicit object is available as named 'pageContext'.
Purpose of PageContext:
> PageContext class provides getter methods for getting remaining jsp implicit object.
view.jsp
=========
<%!
void m1(PageContext pageContext){
JspWriter out=pageContxt.getOut();
ServletRequest request = pageContext.getRequest();
ServletResponse response = pageContext.getResponse();
ServletConfig config = pageContext.getServletConfig();
ServletContext context = pageContext.getServletContext();
out.println("Hello"); invalid
String x = request.getParameter("x");
PrintWriter pw = response.getWriter();
String y = config.getInitParameter("y");
context.setAttribute("x",x);
.........
}
%>
<%
m1(pageContext);
%>
2. By using pageContext methods we can perform request dispatching operations.
include();
forward();
Servlet Code:
RD rd = request.getRD("login");
............
rd.include(request,response);
Jsp code:
pageContext.include("login");
3. By using PageContext we can manage any scope attributes. By default PageContext manages page scope attribute
Servlet Code:
request.setAttribute("x",10);
HttpSession session = request.getSession();
session.setAttribute("y",20);
ServletContext context = getServletContext();
context.setAttribute("z",30);
JSP code:
pageContext.setAttribute("x",10,?)
........
request.getAttribute("?");
session.getAttribute("?")
context.getAttribute("?")
pageContext.findAttribute("?"); page,request,session,cont
Methods of PageContext class:
1. Getting implicit object
ServletRequest getRequest()
ServletResponse getResponse()
HttpSession getSession()
JspWriter getOut()
Exception getException()
ServletConfig getServletConfig()
ServletContext getServletContext()
Object getPage()
2. Request dispatching methods
void include(String url);
void forward(String url);
3. Attribute methods
setter:
void setAttribute(String name,Object value); //page
void setAttribute(String name,Object value,int scope);
Note: The only applicable values for scope argument are 1,2,3,4
1 => page scope
2 => request scope
3 => session scope
4 => application scope
getter methods:
Object getAttribute(String name); //page
Object getAttribute(String name,int scope);
Object findAttribute(String name); page>request>ses>app
remove method:
void removeAttribute(String name); //page
void removeAttribute(String name,int scope); //any scope
int getAttributeScope(String name); //valid: 1,2,3,4
//invalid: 0
JSP directives:
==============
> JSP directives are page translation time instructions to the jsp translator.
> By using jsp directives we can customize the default behaviour of jsp translator.
> By using jsp directive we can send command to the jsp translator.
Syntax:
<%@ directivename attributename='attributevalue'%>
<%@ directivename attributename="attributevalue"%>
Types of jsp directives:
1. page directive
2. include directive
3. taglib directive (JSTL,custom action)
1. page directive:
syntax:
<%@ page attname = "attvalue" %>
There are 13 attributes available for page directive.
By using page directive attributes we can instruct jsp translator.
Available attribute name:
> session
> import
> contentType
> isElIgnored
> isErrorPage
> errorPage
> isThreadSafe
> extends
> buffer="0"
> autoFlush
>into
>pageEncoding


Comments
Post a Comment