0% found this document useful (0 votes)
22 views55 pages

Java Server Pages

Uploaded by

farhanamfk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views55 pages

Java Server Pages

Uploaded by

farhanamfk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

JSP

Java Server Pages


JSP
• JSP stands for Java Server Pages, a technology
invented by Sun to allow the easy creation of
server side HTML pages.
• JSP lies in the presentation tier on the web
server, with the main responsibility of generating
HTML content that needs to be served to the
browser. It also has the additional responsibility
of pass on the requests to the backend through
the JavaBeans, as and when required
Why JSP?
1. Extension to servlet
2. Convenient to code and maintain as against
servlets
3. Fast development
4. Powered by Java
– Has access to all Java APIs
– Has access to all J2EE APIsa
– Inherent Platform independence
The Architecture: The flow of JSP request
1. The HTTP request comes to a web server.
2. The server extension receives the request (as it is not a request
for static HTML but for a JSP) and passes it on to the JSP
engine.
3. The JSP engine invokes the JSP parser to parse the JSP and
check for any syntax errors.
4. On successful parsing, the JSP engine invokes the Java
compiler to compile the JSP into an equivalent servlet class.
5. Once the class is generated, the control is passed on to a servlet
engine.
6. The servlet engine loads the servlet into memory using its class
loader.
7. The appropriate methods in the servlet are invoked and the
response is routed back to the browser via the server extension
and web server.
JSP Execution Procedure
simple JSP Page
JSP Life Cycle
The JSP API
• The JSP API consists of two packages:
– javax.servlet.jsp
– javax.servlet.jsp.tagext
javax.servlet.jsp package
The javax.servlet.jsp package has two interfaces and classes.
The two interfaces are as follows:
• JspPage
• HttpJspPage
The classes are as follows:
• JspWriter
• PageContext
• JspFactory
• JspEngineInfo
• JspException
• JspError
JspPage interface
All the generated servlet classes must implement
the JspPage interface. It extends the Servlet
interface. It provides two life cycle methods.
public void jspInit()
public void jspDestroy():
The HttpJspPage interface
The HttpJspPage interface provides the one life
cycle method of JSP. It extends the JspPage
interface.
public void _jspService():
Elements ofJSP
Scripting elements

• Expressions: Useful shorthand for printing out strings and contents of


variables.
• Scriptlets: Lets you insert any valid Java code into the JSP.
• Declarations: Useful for declaring page wide variables and
methods(Java) or functions.

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)

JSP Scripting elements


The scripting elements provides the ability to
insert java code inside the jsp.
There are three types of scripting elements:
• scriptlet tag
• expression tag
• declaration tag
JSP scriptlet tag
<% java source code %>
JSP scriptlet tag that prints the user name
JSP expression tag
• The code placed within JSP expression
tag is written to the output stream of the
response
<%= statement %>
JSP expression tag
JSP expression tag that prints current time

<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

• format: <%@ directive attribute="value" %>

• There are two main types of directives:


– page
– include
Directives: Page Directives
• Defines attributes that apply to an entire JSP page

• Lets you do things like:


– Import classes
– Handle error messages
– Define if the JSP is thread-safe
– Define is session object is available
– Set the page content type
Directives: The Include Directive
• Inserts the contents of another file in the main JSP file,
where the directive is located

• Useful for including copyright information, scripting


language files, or anything you might want to reuse in
other applications

• The included file can be an HTML file, a JSP file, a text


file, or a code file written in the Java programming
language

23
Actions
• Control the behavior of the servlet engine

• You can dynamically insert a file, JavaBeans


reuse components

• Available actions include:


– jsp:include
– jsp:forward
– jsp:useBean
Actions: The jsp:include Action
• Lets you insert files into the page being generated

• The syntax looks like this:

<jsp:include page="relative URL" />

• Unlike the include directive, which inserts the file at the


time the JSP page is translated into a servlet, this action
inserts the file at the time the page is requested
The include element Example
<%@ page language="java"
contentType="text/html" %>
<html>
<head><title>Include Action
Demo</title></head>

<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;

if (percent < 0.5) {


%>
<jsp:forward page="ex2.jsp"/>

<% } else { %>

<jsp:forward page="ex3.jsp"/>

<% } %>

You might also like