Introduction JSP Elements Implicit Objects Tag Libraries JSP Actions Error Handling
JSP Stands for Java Server Pages Presents dynamic content to users Handles the presentation logic in an MVC architecture
(business logic)
request Container
servlet
Helper Objects
Business Tier
JSP
(presentation logic)
response
Servlets
Handles dynamic data Handles business logic
Lifecylce methods init() : can be overridden service() : can be overridden destroy() : can be overridden
JSP
Compiles to
0010 0001 1100 1001 0001 0011
MyJsp_jsp.java MyJsp_jsp.class
Introduction Comments
JSP Comments
<%-- {CODE HERE} --%> Does not show the comments on the page Does not show the comments in page source Can only be used outside JSP Elements
HTML Comments
<! {CODE HERE} --> Does not show the comments on the page Shows the comments in page source Can only be used outside JSP Elements
// {CODE HERE} When put outside JSP Elements, shows comments on the page & source When put inside Scriptlets/Declarations, does not show on page & source Can be used inside scriptlets/declarations and outside JSP Elements
Want to make your HTML more dynamic? JSP Declarations JSP Scriptlets JSP Expressions
:: Code that goes outside the service method :: Code that goes within the service method :: Code inside expressions is evaluated
JSP Directives
Request 1
Service1 Service2 Service3
10
11
<html><head><title>JSP Elements Example</title> </head> <body> <%! int userCnt = 0; %> <% String name = "Sharad"; userCnt++; %> <table> <tr><td> Welcome <%=name%>. You are user number <%=userCnt%> </td></tr> </table> </body> </html>
Declarations Scriptlets
Expressions
12
Any number of the pre-defined attributes can be added to the page directive
<%@ page import = {IMPORTED_CLASSES} contentType = {CONTENT_TYPE} isThreadSafe = {true/false} session = {true/false} buffer = {BUFFER_SIZE} autoflush = {true/false} extends = {EXTENDS_FROM_PAGE} info = {PAGE_INFO} errorPage = {ERROR_PAGE_NAME} isErrorPage = {true/false} language = {LANGUAGE}
14
Including other page content into your JSP <%@ include file = {PAGE_URL_TO_INCLUDE} %> Includes files at translation time If included file is modified, the main jsp needs to be recompiled
Hello <%=userName%>
header.jsp
Main page
<%@ include file =footer.jsp"/>
main.jsp
footer.jsp
15
Providing output based on common custom logic <%@ taglib uri={TLD_FILE}" prefix={PREFIX}" %>
Components involved in tag libraries Tag handler class Common custom logic and HTML generation Descriptor configuration file Directives properties mapping information taglib directive Used in JSPs to use the tag handler functionality
16
test.jsp
sharmanj-taglib.tld
<web-app> <taglib> <taglib-uri>sharmanjTags</taglib-uri> <taglib-location> sharmanj-taglib.tld </taglib-location> </taglib> </web-app>
web.xml
TxtBoxTag.java
17
18
<html> <head> <title>JSP Implicit objects Example</title> </head> <body> <% if (YES.equals(request.getAttribute(SCOPE))) { out.println(REQUEST); } else { if (YES.equals(session.getAttribute(SCOPE))) { out.println(SESSION); } else { out.println(NONE); } } %> </body> </html>
19
An instance of JspWriter
Writes statements to the page
20
Instance of HttpServletRequest Use the attributes stored in request object for display /basic
validations in JSP
Servlet
request.setAttribute(a, 10)
JSP
a = 10
Uses helper object
Instance of HttpServletResponse Send the response back to the client (HTML in most of the
cases)
JSP1
JSP1
JSP2
22
Instance of HttpSession Retrieve and set the session attributes in JSP Remember the client across multiple requests
Request 1 Response1 Web App CLIENT Request 1 Response2
SESSION
23
JSP 1
JSP 1
JSP 1
JSP 1
24
Servlet Config
Servlet Config
Servlet Config
Servlet Config
<jsp:include page=/header.jsp"/>
Hello <%=userName%>
header.jsp
Main page
<jsp:include page=/footer.jsp"/> <table><tr> <td>Contact Details</td> <tr></table>
footer.jsp main.jsp
25
application
page
26
JSP Actions
Special tags which provides special features The container handles these tags in a special way Addresses certain common functionalities Achieve functionality with less code, and more standard way
<jsp:useBean>
<jsp:getProperty> <jsp:setProperty>
27
28
Include Directive
Include Action
Translation time
Run time
30
Syntax: <jsp:include page="{PAGE_TO_INCLUDE}" flush="true" > <jsp:param name={parameterName} value="{paramValue}" /> </jsp:include> <jsp:forward page="{PAGE_TO_FORWARD}" flush="true" > <jsp:param name={parameterName} value="{paramValue}" /> </jsp:forward>
31
bean
Servlet
JSP
<jsp:useBean ./>
request.setAttribute(userBean, userBean)
32
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}" type={package.class}" beanName=" {package.beanName}> </ jsp:useBean>
Syntax:
<jsp:getProperty name={beanInstanceName}" property= {propertyName}">
34
35
<jsp:useBean id=userBean" scope="session class=com.sharmanj.UserBean" > <jsp:setProperty name=userBean property=userName value=Sharad/> </ jsp:useBean>
36
Errors in HTML
Error Handling
38
39
Error Handling
.. . </web-app>
40
Thanks
41