JSP (java server pages)
JSP will be divided in to 3 sections
Scriplets
Expressions
Declarations
Life Cycle Of JSP:
Translated servlet of a jsp contains the following
jspInit()
_jspService()
jspDestroy()
scriplet:
.is a jsp element in which we can write any java valid stmt.
.All the java stmts inside the scriptlet must be terminated with the semi colon
.Write the scriplet <% %>
Expressions:
.Is a jsp element which is shortcut form of out.println.
.<%= "i am an exp"%>
.Semicolon is not allowed in the expression
Declaration:
.Is a jsp element in which we can write the following
1) variable declarations
2) method definations
3) class definations
4) inteface definations
Syntax : <%! %>
Directives: is an instruction given to the container for a particular task.
1) include
is used to include another jsp or html
<%@include file ="filename"%>
When we use include directive the directive statement will be replaced with contents of include jsp or html
2) page
must be the first line the jsp
<%@ page extends= "" %>
<%@ page contentType="text/xml" %>
<%@ page errorPage="MyErrorPage.jsp" %>
<%@ page isErrorPage="true" %>
<%@ page import="java.sql.*" %>
<%@ page session="true" %>
3) taglib directive is used to use the sun taglibraries(JSTL java standard tag libraries) and your own custom taglibraries
JSP Standard Action:
<jsp:include>
<jsp:forward>
<jsp:param>
<jsp:useBean>
<jsp:setProperty>
<jsp:getProperty>
<jsp:plugin>
<jsp:fallback>
Include : This action is used to include another jsp or html
<jsp:include page="html or jsp file"/>
Forward : Used to forward a request from current jsp to another jsp or html
<jsp:forward page="any file"/>
.Include and Forward actions functionality similar to request dispatcher interface include() and forward()methods
.When use include, control will be transfered to include.jsp and executes and excuted response will be included and continue the execution of statement next to include action.
.When we use forward, control will be forwarded to the specified jsp. Its won't execute statements after forward
Param: Used along with <jsp:include> and <jsp:forward> . Used to send the parameters form current jsp to another jsp that included or forwarded.
<jsp:param name="name" value="siva"/>
UseBean :
<%@ page import="com.bellinfo.Student" %>
<jsp:useBean id="ss" class="Student" scope="request">
<jsp:setProperty name="ss" property="id" value="99"/>
<jsp:setProperty name="ss" property="name" value="siva"/>
<jsp:forward page="xyz.jsp"/>
<jsp:getProperty name="ss" property="id"/>
JSP Scopes:
1) Page – Any obeject with in the page scope can be accessible with in a jsp
2) Request – Any object with in request scope can be accessible across multiple jsps with in one req and res mechnaism
3) Session – Any obejct with session scope can be accessible across multiple jsps and multiple request from one client
4) Application – Any obejct with application scope can be accessed across multiple pages, multiple request and multiple sessions
Implicit Objects:
We have 9 implicit object in JSP, container creates all these implicit objects for you. you can use simply with out creating.
1)out
2)request (javax.servlet.http.httpServletRequest)
3)response ()
4)session (HttpSession)
5)Application (ServletContext)
6)Page
7)Config (ServletConfig)
8)PageContext (Javax.servlet.jsp.PageContext)
9)Exception (JspException)
All the remaining implict objects will be created using pageContext
Custom Tag library:
<%@ taglib uri="bellinfo.com" prefix="koka"%>
<koka:hello name="siva" email="siva@gmail.com"/>
o/p : Hello, Siva!! Nice work, your email siva@gmail.com is accepted.
2)Web.xml
<taglib>
<tlib-uri></tlib-uri>
<tlib-location></tlib-location>
3)Write the .tld file
4)Implement the tagHandle class
JSP JSTL Tags
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Core Tag Library
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach var = "x" begin = "10" end = "15">
Item <c:out value = "${i}"/><p>
</c:forEach>
<c:forEach var="team" items="${teams}">
<tr>
<td>${team.id}</td>
<td>${team.name}</td>
<td>${team.rating}</td>
</td>
</tr>
</c:forEach>
<c:set var="score" value="25"/>
<c:if test = "${salary > 20}">
<p>Match won with: <c:out value = "${score}"/><p>
</c:if>
<c:redirect url = "http://www.photofuntoos.com"/>
Functions Taglibrary
<%@ taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "fun"%>
<c:set var = "sample" value = "I'm learning jstl tags"/>
<c:if test = "${fn:contains(sample, 'jstl')}">
<p>I'm on track<p>
</c:if>
Formating Tag Libraries
<%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt"%>
<c:set var = "number" value = "35454" />
<fmt:formatNumber value = "${number}" type = "currency"/></p>
<fmt:setLocale value = "en_US"/>
XML Tag libraries
<%@ taglib uri = "http://java.sun.com/jsp/jstl/xml" prefix = "xml"%>
SQL Tag libraries
<%@ taglib uri = "http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>