Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 55

JavaServer Pages

(JSP)

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Sunil Pratap Singh UNIT - II
What is JSP?
• JSP is a server-side technology used to create dynamically generated Web
pages.
• JSP pages are opposite of Servlets as a servlet adds HTML code inside Java
code, while JSP adds Java code inside HTML using JSP tags.
• Everything a Servlet can do, a JSP page can also do it.
• The main advantages of JSP includes Easy to Learn and Implement, Scalability,
and Platform Independent (JSP is built on Java technology).

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 2
Advantages of JSP over Servlet
• JSP provides an easier way to code dynamic web pages.
• JSP does not require additional files like, java class files, web.xml, etc.
• There is automatic deployment of a JSP, recompilation is done automatically
when changes are made to JSP pages.
• JSP pages can be directly accessed, and web.xml mapping is not required like
in servlets.
• Allows us to use separate presentation logic (html code) from Java code
(business logic).
• Servlets use println statements for printing an HTML document which is usually
very difficult to use. JSP has no such tedious task to maintain.
• JSP pages can be used in combination with servlets that handle the business logic.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 3
JSP becomes Servlet
• JSP pages are converted into Servlet by the Web Container.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 4
Life Cycle of JSP
• A JSP page is converted into Servlet in order to service requests.
• The translation of a JSP page to a Servlet is called Lifecycle of JSP.
• JSP Lifecycle is exactly same as the Servlet Lifecycle, with one additional first
step, which is, translation of JSP code to Servlet code.
• Lifecycle Steps:
1) Translation of JSP to servlet code
2) Compilation of servlet to bytecode
3) Loading servlet class
4) Creating servlet instance
5) Initialization by calling jspInit() method
6) Request processing by calling _jspService() method
7) Destroying by calling destroy() method
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 5
Life Cycle of JSP

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 6
Conversion of JSP into Servlet

JSP Code (Name: index.jsp)

Servlet
Code

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 7
JSP Scripting Element
• JSP Scripting element are written inside <% %> tags.
• The 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.
• Removing the <% %> tag from the code and run it as JSP will print
everything as it is on the browser.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 8
Types of Scripting Elements
• Comment (<%-- Comment --%>)
• Scriptlet (<% Scriplets %>)
• Directive (<%@ Directive %>)
• Declaration (<%! Declarations %>)
• Expression (<%= Expression %>)

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 9
Comment Tag
• Comments are added with the purpose of making the source code easier for
humans to understand.
• JSP comments are only seen in the JSP page.
• These comments are not included in servlet source code during translation
phase, nor they appear in the HTTP response.
• Adding comments in your code is considered to be a good practice in
Programming world
• Syntax of JSP Comment Tag:
<%-- JSP Comment --%>

• Example of JSP Comment Tag:


<%-- Function Calling --%>

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 10
Scriptlet Tag
• Scriptlet tag allows us to write java code inside JSP page.
• Scriptlet tag implements the _jspService() method functionality by
writing script/java code.
• Syntax of Scriptlet Tag:
<% Java Code %>

• Examples of Scriptlet Tag:


<% int count = 0; %>
<% String uId = request.getParameter(“userId”); %>

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 11
Scriptlet Tag (contd..)
• Mixing Scriptlet Tags with HTML Tags: (Example)

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 12
Declaration Tag
• When a variable or method is declared inside Declaration Tag, it means the
declaration is made inside the Servlet class but outside the service (or any
other) method.
• Static member, instance variable and methods can be declared inside
Declaration Tag.
• Syntax of Declaration Tag:
<%! Declaration Code %>
• Example of Declaration Tag:
<%! int count = 0; %>
• If we declare a variable using Scriptlet Tag, it will come inside the service
method.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 13
When to use Declaration/Scriptlet Tag
• Methods and variables declared inside the Declaration Tag becomes
instance methods and instance variables and are also assigned default
values.
• Anything added inside Scriptlet Tag, goes inside the _jspservice()
method.
• We cannot add any function inside the Scriptlet Tag, as on compilation it
will try to create a function inside the service method which is not possible
in Java (method inside a method is not allowed).

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 14
Directive Tag
• Directive Tag gives special instruction to Web Container at the time of
page translation.
• Types of Directive Tags:
 Page - defines page dependent properties such as language, session, errorPage
