You are on page 1of 50

Presentation by:

JOSHI
JSP
Java Server Pages
JSP

•JSP (Java Server Pages) technology


provides a simplified, fast way to create
dynamic web content.

•JSP technology enables rapid


development of web-based applications
that are server-independent and
platform-independent.
JSP
•JSP simply puts Java inside HTML pages.

•JSP is being turned into a Java file,


compiled and loaded. This compilation
only happens once, so after the first load,
the file doesn't take long to load anymore.

•Every time you change the JSP file, it will


be re-compiled again.
JSP
Every JSP page will have a corresponding servlet
which is generated by ‘JSP Engine’ (which is a part
of web container and itself is a servlet)
Corresponding servlet for JSP is generated only
once when JSP is requested for the first time.

Request IS No
CLIENT JSP VALID
?
Text
COMPILE
Yes
Response
SERVLET

Class
JSP
A JSP page is a text document havaing tags
which are in two types
2. Display Related Tags
These are simple HTML Tags, sent as it is
to the client.
3. JSP Tags
These Tags are processed at server side
and response will be send to client.
JSP Display Related Tags
<HTML>
<BODY>
Hello, world
</BODY>
</HTML>

save the file with extension of .jsp Ex: Hello.jsp


put the file in JSP folder of Web/Application
Server Load it from the client browser
JSP Tags
It is not useful to just write HTML pages with
a .jsp extension. What makes JSP so useful
is ability to embed Java code in HTML page
<HTML>
<BODY>
Hello! Present time is
<%= new java.util.Date() %>
</BODY>
</HTML>
JSP Tags

SCRIPTING TAGS
DIRECTIVE TAGS
ACTION TAGS
CUSTOM TAGS
Scripting Tags
Scripting Tags
• JSP scripting elements let you insert Java code
into the servlet that will be generated from the
current JSP page.

• Blocks of Java code inside the JSP by placing


your Java code between <% and %>

• Scriptlet doesn't contribute any HTML

• A scriptlet contains Java code that is executed


every time the JSP is invoked.
Scripting Tags

SCRIPTING TAGS are three types

SCRIPTLET TAGS

EXPRESSION TAGS

DECLARATION TAGS
Scriptlet Tag
Scriptlets of the form <% code %> that are
inserted into the servlet's service method
<HTML><BODY>
<%
System.out.println( "Evaluating date now");
java.util.Date date = new java.util.Date();
%>
Hello! Present time is <%= date %>
</BODY></HTML>
Note: Output from "System.out.println" will be on the server.
Scriptlet Tag
<HTML><BODY>
<% System.out.println( "Evaluating date now" );
java.util.Date date = new java.util.Date();
%>
Hello! Present time is
<% out.println( String.valueOf( date ));
%>
</BODY></HTML>

Note: Output from "out.println" will generate HTML.


"out" variable is of type javax.servlet.jsp.JspWriter.
Expression Tag
Expression tags, <%= and %> encloses Java
Expressions, which are evaluated at run time.

<HTML>
<BODY>
Hello! Present time is
<%= new java.util.Date() %>
</BODY>
</HTML>
Declaration Tag
JSP declaration, <%! Java Code %> lets you
define methods or fields that get inserted into
the main body of the servlet class (outside of
the service method processing the request).

<%! private int accessCount = 0; %>


Accesses to page since server reboot:
<%= ++accessCount %>
Declaration Tag
<%@ page import="java.util.*“ %>
<HTML><BODY>
<%!
Date theDate = new Date();
Date getDate()
{
System.out.println( "In getDate() method" );
return theDate;
}
%>
Hello! The time is now <%= getDate() %>
</BODY></HTML>
Mixing Scriptlets and HTML
Example 1:

<TABLE BORDER=2>
<% for ( int i = 0; i < n; i++ )
{ %>
<TR>
<TD> Number </TD>
<TD> <%= i+1 %> </TD>
</TR>
<% }%>
</TABLE>
Mixing Scriptlets and HTML

Example 2:

<% if ( hello )
{%>
<P>Hello, world
<% }else
{%>
<P>Goodbye, world
<% }%>
Directive Tags
Directives
A JSP directive affects the overall structure of
the servlet class. Directives can have either
one attribute or any number of attributes with
values. A JSP "directive" starts with <%@

<%@ directive attribute="value" %>

<%@ directive attribute1="value1“


attribute2="value2“ ...attributeN="valueN" %>
Directives

DIRECTIVE TAGS are three types

PAGE DIRECTIVE

INCLUDE DIRECTIVE

TAGLIB DIRECTIVE
Page Directives
This contain the list of imported packages.
<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%System.out.println( "Evaluating date now" );
Date date = new Date(); %>
Hello! The time is
now <%= date %>
</BODY>
</HTML>

To import more than one package :


<%@ page import="java.util.*, java.text.*" %>
Include Directives
These directives are used to physically include the
contents of any file like HTML or JSP or anything.
It is like JSP file actually contained the included text.

<HTML><BODY>
Going to include hello.jsp...

<BR> <%@ include file="hello.jsp" %>


</BODY></HTML>

View this JSP in your browser, and you will see your
original hello.jsp get included in the new JSP.
Taglib Directives
JSP 1.1 introduces a method of extending authors
defined JSP tags, called "tag libraries".
Each taglib will have taglib specific
documentation. To use the tag library, use the
"taglib" directive to specify where tag library's
"description" resides.
Example:
<%@ taglib prefix="blx" uri="/blx.tld" %>
The "uri" specifies where to find the tag library
description. The "prefix" is unique for the tag
library. This directive is shows that we are using
Action Tags
Action Tags
ACTION TAGS are three types

FORWARD ACTION

INCLUDE ACTION

