Академический Документы
Профессиональный Документы
Культура Документы
sharadballepu@sharmanj.com
www.sharmanj.com
1
Java Server Pages (JSP) Servlets & JSPs
Agenda
• Introduction
• JSP Elements
Implicit Objects
• Tag Libraries
• JSP Actions
• Error Handling
• Demo
•
2
Introduction – What is JSP
(business logic)
request servlet
Business Tier
Container
Helper
Objects
JSP
(presentation logic)
response
3
Introduction – JSP v/s Servlet
Servlets JSP
JSP
servlet. 1100 1001 Servlet
HttpServlet.* 0001 0011
5
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
7
JSP Elements - Declarations
• Example
<%! int instanceVar = 10; %>
8
JSP Elements - Declarations
Request 1
Request 2
Declarations
Request 3
9
JSP Elements - Scriptlets
• Example
<% int localVar = 10; %>
10
JSP Elements - Expressions
• Example
<%= localVar %>
11
JSP Elements - Example
<% Scriptlets
String name = "Sharad";
userCnt++;
%> Expressions
<table>
<tr><td>
Welcome <%=name%>. You are user number <%=userCnt%>
</td></tr>
</table>
</body>
</html>
12
JSP Elements - Directives
Example
<%@ page import=“java.util.ArrayList” %>
13
Directives – page directive
• Any number of the pre-defined attributes can be added to the page directive
14
Directives – include directive
header.jsp
Main page
<%@ include file =“footer.jsp"/>
<table><tr>
<td>Contact Details</td>
<tr></table>
main.jsp footer.jsp 15
Directives – taglib directive
taglib directive
Used in JSPs to use the tag handler functionality
16
taglib directive – high-level overview
<tag>
<name>txtBox</name>
<%@ taglib uri=“sharmanjTags” prefix=“sharmanj” %> <tagclass>TxtBoxTag</tagclass>
….. <bodycontent>EMPTY</bodycontent>
< sharmanj:txtBox length=“10“ name=“userName”/> <attribute>
….. <name>name</name>
<required>true</required>
test.jsp ….
</attribute>
…
</tag>
sharmanj-taglib.tld
<web-app>
<taglib>
<taglib-uri>sharmanjTags</taglib-uri> public class TxtBoxTag extends TagSupport {
<taglib-location> public int doStartTag() {
sharmanj-taglib.tld ….
</taglib-location> }
</taglib> }
</web-app>
TxtBoxTag.java
web.xml
17
Implicit Objects - Introduction
18
Implicit Objects - Example
<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
Implicit Objects – out
An instance of JspWriter
20
Implicit Objects – request
• Instance of HttpServletRequest
a = 10
Helper
Uses helper Object
object
(a = 10)
21
Implicit Objects – response
• Instance of HttpServletResponse
JSP1
response.sendRedirect(“url”);
22
Implicit Objects – session
• Instance of HttpSession
Request 1
Response1
Response2
SESSION 23
Implicit Objects – config and application
24
Servlet Config Servlet Config Servlet Config Servlet Config
Implicit Objects – page
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
25
Implicit Objects – pageContext
application
Least visible to most visible
session
pageContext
request
page
26
JSP Actions
<jsp:include>
<jsp:forward>
<jsp:param>
<jsp:useBean>
<jsp:getProperty>
<jsp:setProperty>
27
JSP Actions - <jsp:include>
Definition:
Includes a page at the given location in the main page
Syntax:
28
JSP Actions – Include Action v/s Include Directive
29
JSP Actions - <jsp:forward>
Definition:
Forwards the request to the given page
Syntax:
30
JSP Actions - <jsp:param>
Definition:
Pass parameters to the included/forwarded page
Syntax:
31
JSP Actions - <jsp:useBean>
getId();
setId(..)
getName()
How can I use
setName(…)
that bean?
bean
Servlet JSP
32
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}"
type=“{package.class}"
beanName=" {package.beanName}”>
</ jsp:useBean>
Definition:
Gets property values of a Bean
Syntax:
<jsp:getProperty name=“{beanInstanceName}"
property= “{propertyName}">
34
JSP Actions - <jsp:setProperty>
Definition:
Sets property values in a Bean
Syntax:
<jsp:setProperty name=“{beanInstanceName}"
property= "*" | property="propertyName"
param=“{parameterName}"
value="{paramValue}" } />
35
JSP Actions – useBean
<jsp:useBean id=“userBean"
scope="session“
class=“com.sharmanj.UserBean" >
<jsp:setProperty name=“userBean”
property=“userName”
value=“Sharad”/>
</ jsp:useBean>
36
Error Handling – Types of errors
• Errors in HTML
38
Error Handling – Error page
39
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>
40
Thanks
41