etc.

 Include - defines file to be included

 Taglib - declares tag library used in the page

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 15
Directive Tag: Page Directive
• The Page Directive defines a number of page dependent properties which
communicates with the Web Container at the time of translation.
• Syntax of Page Directive:
<%@ page attribute = “value” %>, where attribute can be:
 import
 language
 extends
 session
 isErrorPage
 errorPage
 contentType
 info
 buffer
 autoFlush
 isThreadSafe
 isELIgnored
 isScriptingEnabled
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 16
Page Directive: import attribute
• The import attribute defines the set of classes and packages that must
be imported in servlet class definition.
• Examples:
<%@ page import = "java.util.Date" %>

<%@ page import = "java.util.Date, java.net.*" %>

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 17
Page Directive: language attribute
• The language attribute tells the container which language is being used
to write the JSP page.
Case Sensitive
• Examples:
<%@ page language = “java” %>
• The default language is Java and for this reason, there is no need to
write this attribute.
• In future, if the designers would like to give support for other language,
then language attribute might come into usage.
• As on today, no other language is supported by JSP.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 18
Page Directive: extends attribute
• The extends attribute specifies a parent class that will be inherited by
the generated servlet.
• Examples:
<%@ page extends = “packName.className” %>
• It is rarely used.
• Note: This may not work as the Servlet class generated by the container
extends the class HttpJspBase  HttpServlet  GenericServlet  Object.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 19
Page Directive: session attribute
• session attribute defines whether the JSP page is participating in an
HTTP session.
 It takes a Boolean value of true or false.
 If set to true, the session is maintained in JSP.
 Is set to false, the session is not maintained.
 Default is true; that is, session is implicitly maintained with every JSP page.

• Example:
<%@ page session = “true” %>

<%Object o1 = session.getAttribute(“uName”)%>

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 20
Page Directive: isErrorPage attribute
• JSP provide 3 different ways to perform exception handling:
 Using isErrorPage and errorPage attribute of page directive.
 Using <error-page> tag in Deployment Descriptor.
 Using simple try...catch block.

• isErrorPage attribute in page directive officially appoints a JSP page as an


error page.
• Examples: Error.jsp

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 21
Page Directive: errorPage attribute
• errorPage attribute indicates another JSP page that will handle all the
run time exceptions thrown by current JSP page.
• It specifies the URL path of another page to which a request is to be
dispatched to handle run time exceptions thrown by current JSP page.

• Examples: Error.jsp

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 22
Error Page in Deployment Descriptor
Examples:
… … … … … … …
<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/Error.jsp</location>
</error-page>
… … … … … … …

… … … … … … …
<error-page>
<error-code>404</error-code>
<location>/Error.jsp</location>
</error-page>
… … … … … … …

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 23
Page Directive: contentType attribute
• contentType attribute defines the MIME type for the JSP response.
• Examples:
<%@ page contentType = “text/html” %>

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 24
Page Directive: info attribute
• Programmer uses info attribute to give some information or description
about the JSP page like: when the project started, Programmers
involved, functionality of the page, etc.
• Example:
<%@ page info = “Proj Started on 11.09.17” %>
• The information given as string for info attribute can be accessed with
getServletInfo() method.
• The web container will create a method getServletInfo() in the resulting
servlet. For example:
public String getServletInfo() {
return "Proj Started on 11.09.17";
}

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 25
Page Directive: buffer attribute
• JSP first writes the response data to the buffer and when the buffer is full, the
data is sent to client.
• When the PrintWriter object is closed with out.close(), the unfilled buffer is
flushed to the client.
• Using buffer page directive attribute, the buffer size can be increased or
decreased or set to none (means no buffer is maintained)
• If not set by the Programmer, the default buffer size is 8 KB.
• Syntax: <%@ page buffer = “integer/none” %>
• Example:
<%@ page buffer = “5kb” %>
<%@ page buffer = “none” %> - buffer is not maintained or set to
zero. The response data is immediately written to output stream of client. This kills
performance and increases network traffic.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 26
Page Directive: autoFlush attribute
• autoFlush indicates the container to flush the data or not when the buffer
gets filled to be sent to client.
• It takes Boolean values of true or false.
• If not set by the Programmer, the default value is true, indicating flushing is
required.
• When set to false, the autoFlush raises exception when the buffer is full.
• Syntax: <%@ page autoFlush = “true/false” %>
 <%@ page autoFlush = “true” %> - It is the default value. When the
