Mod2 PDF
Mod2 PDF
Mod2 PDF
These code inside <% %> tags are processed by the JSP engine during translation of the
JSP page.
Any other text in the JSP page is considered as HTML code or plain text.
Creating Template Text
JSP document consists of static text (usually HTML), known as template text.
There are two minor exceptions to the “template text is passed straight through” rule.
First, if you want to have <% or %> in the output, you need to put <\% or %\> in the output,
you need to put <\% or %\> in the template text.
Second, if you want a comment to appear in the JSP page but not in the resultant document, use
<%-- JSP COMMENTS --%>
HTML comments of the form are passed through to the client normally
<!-- HTML COMMENTS -->
Invoking java code from JSP
JSP scripting elements allows to insert Java code into the servlet that will be generated f
JSP page. There are three forms:
1. Expressions of the form, which are evaluated and inserted into the servlet’s output.
2. Scriptlets of the form, which are inserted into the servlet’s _jspService method (called
service).
3. Declarations of the form, which are inserted into the body of the servlet class, outside
existing methods.
Put those 25 lines in a separate Java class and put 1 line in the JSP page t
invokes it
Why is the second option much better?
Development- Write the separate class in a Java environment (editor or IDE), not an HTML
environment
Compilation. To compile a regular Java class, you press the Build button in your IDE or invoke
javac.
Debugging- If there are syntax errors, it is immediately seen at compile time.
Testing. You can write a test routine with a loop that does 10,000 tests and reapply it after each
change.
Reuse. You can use the same class from multiple pages
Using jsp expressions
A JSP expression is used to insert values directly into the output. It has the following form.
These Objects are the Java objects that the JSP Container makes available to the
developers in each page and the developer can call them directly without being
explicitly declared.
<!DOCTYPE html>
<html>
<head><title>JSP Page</title></head>
<body> <h1>JSP Expression</h1>
<UL>
<li>Current time: <%= new java.util.Date() %> </li>
<li>Server: <%= application.getServerInfo() %> </li>
<li>Session ID: <%= session.getId() %> </li>
</UL>
</body> </html>
Comparing JSP and Servlets
JSP Servlet
JSP is a web page scripting language that can Servlets are already compiled which also create
generate dynamic content dynamic web content
JSP runs slower compared to servlet as it takes Servlets run faster compared to JSP.
compilation time to convert into java Servlets
It’s easier to code in JSP than in Java Servlets. Its little much code to write here.
<html>
<head>
<title>My First JSP Page</title>
</head>
<body>
Page Count is
<% out.println(++count); %>
</body>
</html>
Using Scriptlets to make parts of jsp
conditional
(a) code inside a scriptlet gets inserted into the resultant servlet’s
_jspService method (called by service) exactly as written and
(b) that any static HTML (template text) before or after a scriptlet
gets converted to print statements.
Using Declaration
A JSP declaration lets you define methods or fields that get inserted
into the main body of the servlet class (outside the _jspService
method)
Declaration is made inside the servlet class but outside the service
method.
Overuse of this approach can lead to JSP code that is hard to understand and maintain.
<html>
<head> <title>My First JSP Page</title>
</head>
<%! int count = 0; %>
<body>
Page Count is:
<% out.println(++count); %>
</body>
</html>
JSP declarations can contain field (instance variable) definitions, method definitions,
inner class definitions, or even static initializer blocks: anything that is legal to put inside
a class definition but outside any existing methods.
Declarations almost always contain field or method definitions.
<H1>Some Heading</H1>
<%!
private String randomHeading() {
return("<H2>" + Math.random() + "</H2>");
}
%>
<%= randomHeading() %>
Declaration Example
JSP directive affects the overall structure of the servlet that results from the JSP
page.
The jsp directives are messages that tells the web container how to translate a
JSP page into the corresponding servlet.
Syntax:
inserts a file into the servlet class at the time the JSP file is translated into a servlet.
An include directive should be placed in the document at the point at which you want the file to
be inserted.
<html>
<body>
</body>
</html>
</body>
</html>
Session Attribute
The session attribute controls whether or not the page participates in HTTP
sessions.
Use of this attribute takes one of the following two forms:
<%@ page session="true" %> <%-- Default --%>
<%@ page session="false" %>
A value of true indicates that the predefined variable session (of type
HttpSession) should be bound to the existing session if one exists; otherwise,
a new session should be created and bound to session.
A value false means that no sessions will be used automatically and
attempts to access session variable
<%@ page session="true" %>
<html>
<head><title>session </title></head>
<body>
<h3>Hello this is a session page directiv
example.</h3>
<%
out.print("Session id:" +
session.getId());
%>
</body>
</html>
isThreadSafe
The buffer attribute specifies the size of the buffer used by the out
variable, which is of type JspWriter (a subclass of PrintWriter). Use of
this attribute takes one of two forms:
Servers can use a larger buffer than you specify, but not a smaller
one.
For example, <%@ page buffer="32kb" %> means the document
content should be buffered and not sent to the client until at least 32
kilobytes have been accumulated or the page is completed.
For example, the following directive directs the JSP translator to generate
the servlet such that the servlet extends somePackage.SomeClass
The info attribute defines a string that can be retrieved from the serv
means of the getServletInfo method.
The errorPage attribute specifies a JSP page that should process any exceptions (i.e.,
something of type Throwable) thrown but not caught in the current page.
It is used as follows:
<%@ page errorPage="Relative URL" %>
The exception thrown will be automatically available to the designated error page by
means of the exception variable.
iserrorPage and language
since java is both the default and the only legal choice
isElignore attribute
The include directive tells the Web Container to copy everything in the
included file and paste it into current JSP file.
Syntax: <%@ include file="filename.jsp" %>
taglib directive
The action tags are used to control the flow between pages and to use Java Bean.
These tags are used to remove or eliminate scriptlet code from JSP page because
scriplet code are technically not recommended nowadays.
It's considered to be bad practice to put java code directly inside your JSP page.
Standard tags begin with the jsp: prefix.
There are many JSP standard Action tag which are used to perform some specific
task.
JSP action tags
Definition
Includes a page at the given location in the main
page
Syntax:
The jsp:forward action tag is used to forward the request to another resource it
may be jsp, html or another resource.
<jsp:forward page="relativeU
RL />
Syntax of jsp:forward action tag with parameter
Definition
Pass parameters to the included/forwarded page
Syntax:
<jsp:include page =“{page_to_include}” flush=“true”>
<jsp:param name=“{param_name}" value=“{param_value }”/>
</jsp:include>
The jsp:plugin element instructs the server to build a tag appropriate for applets that use the
plug-in.
The jsp:plugin Element
The simplest way to use jsp:plugin is to supply four attributes
type
code
width
height
The attribute names are case sensitive, and single or double quotes are always required
around the attribute values.
<APPLET CODE=“MyAppplet.class”
WIDTH=475 HEIGHT=350>
</APPLET>
With
<jsp:plugin type=“applet”
code=“MyApplet.class”
width=“475” height=“350”>
</jsp:plugin>
The jsp:plugin element has a number of other optional attribute
type
code
width
height
codebase
align
hspace
vspace
archive
name
title
Why Use Beans
A no-argument constructor.
For example:
<jsp:useBean
jsp:getProperty. This element reads and outputs the value of a bean prope
Reading a property is a shorthand notation for calling a method of the form
<jsp:getProperty name=“beanName”
property=“propertyName” />
jsp:setProperty
jsp:setProperty. This element modifies a bean property (i.e., calls a method of the form
setXxx).
<jsp:setProperty name="beanName"
property="propertyName"
value="propertyValue" />
There are several ways to use jsp:setProperty, but the best is with property =“*”
Scope (Visibility)
Page
(Least
Visible)
Request
Session
• <jsp:useBean … scope=“request”/>
o Can access from any page
JSP has three main capabilities for including external pieces into a JS
document:
Enter jsp:include, a portable mechanism that lets you insert any of the following into the
JSP output:
The content of an HTML page.
The content of a plain text document.
The output of JSP page.
The output of a servlet
The jsp:include action includes the output of a secondary page at the time the main page is
requested.
The jsp:include action
include the output of a page at request time.
Advantage
it saves you from changing the main page when the included pages change.
Disadvantage
is that since it includes the output of the secondary page, not the secondary
page’s actual code
The include directive
insert JSP code into the main page before that main page is translated into a servlet.
Advantage
Powerful
the included code can contain JSP constructs such as field definitions and content-type
settings that affect the main page as a whole.
Disadvantage
hard to maintain
Jestin Johnson
1AY18MCA12