J S P (JSP) : AVA Erver AGE
J S P (JSP) : AVA Erver AGE
J S P (JSP) : AVA Erver AGE
Is translated to Compiles to
Import 010101
JSP javax. 010100
sevlet. 101010 Servlet
Httpservlet 010
Myjsp.jsp Myjsp_jsp.java Myjsp_jsp.class
LIFE CYCLE OF A JSP PAGE
ADVANTAGE OF JSP OVER SERVLET
EXTENSION TO SERVLET
JSP technology is the extension to servlet
technology. We can use all the features of servlet
in JSP.
In addition to, we can use implicit objects,
predefined tags, expression language and Custom
tags in JSP, that makes JSP development easy.
EASY TO MAINTAIN
JSP can be easily managed because we can easily
separate our business logic with presentation
logic.
FAST DEVELOPMENT: NO NEED TO
RECOMPILE AND REDEPLOY
Request 1
Request 2
Declarations
Request 3
14
JSP ELEMENTS - SCRIPLETS
What are Scriplets?
Whatever goes inside “<%JAVA_HERE%>” tag is called a
scriplet
Everything you write in a scriplet goes in the sevice method
Treat variables in scriplets as methods/local variables
Example:
<%= localvar %>
EXAMPLE
JSP Elements - Example
<% Scriptlets
String name = "Sharad";
userCnt++;
%> Expressions
<table>
<tr><td>
Welcome <%=name%>. You are user number <%=userCnt%>
</td></tr>
</table>
</body>
</html>
19
JSP ELEMENTS :- DIRECTIVES
What are Directives?
Whatever goes inside “<%@{Directives}%>” tags are called directive
Directives gives pre-processing commands to the JSP engine.
Everything in directives are processed before jsp is translated to servlet
Example:
<%@ page import =“java.util.ArrayList” %>
THERE ARE THREE TYPES OF DIRECTIVE
TAG:
Directive Description
<%@ include ... %> Includes a file during the translation phase.
Create the JSP file that uses the Custom tag defined
in the TLD file
FLOW OF CUSTOM TAG IN JSP
TAGLIB LIBRARY – HIGH LEVEL OVERVIEW
<%@ taglib uri=“jspTag” prefix=“mytag”%>
----
<mytag:txtbox length=“10” name = “username”/>
jspTag-taglib-tld
Test.jsp <tag>
<name>txtbox</name>
<tagclass>txtboxclass</tagclass>
</tag>
Txtboxclass.java
Web.xml Public class txtboxclass extends Tagsuport
<web-app>
<taglib> {
<taglib-uri>jspTag</taglib-uri> Public int doStartTag()
<taglib-location>jspTag-taglib-tld {
</taglib-location> --------
</taglib> }
</web-app> }
IMPLICIT OBJECTS- INTRODUCTION
Set of predefined objects readily available for use.
Out : JspWriter
Request : HttpRequest
Response : HttpResponse
Session : HttpSession
Config : ServletConfig
Application : ServletContext
Page : Jsp Page
Page context : special objects
IMPLICIT OBJECTS:- OUT
An instance of JspWriter
Helper
Object
Uses helper (a=10)
object
IMPLICIT OBJECTS: RESPONSE
Instance of HttpServletResponse
Send the response back to the client
Response.sendredirect(“jsp2”)
Send response to
Redirect to JSP
page the client
JSP1 JSP2
IMPLICIT OBJECTS : SESSION
Instance of HttpSession
Retrieve and set the session attributes in JSP
Request1
Client
Response1
Web App
Request2
Response2
IMPLICIT OBJECTS : CONFIG AND
APPLICATION
servletContext
Hello <%=userName%>
<jsp:include page=“/header.jsp"/>
header.jsp
Main page
<table><tr>
<td>Contact Details</td>
<tr></table>
<jsp:include page=“/footer.jsp"/>
footer.jsp
main.jsp 33
Implicit Objects – pageContext
application
Least visible to most visible
session
pageContext
request
page
34
JSP Actions
<jsp:include>
<jsp:forward>
<jsp:param>
<jsp:useBean>
<jsp:getProperty>
<jsp:setProperty>
35
JSP Actions - <jsp:include>
Definition:
Includes a page at the given location in the main page
Syntax:
36
JSP Actions – Include Action v/s Include Directive
37
JSP Actions - <jsp:forward>
Definition:
Forwards the request to the given page
Syntax:
38
JSP Actions - <jsp:param>
Definition:
Pass parameters to the included/forwarded page
Syntax:
39
A JAVABEAN
A Javabean is a ordinary java class that conforms to the following
rules:
Java in a JSP
* Java Scriplets
* Include Statement
* Javabeans
JSP Actions - <jsp:useBean>
getId();
setId(..)
getName()
How can I use
setName(…)
that bean?
bean
Servlet JSP
42
JSP Actions - <jsp:useBean>
Definition:
Instantiate a bean class, use bean properties, and set property values
Syntax:
<jsp:useBean id=“{beanInstanceName}"
scope="page|request|session|application"
class=“{package.class}" >
</ jsp:useBean>
Definition:
Gets property values of a Bean
Syntax:
<jsp:getProperty name=“{beanInstanceName}"
property= “{propertyName}">
44
JSP Actions - <jsp:setProperty>
Definition:
Sets property values in a Bean
Syntax:
<jsp:setProperty name=“{beanInstanceName}"
property= "*" | property="propertyName"
param=“{parameterName}"
value="{paramValue}" } />
45
JSP Actions – useBean
<jsp:useBean id=“userBean"
scope="session“
class=“com.jaydeep.UserBean" >
<jsp:setProperty name=“userBean”
property=“userName”
value=“jaydeep”/>
</ jsp:useBean>
46
Error Handling – Types of errors
• Errors in HTML
48
Error Handling – Error page
49
Error Handling
• Specify error page in your main page • Specify error page in web.xml
<%@page errorPage=“errPage.jsp" %> <web-app>
<html> …………….
<head> ……………
<title>This is the main page</title>
</head> <error-page>
<body> <exception-type>com.a.myExp</exception-type>
………………….. <location>/error.jsp</location>
………………….. </error-page>
…………………… <error-page>
…………………… <error-code>404</error-code>
<location>/errorServlet</location>
// ERROR CODE GOES HERE </error-page>
………………….
…………………. ………………..
</body> ……………….
</html> </web-app>
50
Thanks
51