buffer is full, the data is flushed out to the output stream of the response object
to send to client. This is done by the container implicitly.
 <%@ page autoFlush = “false” %> - The container raises exception
when the autoFlush is set to false, when buffer gets filled up.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 27
Page Directive: isThreadSafe attribute
• This attribute takes Boolean values of true or false indicating the JSP page can
be safe in multithreaded environment or not. The default value is true.
• When set to true, the same JSP page can honor multiple clients.
 When a client requests a JSP file, the container loads the JSP file, translates it to a Servlet, compiled
and then executed. For multiple clients’ requests also, the container creates multiple objects of the
same Servlet and honors. For each Servlet object, a separate service() method is created. That is, each
client will have a separate service() method. For this reason, Servlets and JSP are implicitly thread safe.

• If the Programmer would like to create only one object for the Servlet/JSP
(and not multiple objects), then declare false.
 That is, multiple clients request the same JSP (or Servlet), multiple files of the same JSP are loaded and
executed. This decreases the performance. This type coding is required when the data is very
important and critical like money transfer operations in banks.

• Syntax:
<%@ page isThreadSafe = “true/false” %>
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 28
Page Directive: isELIgnored attribute
• The isELIgnored attribute value informs the container whether to
evaluate or execute EL (Expression Language) expression present in the
JSP or not.
• It takes Boolean values of true or false.
• If not set by the Programmer, the default value is true, indicating that
EL expressions are ignored.
• Example:
<%@ page isELIgnored = “true” %>

• While using EL in JSP, this attribute should be set to false else not
executed.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 29
Page Directive: isScriptingEnabled attribute

• JSP code contains scriptlets, expressions and declarations.

• isScriptingEnabled takes Boolean values of true or false.

 By default, this attribute is set to true and means these elements (non-EL)
are recognized by the container.

 When set to false, if scriptlet etc. are used in code, the container raises
translation-time error.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 30
Directive Tag: Include Directive
• 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” %>
• Example:
One more standard application of include directive
is, if you create a separate JSP file, with some
commonly used functions, kind of like a utility JSP
file. Which can be included in the web pages
wherever you want to use those functions.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 31
Directive Tag: Taglib Directive
• The taglib directive is used to define tag library that the current JSP page uses.
• A JSP page might include several tag library.
• JavaServer Pages Standard Tag Library (JSTL), is a collection of useful JSP tags,
which provides many commonly used core functionalities.
• It has support for many general, structural tasks such as iteration and
conditionals, readymade tags for manipulating XML documents,
internationalization tags, and for performing SQL operations.
• Syntax:
<%@ taglib prefix="prefixOfTag" uri="uriOfTagLibrary" %>
• The prefix is used to distinguish the custom tag from other library custom tag.
• You can name the prefix anything, but it should be unique. Every custom tag must
have a prefix.
• The URI is the unique name for Tag Library.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 32
Expression Tag
• Expression Tag is used to print out java language expression that is put
between the tags.
• An expression tag can hold any java language expression that can be
used as an argument to the out.print() method.
• Syntax of Expression Tag:
<%= Expression %>
• Example of Expression Tag:
<%= (2*5) %>
 out.print((2*5));
