Java Server Pages
Java Server Pages
Directives
• Affect the overall structure of the servlet class generated from this JSP
• format: <%@ directive attribute="value" %>
• There are two main types of directives: page & include
Actions
• Control the behavior of the servlet engine
• You can dynamically insert a file, reuse JavaBeans components
• Available actions include:
• (1) jsp:include (2) jsp:forward (3) jsp:useBean
JSP Scriptlet tag (Scripting elements)
<html>
<body>
Current Time: <
%= java.util.Calendar.getInstance().getTime() %
>
</body>
</html>
JSP expression tag that prints the user name
JSP Declaration Tag
• The JSP declaration tag is used to declare
fields and methods.
• The code written inside the jsp declaration tag
is placed outside the service() method of auto
generated servlet.
• So it doesn't get memory at each request.
<%! field or method declaration %>
Difference between JSP Scriptlet tag and
Declaration tag
JSP declaration tag that declares field
JSP declaration tag that declares method
JSP Implicit Objects
• There are 9 jsp implicit objects. These objects
are created by the web container that are
available to all the jsp pages.
JSP out implicit object
• For writing any data to the buffer, JSP
provides an implicit object named out. It is the
object of JspWriter.
PrintWriter out=response.getWriter();
JSP request implicit object
• The JSP request is an implicit object of type
HttpServletRequest i.e. created for each jsp
request by the web container. 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.
JSP response implicit object
• In JSP, response is an implicit object of type
HttpServletResponse. The instance of
HttpServletResponse is created by the web
container for each jsp request.
• It can be used to add or manipulate response
such as redirect response to another resource,
send error etc.
Example of response implicit object
JSP config implicit object
• In JSP, config is an implicit object of
type ServletConfig. This object can be used to
get initialization parameter for a particular JSP
page. The config object is created by the web
container for each jsp page.
Example of config implicit object:
JSP application implicit object
• In JSP, application is an implicit object of
type ServletContext.
• The instance of ServletContext is created only once by the
web container when application or project is deployed on
the server.
• This object can be used to get initialization parameter
from configuaration file (web.xml). It can also be used to
get, set or remove attribute from the application scope.
• This initialization parameter can be used by all jsp pages.
Example of application implicit object
session implicit object
• In JSP, session is an implicit object of type
HttpSession.The Java developer can use this
object to set,get or remove attribute or to get
session information.
Example of session implicit object
Welcome.jsp
Second.jsp
pageContext implicit object
In JSP, pageContext is an implicit object of type
PageContext class.The pageContext object can
be used to set,get or remove attribute from one
of the following scopes:
• page
• request
• session
• application
In JSP, page scope is the default scope.
Example of pageContext implicit object
Welcome.jsp
<html>
<body>
<%
String name=(String)pageContext.getAttribute("user",PageContex
t.SESSION_SCOPE);
out.print("Hello "+name);
%>
</body>
</html>
page implicit object
• In JSP, page is an implicit object of type Object
class.This object is assigned to the reference of auto
generated servlet class. It is written as:
Object page=this;
• For using this object it must be cast to Servlet type.For
example:
<% (HttpServlet)page.log("message"); %>
• Since, it is of type Object it is less used because you
can use this object directly in jsp.For example:
<% this.log("message"); %>
exception implicit object
• In JSP, exception is an implicit object of type
java.lang.Throwable class. This object can be
used to print the exception. But it can only be
used in error pages.It is better to learn it after
page directive.
Example of exception implicit object
Directives
• Affect the overall structure of the servlet class
generated
from this JSP
23
Actions
• Control the behavior of the servlet engine
<body bgcolor="white">
<jsp:include page="nav.jsp"
/>
</body>
<html>
Actions: The jsp:forward Action
• Forwards a client request to an HTML file, JSP file,
or servlet for processing
• Syntax
<jsp:forward page= “relativeURL" />
The forward element Example
<%
double freeMem = Runtime.getRuntime().freeMemory();
double totlMem = Runtime.getRuntime().totalMemory();
double percent = freeMem/totlMem;
<jsp:forward page="ex3.jsp"/>
<% } %>