USEBEAN ACTION
Forward Action
•This action lets you forward the request
to another page.
•It has a single attribute, page, which
should consist of a relative URL. This
could be a static value, or could be
computed at request time
Syntax:
<jsp:forward page="relative URL"/>
<jsp:forward page="<%= JavaExpression %>" />
Example:
<jsp:forward page="/utils/errorReporter.jsp" />
Include Action

This action inserts the file at the time


the page is requested but not at the
time the JSP page is translated into a
servlet as like Action Directives

syntax:

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


Include Action
<HTML>
<BODY>
<P> Our 4 most recent news stories:
<OL>
<LI><jsp:include page="news/Item1.html" flush="true"/>
<LI><jsp:include page="news/Item2.html" flush="true"/>
<LI><jsp:include page="news/Item3.html" flush="true"/>
<LI><jsp:include page="news/Item4.html" flush="true"/>
</OL>
</BODY>
</HTML>
UseBean Action
This action lets you load in a JavaBean to be used in
the JSP page.
This is a very useful capability of reusability of Java
classes without sacrificing the convenience that JSP
adds over servlets alone.
syntax:
<jsp:useBean id="name" class="package.class" />
This will instantiate an object of the class specified
by class, and bind it to a variable with the name
specified by id.
Attributes: id, class, scope, type, beanName
Bean Class
package hall;
public class SimpleBean
{
private String message = “Hello, Chakradhar";

public String getMessage()


{ return(message); }

public void setMessage(String message)


{ this.message = message; }
}
UseBean Action
<HTML><BODY>
<jsp:useBean id="test” class="hall.SimpleBean" />
<jsp:setProperty name="test”
property="message“ value="Hello WWW" />

<H1> Message:
<jsp:getProperty name="test“ property="message" />
</H1>
</BODY></HTML>
jsp:setProperty
It is used to sets values to properties of beans in two ways

1. use it after, but outside of a jsp:useBean element, which is


executed regardless of whether a new bean was
instantiated or an existing bean was found.
<jsp:useBean id="myName" ... />...
<jsp:setProperty name="myName” property=“anyProperty”… />
</jsp:useBean>

9. appears inside the body of a jsp:useBean element, which


is executed only if a new object was instantiated, not if an
existing one was found.
<jsp:useBean id="myName" ... > ...
<jsp:setProperty name="myName” property=“anyProperty”… />
</jsp:useBean>
jsp:getProperty
• This element retrieves the value of a bean
property, converts it to a string, and inserts it into
the output.

• The two required attributes are name of a bean,


and property whose value should be inserted.

<jsp:useBean id="itemBean" ... />...


<UL>
<LI>Number of items:
<jsp:getProperty name="itemBean" property="numItems" />
<LI>Cost of each:
<jsp:getProperty name="itemBean" property="unitCost" />
</UL>
jsp:plugin
Generate browser-specific code that makes
an OBJECT or EMBED tag for the Java
plugin
This action lets you insert the browser-
specific OBJECT or EMBED element
needed to specify that the browser run an
applet using the Java plugin

<jsp:plugin attribute="value"*>
.....
</jsp:plugin>
Custom Tags
Custom Tags

JSP provides a tag


library which allows us
to write our own tags
within the context of the
page.
JSP IMPLICIT OBJECTS
REQUEST
•This is the HttpServletRequest associated with the
request, and lets you look at the request parameters
via getParameter, the request type like GET, POST,
HEAD, etc., and the incoming HTTP headers like
cookies, Referer, etc..
•Request is a subclass of ServletRequest

<HTML><BODY>
<% out.println( "<BR>Your machine's address is " );
out.println( request.getRemoteHost()); %>
</BODY></HTML>
RESPONSE
•Response can be used to affect the
response being sent to the browser.
response.sendRedirect( “anotherUrl” );
•It sends a response to the browser that it
should load a different URL. The browser
will then send a different request, to
"anotherUrl".
•There is another mechanisms for including
another page or forwarding the browser to
another page called include page.
OUT
•This is the PrintWriter used to send output
to the client.
•This is a buffered version of PrintWriter
called JspWriter.
•Note that you can adjust the buffer size, or
even turn buffering off, through use of the
buffer attribute of the page directive.
•out is used almost exclusively in scriptlets,
since JSP expressions automatically get
placed in the output stream
SESSION
This is the HttpSession object associated
with the request.

Sessions are created automatically, so this


variable is bound even if there was no
incoming session reference.

Raises an exception in translation of the JSP


page to servlet when trying to refer to a
variable of session when it is turned off
APPLICATION

It is the ServletContext as obtained via

getServletConfig().getContext().
CONFIG

It is the ServletConfig object for


the page.
PAGE CONTEXT
JSP introduced a new class called
PageContext to encapsulate use of
server-specific features like higher
performance JspWriters.

When server specific features are


accessed through this class rather
than directly, code will still run on
"regular" servlet/JSP engines.
PAGE

This is simply a synonym for ‘this’.

It is not very useful in Java.

It is created as a placeholder for


the time when the scripting
language could be something other
than Java.
COMMENTS &
CHARACTER QUOTING
CONVENTIONS
COMMENTS
<%-- comment --%>
A JSP comment. Ignored by JSP-to-Scriptlet
translator. Any embedded JSP scripting
elements, directives, or actions are ignored.

<!-- comment -->


An HTML comment. Passed through to
resultant HTML. Any embedded JSP scripting
elements, directives, or actions are executed
normally.
CHARACTER QUOTING CONVENTIONS

<\%
To get “<%” character in text (static HTML)
%\>
To get “%>” character in scripting elements
\'
To get a single quote in an attribute
\"
To get a double quote in an attribute

You might also like