• Never end an expression with semicolon inside Expression Tag.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 33
Implicit Objects in JSP
• JSP provide access to some implicit object which represent some
commonly used objects for servlets that JSP page developers might
need to use.
• For example, we can retrieve HTML form parameter data by using
request variable, which represents the HttpServletRequest object.
Implicit object associated with
HttpServletRequest

<% String user = request.getParameter(“user”); %>

Implicit object associated with


JspWriter

Hello <% out.println(user); %>

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 34
JSP Implicit Objects

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 35
Standard Action (Action Tags)
• JSP provides a bunch of standard action tags used to perform some
specific tasks such as:
 working with java bean objects,
 including other resource,
 forward the request to other resource, etc.
• Action elements are basically predefined functions.

• Syntax:
<jsp:actionName attribute = “value”>

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 36
List of Standard Actions
• jsp:include
• jsp:forward
• jsp:plugin
• jsp:useBean
• jsp:getProperty
• jsp:setProperty

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 37
JSP Action – jsp:include
• The <jsp:include> standard action is used to include resource into the current
JSP page at request time.
• The included resource can be static (HTMP page) or dynamic (JSP page or
servlet).
• Syntax: <jsp:include page=“url” />
• When the servlet container encounters a <jsp:include> action, it
requests the included page then adds the response (not source code)
into the current JSP page, where the <jsp:include> action is declared.
• Nested inclusion can also be done.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 38
JSP Action - jsp:include (Example)
• Date.jsp

• Main.jsp

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 39
JSP Action - jsp:include (Example)
• Output

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 40
Difference b/w include Directive and include Action

• JSP include Directive:


 Includes resource at translation time
 better for static pages
 includes the original content in the generated servlet

• JSP include Action:


 includes resource at request time
 better for dynamic pages
 calls the include method

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 41
JSP Action - jsp:forward
• The <jsp:forward> standard action terminates the action of current page and
forwards the request to another resource such as a static page, another JSP
page, or a servlet.
• Syntax: <jsp:forward page=“url” />

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 42
JSP Action - jsp:forward (Example)
• Date.jsp

• Main.jsp

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 43
JSP Action - jsp:forward (Example)
• Output

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 44
JSP Action - jsp:plugin
• The <jsp:plugin> action is used to insert Java components
(Applet/JavaBean) into a JSP page. It determines the type of browser
and inserts the <object> or <embed> tags as needed.
• If the needed plugin is not present, it downloads the plugin and then
executes the Java component.
• The plugin action has several attributes that correspond to common
HTML tags used to format Java components.
• The <param> element can also be used to send parameters to the
Applet or Bean.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 45
JSP Action - jsp:plugin
• Syntax:

• The <param> element can also be used to send parameters to the Applet or
Bean.
• The <fallback> element, can be used to specify an error string to be sent to
the user in case the component fails
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 46
JavaBean Components
• Any Java class that follows certain design conventions is a JavaBeans
component.
• A Java class with the following features is called a JavaBean:
 A no-argument constructor
 Properties defined with accessors and mutators (getter and setter method)
 Class must not define any public instance variables
 The class must implement the java.io.Serializable interface

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 47
Example of a JavaBean

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 48
JSP Action - jsp:useBean
• Using a JavaBean in JSP page
 JavaBeans can be used in any JSP page using the <jsp:useBean> tag
 Example:
<jsp:useBean id=“myBeanAttribute” class=“myPack.MyBean”
scope=“request | page | session | application”/>
 Once the bean is defined in JSP, we can get it’s properties using jsp:getProperty.
 We can use jsp:setProperty to set the property values of a java bean.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 49
JSP Action - jsp:useBean (Example)
• Calculator.java

• BeanEx.jsp

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 50
JSP Action - jsp:setProperty
• The jsp:setProperty action tag sets a property value or values in a bean using
the setter method.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 51
JSP Action - jsp:getProperty
• The jsp:getProperty action tag returns the value of the property.

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 52
Example
• TestBean.java

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 53
• Test.jsp

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 54
Output
Got Message …
Hello JSP

©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh U2. ‹#›U2. 55

You might also like