JSP (Unit 5)
JSP (Unit 5)
JSP (Unit 5)
A. Directives
B. Scripting Elements
C. JSP Actions
(B) Scripting Elements
4 types of scripting elements defined:
• Declaration
• Expression
• Scriptlets
• Comments
(B-1)Declaration
Declares a variable or method valid in the scripting language
used in the JSP page.
JSP Syntax
<%! declaration; [ declaration; ]+ ... %>
Examples
%>
<%= "Cube of 3 is:"+cube(3) %>
</body> </html>
(B-2) Expression
Syntax
<%= expression %>
Description
The code placed within JSP expression tag is written to the output
stream of the response. So you need not write out.print() to
write data. It is mainly used to print the values of variable or
method.
Example
Welcome, <%=userName%>
Output:
Welcome, Jay
Cont… Expression
<%= new java.util.Date()%>
When this is generated into servlet code, the resulting
java stmt will probably look like this:
out.print(new java.util.Date());
<html> <body>
Current Time: <%=
java.util.Calendar.getInstance().getTime()
%>
</body>
</html>
(B-3) Script lets
It is used to execute java source code in JSP.
Syntax : <% java source code %> OR
<jsp:scriptlet> code fragment
</jsp:scriptlet>
Scriptlets allows you to include a series of java
statements inside the _jspService method
that are executed on every request to the
page. These java stmts are incorporated into
_jspService method as it is.
JSP scriptlet tag
• Used to execute java source code in JSP
– Syntax: <% java source code %>
Welcome.html:-
<html>
<body>
<form action="welcome.jsp">
Welcome.jsp:-
<input type="text" name="uname"> <html>
<body>
<input type="submit" value="go"> <%
<br/> String
</form> </body> name=request.getParameter(“uname
</html> ”);
Out.print(“welcome”+name);
%>
</body> </html>
(B-4) Comments
To denote any lines you want to be completely
ignored by the JSP translation process.
Example
<%-- Author: James Gosling --%>
(A) JSP Directives
They are the elements of a JSP source code that
guide the web container on how to translate
the JSP page into it’s respective servlet.
Syntax:
<%@ directive attribute = "value"%>
There are three main directives defined in jsp:
• Page
• Include
• Taglib
(A-1) Page Directive
It is used to define the properties applying the JSP page,
such as the size of the allocated buffer, imported
packages and classes/interfaces, defining what type of
page it is etc.
Syntax
<%@page attribute = "value"%>
OR
<jsp:directive.page pageDirectiveAttrList />
Examples
<%@ page import="java.util.*, java.lang.*" %>
<jsp:directive.page errorPage="error.jsp" />
Cont.. Page Directive
Description
• It applies to an entire JSP page and any of its
static include files, which together are called a
translation unit.
Examples:include.jsp:
<html> <head>
<title>An Include Test</title> </head>
<body bgcolor="white">
<font color="blue">
The current date and time are <%@ include file="date.jsp" %>
</font> </body> </html>
Cont… Include Directive
date.jsp
<%@ page import="java.util.*" %>
<%= (new java.util.Date() ).toLocaleString() %>
Description
An include directive inserts a file of text or code in a JSP page at
translation time, when the JSP page is compiled. When you use the
include directive, the include process is static. A static include
means that the text of the included file is added to the JSP page. The
included file can be a JSP page, HTML file, XML document, or text
file. If the included file is a JSP page, its JSP elements are translated
and included (along with any other text) in the JSP page. Once the
included file is translated and included, the translation process
resumes with the next line of the including JSP page.
(A-3) taglib directive
Used to mention the library whose custom-defined tags are being used
in the JSP page. It’s major application is JSTL(JSP standard tag
library).
Syntax:
<%@taglib uri = "library url" prefix="the prefix to identify the tags of this library
with"%>
Examples
<%@ taglib uri="http://www.jspcentral.com/tags" prefix=“abc" %>
Description
• The taglib directive declares that the JSP page uses custom tags, names
the tag library that defines them, and specifies their tag prefix.
• You must use a taglib directive before you use the custom tag in a JSP
page. You can use more than one taglib directive in a JSP page, but the
prefix defined in each must be unique.
(C) JSP ACTIONS
The JSP specification provides a standard tag called Action tag used
within JSP code and is used to remove or eliminate Scriptlet code
from your JSP code as JSP Scriptlets are obsolete and are not
considered nowadays. There are many JSP action tags or elements,
and each of them has its own uses and characteristics. Each JSP
action tag is implemented to perform some precise tasks.
Here is the list of JSP Actions:
jsp:forward
The jsp:forward tag is used to forward the request and response to
another JSP or servlet. In this case the request never return to the
calling JSP page. (internally uses RequestDispatcher forward()
method)
jsp:param/ jsp:params
The jsp:param action is used to add the specific parameter to
current request. The jsp:param tag can be used inside a jsp:include,
jsp:forward or jsp:params block.
Cont.. JSP ACTIONS
jsp:include
Work as a subroutine, the Java servlet temporarily passes
the request and response to the specified JSP/Servlet.
Control is then returned back to the current JSP page.
jsp:useBean
Used to instantiate an object of Java Bean or it can re-use
existing java bean object.
jsp:getProperty
Used to get specified property from the JavaBean object.
jsp:setProperty
Used to set a property in the JavaBean object.
jsp:forward
• The <jsp:forward> element forwards the request object containing the
client request information from one JSP page to another resource.
• The target resource can be an HTML file, another JSP page, or a
servlet, as long as it is in the same application context as the forwarding
JSP page. The lines in the source JSP page after the <jsp:forward>
element are not processed.
Attributes
page="{relativeURL | <%= expression %>}"
– A String or an expression representing the relative URL of the
component to which you are forwarding the request. The component
can be another JSP page, a servlet, or any other object that can
respond to a request.
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
1) JSP out implicit object
• To write data on output stream
Prog 1: Welcome.jsp:- Prog 2: ShowDate.jsp:-
<html> <html> <body>
<body> <% out.print("Today
<%
is:"+java.util.Calendar.getI
Out.print(“welcome to JSP”);
%>
nstance().getTime()); %>
</body> </body>
</html> </html>
2- JSP request object
• It can be used to get request information such as parameter, header
information, remote address, server name, server port, content type,
character encoding etc.
• It can also be used to set, get and remove attributes from the jsp
request scope. Welcome.jsp:-
<html>
Welcome.html:-
<body>
<html>
<%
<body>
<form action="welcome.jsp"> String
<input type="text" name="uname"> name=request.getParameter(“uname”);
Out.print(“welcome”+name);
<input type="submit" value="go"> %>
<br/>
</form> </body> </html>
</body> </html>
3- The response Object:
• It can be used to add or manipulate response such
as redirect response to another resource, send error
etc. Welcome.jsp:-
Welcome.html:- <html>
<html> <body>
<body> <%
<form action="welcome.jsp"> response.sendRedirect(“http:
<input type="text" name="uname">
//google.com”)
<input type="submit" value="go"> %>
<br/>
</form> </body>
</body> </html>
</html>
4- Application Object
Second.jsp:-
<html>
<body>
<%
String name=(String)session.getAttribute("user");
out.print("Hello "+name);
%>
</body>
</html>