Professional Documents
Culture Documents
JOSHI
JSP
Java Server Pages
JSP
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>
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.
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>
<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).
<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 <%@
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>
<HTML><BODY>
Going to include hello.jsp...
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
syntax:
<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
<jsp:plugin attribute="value"*>
.....
</jsp:plugin>
Custom Tags
Custom Tags
<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.
getServletConfig().getContext().
CONFIG
<\%
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