Java EE - Presentation

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 100

iPrimed Education Solutions Pvt. Ltd.

#62B, Electronics City, Phase-I, Opposite ECT PO,


Bangalore, Karnataka– 560100
India
(T) 91 - 80 65832406 / 91 - 80 40941642
www.iprimed.com

JAVAEE PRESENTATION
Index
1. Introduction to Advance Java 6. Http Session

2. What is need of Advance Java 7. URL Rewriting

3. Web and Http 8. Hidden Fields


9. Cookies
4. Introduction to Servlet
10. JSP: Introduction
5. Servlet Life Cycle
11. Different phases of JSP life cycle
6. Steps for creating Servlet
12. JSP APIs
7. Servlet in Eclipse
13. Creating a JSP Page
8. Client-side data processing using
14. JSP Scripting Element
Servlet
15. JSP Implicit Objects
9. Java EE: Welcome-file list 16. JSP Include Directive
10. What is Servler-Request 17. JSP Taglib Directive
11. Request Dispatcher 18. Exception Handling
12. Session Tracking with Servlet 19. Declaring error page in Deployment Descriptor
13. Need of Session Tracking

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Index
1. Error Handling with Try-Catch block 1. JSTL
a) JSTL Core Tags
2. JSP Action Tags
b) JSTL Function Tags
a) jsp:forward
c) JSTL Formatting Tags
b) jsp:include
d) JSTL SQL Tags
c) jsp:useBean
3. jsp:ExpressionLanguage
a) El param
b) EL Session
c) El Cookies
4. Precedence of Operations in EL
5. Reserve words in EL

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Java EE: Introduction

Introduction to Advanced Java:

Advanced Java is everything that goes beyond Core Java.

The APIs defined in Java Enterprise Edition, includes Servlet programming, Web Services,
the Persistence API, etc.

It is a Web & Enterprise application development platform which basically follows client &
server architecture.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Java EE: Introduction

Advanced Java Tutorial: Need for Advance Java:

Below I have listed down few major advantages of Advance Java:

Advance Java i.e. JEE (Java Enterprise Edition) gives you the library to understand the
Client-Server architecture for Web Application Development which Core Java doesn’t
support.

J2EE is platform Independent, Java Centric environment for developing, building &
deploying Web-based applications online. It also consists of a set of services, APIs, and
protocols, which provides the functionality that is necessary for developing multi-tiered,
web-based applications.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Java EE: Introduction

Advanced Java Tutorial: Need for Advance Java:

There are a lot of Advance Java frameworks like Spring, JSF, Struts etc. which enable you
to develop a secure transaction based web apps for the domains like E-Commerce,
Banking, Legal, Financial, Healthcare, Inventory etc.

To work and understand the hot technologies like Hadoop and Cloud services, you should
be prepared with core and advanced Java concepts

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Java EE: Introduction

Web and HTTP:

Web is a system of Internet servers that supports formatted documents.

The documents are formatted using a markup language called HTML (HyperText Markup
Language) that supports links to other documents like graphics, audio, and video files etc.

So, a website is a collection of static files i.e. web pages like HTML pages, images, graphics
etc.

The Web application is a website with dynamic functionality on the server. Google,
Facebook, Twitter are examples of web applications.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Java EE: Introduction

Web and HTTP:

HTTP (Hypertext Transfer Protocol):

HTTP is used by clients and servers to communicate on the web. It is considered as a


stateless protocol because it supports only one request per connection.

With HTTP the clients connect to the server to send one request and then disconnect.
And this mechanism allows more users to connect to a given server over a period of time.

Here, the client sends an HTTP request and the server answers with an HTML page to the
client, using HTTP.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Java EE: Introduction to Servlet

Introduction to Servlet:

A servlet is a Java programming language class that is used to extend the capabilities of
servers that host applications accessed by means of a request-response programming
model. The Servlet interface is the root interface of the servlet class hierarchy. All
Servlets need to either directly or indirectly implement the Servlet interface.

Although servlets can respond to


any type of request, they are
commonly used to extend the
applications hosted by web
servers.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Java EE: Introduction to Servlet

Introduction to Servlet:

Servlet can be described as follows:

• Servlet is a technology which is used to create a web application.

• It is an API that provides many interfaces and classes including documentation.

• Servlet is an interface that must be implemented for creating any Servlet.

• It is also a class that extends the capabilities of the servers and responds to the
incoming requests. It can respond to any requests.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Java EE: Servlet Lifecycle

Advanced Java Tutorial: Servlet Life Cycle

The entire life cycle of a Servlet is managed by the Servlet container which uses the
javax.servlet.Servlet interface to understand the Servlet object and manage it.

Stages of the Servlet Life Cycle: The Servlet life cycle mainly goes through four stages,

• Instantiating a Servlet.
• Initializing the Servlet.
• Request handling
• Destroying the Servlet.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Steps to create Servlet

Advanced Java Tutorial: Steps to create Servlet

Steps to Create Servlet

• Create a directory structure


• Create a Servlet
• Compile the Servlet
• Add mappings to web.xml file
• Start the server and deploy the project
• Access the servlet

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Steps to create Servlet

Advanced Java Tutorial: Steps to create Servlet

To run a servlet program, we should have  Servlet Container installed and configured.


Here we will use Apache Tomcat Server.

Eclipse for Java EE provides in-built Apache Tomcat.

Once the server is configured, you can start with your program.

One important point to note – for any servlet program, you need 3 files – index.html file,
Java class file, and web.xml file.
Though index.html is not mandatory. And web.xml can also be replaced by using
annotations. Here we will consider the generic structure of the project. 

The very first step is to create a Dynamic Web Project and then proceed further.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet in Eclipse

Servlet in Eclipse:

Click on New

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet in Eclipse

Servlet in Eclipse:

Enter the name, and click on


finish. 

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Client-side Data Processing with Servlet

Processing the client-side data through servlet:

Now, based on the above steps let’s write a program and understand how servlet works.

• Step 1: To run a servlet program, we should have Apache tomcat server installed and
configured. Once the server is configured, you can start with your program.

• Step 2: For a servlet program, you need 3 files – index.html file, Java class file, and
web.xml file. The very first step is to create Dynamic Web Project and then proceed
further.

• Step 3: Now let’s see how to add 2 numbers using servlets and display the output in
the browser.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Client-side Data Processing with Servlet

Processing the client-side data through servlet:

First, I will write index.html file

Step 4: Now without the Java class file, you can’t perform addition on 2 numbers. So let’s
write a class file.

Above program creates a form to enter the numbers for the addition operation.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Client-side Data Processing with
Servlet
Processing the client-side data through servlet:

Step 4: Now without the Java class file, you can’t perform addition on 2 numbers. So let’s
write a class file.

Step 5: After writing the Java class file, the last step is to add mappings to the web.xml
file. Let’s see how to do that.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Client-side Data Processing with
Servlet
Processing the client-side data through servlet:

Step 6: web.xml file will be present in the WEB-INF folder of your web content. If it is not
present, then you can click on Deployment Descriptor and click on Generate Deployment
Descriptor Stub.

Step 7: After that, you can proceed further and add the mappings to it.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Client-side Data Processing with
Servlet
Processing the client-side data through servlet:

Step 8: Run the program and enter two values you want to add.

Step 9: Fill up the form, click the submit button to trigger the servlet.

Step 10.: You get the desired output on the browser.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Java EE: Welcome-file-list

Welcome-file-list

The welcome-file-list element of web-app, is used to define a list of welcome files.

Its sub element is welcome-file that is used to define the welcome file.

A welcome file is the file that is invoked automatically by the server, if you don't specify
any file name.

By default server looks for the welcome file in following order:


• welcome-file-list in web.xml
• index.html
• index.htm
• index.jsp
If none of these files are found, server renders 404 error.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Java EE: Welcome-file-list

Welcome-file-list

Lets see how welcome-file-list is given:

A welcome file is the file that is invoked automatically by the server.

By default server looks for the welcome file in following order:


• welcome-file-list in web.xml
• index.html
• index.htm
• index.jsp
If none of these files are found, server renders 404 error.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Request

What is Servlet Request?

Lets see how welcome-file-list is given:

An object of ServletRequest is used to provide the client request information to a servlet


such as content type, content length, parameter names and values, header informations,
attributes etc.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Request

Servlet Request Example:

Index.html

Output:

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Request

Servlet Request Example:

Show servlet After submitting:

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Request Dispatcher

Request Dispatcher

The RequestDispatcher interface provides the facility of dispatching the request to


another resource it may be html, servlet or jsp.

This interface can also be used to include the content of another resource also. It is one of
the way of servlet collaboration.

There are two methods defined in the RequestDispatcher interface.

public void forward(ServletRequest request,ServletResponse response)

public void include(ServletRequest request,ServletResponse response)

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Request Dispatcher

Request Dispatcher Example:


Show Servlet
Index.html

Output:

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

Session Tracking with Servlet:

Session simply means a particular interval of time.

Session Tracking is a way to maintain state (data) of an user. It is also known as session
management in servlet.

Http protocol is a stateless so we need to maintain state using session tracking


techniques.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

Need of Session Tracking:

There are four techniques used in Session tracking:

• Cookies
• Hidden Form Field
• URL Rewriting
• HttpSession

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

HttpSession:

The HttpServletRequest interface provides two methods to get the object of HttpSession:

public HttpSession getSession():Returns the current session associated with this request,
or if the request does not have a session, creates one.

public HttpSession getSession(boolean create):


Returns the current HttpSession associated with this
request or, if there is no current session and create is
true, returns a new session.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

HttpSession Example:

Login page

request

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

HttpSession Example:
Login page

Next page

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

HttpSession Example:
Login page

Next page

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

URLRewriting:

In URL rewriting, we append a token or identifier to the URL of the next Servlet or the
next resource.

We can send parameter name/value pairs using the following format:


url?name1=value1&name2=value2&??

Advantages

It will always work whether cookie is disabled or not (browser independent).

Extra form submission is not required on each pages.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

URLRewriting Example:

Extra form submission is not required on each pages.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

HiddenFields:

A hidden (invisible) textfield is used for maintaining the state of an user.

We store the information in the hidden field and get it from another servlet. This
approach is better if we have to submit form in all the pages and we don't want to depend
on the browser.

<input type="hidden" name="uname" value=“Abc">

Extra form submission is not required on each pages.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

HiddenFields Example:

Login form

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

Cookies:

A cookie is a small piece of information that is persisted between the multiple client
requests.

A cookie has a name, a single value, and optional attributes such as a comment, path and
domain qualifiers, a maximum age, and a version number.

There are 2 types of cookies in servlets.


1. Non-persistent cookie 2. Persistent cookie

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
Servlet Session Tracking

Cookies Example:

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Introduction

Introduction:

Java Server Pages (JSP) is a technology which is used to develop web pages by inserting
Java code into the HTML pages by making special JSP tags.

The JSP tags which allow java code to be included into it are <% —-java code—-%>.

JSP can consist of either HTML or XML (combination of both is also possible) with JSP
actions and commands.

This can also be used to access JavaBeans objects.

JSP can be used for separation of the view layer with the business logic in the web
application.
Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Introduction

Different phases of JSP Life Cycle

• Translation of JSP page

• Compilation of JSP page(Compilation of JSP page into _jsp.java)

• Classloading (_jsp.java is converted to class file _jsp.class)

• Instantiation(Object of generated servlet is created)

• Initialisation(_jspinit() method is invoked by container)

• Request Processing(_jspservice() method is invoked by the container)

• Destroy (_jspDestroy() method invoked by the container)


Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Introduction

Different phases of JSP Life Cycle

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Introduction

Different phases of JSP Life Cycle

Web Container translates JSP code into a servlet class source(.java) file, then
compiles that into a java servlet class.

In the third step, the servlet class bytecode is loaded using classloader. The
Container then creates an instance of that servlet class.

The initialized servlet can now service request. For each request the Web
Container call the _jspService() method.

When the Container removes the servlet instance from service, it calls the
jspDestroy() method to perform any required clean up.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Introduction

JSP API

The JSP API consists of two packages:

• javax.servlet.jsp

• javax.servlet.jsp.tagext

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Creating a JSP Page

Creating a JSP page

A JSP page looks similar to an HTML page, but a JSP page also has Java code in it.

We can put any regular Java Code in a JSP file using a scriplet tag which start with
<% and ends with %>.

JSP pages are used to develop dynamic responses.

Next: How to create JSP page in Eclipse.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Creating a JSP Page

Creating a JSP page

Right-click on WebContent  New  JSP File.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Creating a JSP Page

Creating a JSP page

Your JSP page looks like below. We will cover the details later.

Write something in your JSP file. The complete HTML and the JSP code, goes
inside the <body> tag, just like HTML pages.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Creating a JSP Page

Creating a JSP page

To run your project, right click on the page, select Run As → Run on Server

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Scripting Element

JSP Scripting Element

JSP Scripting element are written inside <% %> tags.

These code inside <% %> tags are processed by the JSP engine during translation
of the JSP page.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Scripting Element

JSP Scripting Element

There are five different types of scripting elements.


Scripting Element Example

Comment <%-- comment --%>

Directive <%@ directive %>

Declaration <%! declarations %>

Scriptlet <% scriplets %>

Expression <%= expression %>

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Implicit Object

JSP Implicit Object

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 you can retrieve HTML form parameter data by using request
variable, which represent the HttpServletRequest object.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Implicit Object

JSP Implicit Object

JSP provide access to some implicit object which represent some commonly used
objects for servlets that JSP page developers might need to use.
Implicit Object Description
request The HttpServletRequest object associated with the request.
response The HttpServletRequest object associated with the response that is sent back to the browser.
out The JspWriter object associated with the output stream of the response.
session The HttpSession object associated with the session for the given user of request.
application The ServletContext object for the web application.
config The ServletConfig object associated with the servlet for current JSP page.
pageContext The PageContext object that encapsulates the enviroment of a single request for this current JSP
page
page The page variable is equivalent to this variable of Java programming language.
exception The exception object represents the Throwable object that was thrown by some other JSP page.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Include Directive

JSP Include Directive

The include directive tells the Web Container to copy everything in the included


file and paste it into current JSP file. 

Syntax of include directive is:

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Include Directive

JSP Include Directive Example:

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Taglib Directive

JSP Taglib Directive:

The JSP taglib directive is used to define a tag library that defines many tags.

We use the TLD (Tag Library Descriptor) file to define the tags.

In the custom tag section we will use this tag so it will be better to learn it in custom tag.

<%@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>  

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Taglib Directive

JSP Taglib Directive:

The JSP taglib directive is used to define a tag library that defines many tags.

We use the TLD (Tag Library Descriptor) file to define the tags.

In the custom tag section we will use this tag so it will be better to learn it in custom tag.

<%@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>  

The prefix is used to distinguish the custom tag from other libary custom tag.

Prefix is prepended to the custom tag name. Every custom tag must have a prefix.

The URI is the unique name for Tag Library.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Taglib Directive

JSP Taglib Directive:

You can name the prefix anything, but it should be unique.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Taglib Directive

JSP Taglib Directive:

JSP: Using Taglib Directive


To use the JSTL in your application you must have the jstl.jar in your webapps
/WEB-INF/lib directory. Download the jar file from
http://tomcat.apache.org/taglibs/index.html
There are many readymade JST Libraries available which you use to make your life easier.

• Core Tags - URI → http://java.sun.com/jsp/jstl/core


• Formatting Tags - URI → http://java.sun.com/jsp/jstl/fmt
• SQL Tags - URI → http://java.sun.com/jsp/jstl/sql
• XML Tags - URI → http://java.sun.com/jsp/jstl/xml
• JSTL Functions - URI → http://java.sun.com/jsp/jstl/functions

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Taglib Directive

JSP Taglib Directive Example:

<html>
<body>

<%@ taglib uri="http://www.javatpoint.com/tags" prefix="mytag" %>

<mytag:currentDate/>

</body>
</html>

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Exception Handling

Exception Handling

Exception Handling is a process of handling exceptional condition that might occur in


your application.

Ways to perform Exception Handling in JSP:

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.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Exception Handling

Exception Handling

Exception Handling is a process of handling exceptional condition that might occur in


your application.

Example of isErrorPage and errorPage attribute:

isErrorPage attribute in page directive officially appoints a JSP page as an error page.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Exception Handling

Declaring error page in Deployment Descriptor:

You can also declare error pages in the DD for the entire Web Apllication. Using <error-
page> tag in the Deployment Descriptor.

You can even configure different error pages for different exception types, or HTTP error
code type(503, 500 etc).

Declaring an error page for all type of exception.

<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Exception Handling

Error Handling with try-catch block

This will show the


error message in
console.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Action Tags

JSP Action Tags:

There are many JSP action tags or elements. Each JSP action tag is used to perform some
specific tasks.

The action tags are used to control the flow between pages and to use Java Bean.

JSP Action Tags Description


jsp:forward forwards the request and response to another resource.

jsp:include includes another resource.


jsp:useBean creates or locates bean object.
jsp:setProperty sets the value of property in bean object.
jsp:getProperty prints the value of property of the bean.
jsp:plugin embeds another components such as applet.

jsp:param sets the parameter value. It is used in forward and include mostly.

jsp:fallback can be used to print the message if plugin is working. It is used in jsp:plugin.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Action Tags

JSP:forward Action Tags:

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="relativeURL | <%= expression %>" />

<jsp:forward page="relativeURL | <%= expression %>">  
<jsp:param name="parametername" value="parametervalue | <%=expression%>" />  
</jsp:forward>  

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Action Tags

JSP:forward Action Tags:

The jsp:forward action tag is used to forward the request to another resource it may be
jsp, html or another resource.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Action Tags

Jsp:include Action Tags:

Include action tag is used for including another resource to the current JSP page. The
included resource can be a static page in HTML, JSP page or Servlet. We can also pass
parameters and their values to the resource which we are including. 

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Action Tags

Jsp:useBean Action Tags:

The useBean action is quite versatile. It first searches for an existing object utilizing the id
and scope variables. If an object is not found, it then tries to create the specified object.

The simplest way to load a bean is as follows −

Syntax of jsp:useBean:
<jsp: useBean id="unique_name_to_identify_bean"
class="package_name.class_name" />
Syntax of jsp:setProperty:
<jsp:setProperty name="unique_name_to_identify_bean" property="property_name" />
Syntax of jsp:getProperty:
<jsp:getProperty name="unique_name_to_identify_bean" property="property_name" />

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: Action Tags

Jsp:useBean Action Tags:

Example:

Output

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: ExpressionLanguage

JSP: Expression Language

The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean
component, and other objects like request, session, application etc.

There are many implicit objects, operators and reserve words in EL. ${ expression }
Implicit Objects Usage

pageScope it maps the given attribute name with the value set in the page scope

requestScope it maps the given attribute name with the value set in the request scope

sessionScope it maps the given attribute name with the value set in the session scope

applicationScope it maps the given attribute name with the value set in the application scope

param it maps the request parameter to the single value

paramValues it maps the request parameter to an array of values

header it maps the request header name to the single value

headerValues it maps the request header name to an array of values

cookie it maps the given cookie name to the cookie value

initParam it maps the initialization parameter

pageContext it provides access to many objects request, session etc.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: ExpressionLanguage

EL param example:

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: ExpressionLanguage

EL session example:

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: ExpressionLanguage

EL cookie example:

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: ExpressionLanguage

Precedence of Operators in EL

[] .
()
-(unary) not ! empty
* / div % mod
+ - (binary)
< <= > >= lt le gt ge
== != eq ne
&& and
|| or
?:

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: ExpressionLanguage

Reserve words in EL

There are many reserve words in the Expression Language. They are as follows:

lt le gt ge
eq ne true false
and or not instanceof
div mod empty null

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: ExpressionLanguage

Reserve words in EL

There are many reserve words in the Expression Language. They are as follows:

lt le gt ge
eq ne true false
and or not instanceof
div mod empty null

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL (JSP Standard Tag Library)

The JSP Standard Tag Library (JSTL) represents a set of tags to simplify the JSP
development.

JSTL Core Tags:

The JSTL core tag provides variable support, URL management, flow control etc. The
syntax used for including JSTL core library in your JSP is:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL tags

Tags Description
c:out It display the result of an expression, similar to the way <%=...%> tag work.
c:import It Retrives relative or an absolute URL and display the contents to either a String in 'var',a Reader in 'varReader' or
the page.
c:set It sets the result of an expression under evaluation in a 'scope' variable.

c:remove It is used for removing the specified scoped variable from a particular scope.

c:catch It is used for Catches any Throwable exceptions that occurs in the body.
c:if It is conditional tag used for testing the condition and display the body content only if the expression evaluates is
true.
c:choose, c:when, c:otherw It is the simple conditional tag that includes its body content if the evaluated condition is true.
ise
c:forEach It is the basic iteration tag. It repeats the nested body content for fixed number of times or over collection.

c:forTokens It iterates over tokens which is separated by the supplied delimeters.


c:param It adds a parameter in a containing 'import' tag's URL.
c:redirect It redirects the browser to a new URL and supports the context-relative URLs.

c:url It creates a URL with optional query parameters.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL <c:out>

The <c:out> tag is similar to JSP expression tag, but it can only be used with expression. It
will display the result of an expression, similar to the way < %=...% > work.

The < c:out > tag automatically escape the XML tags. Hence they aren't evaluated as
actual tags.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL <c:import>

The <c:import> is similar to jsp 'include', with an additional feature of including the
content of any resource either within server or outside the server.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL <c:set>

The <c:set> tag is JSTL-friendly version of the setProperty action. The tag is helpful
because it evaluates an expression and uses the results to set a value of a JavaBean or a
java.util.Map object.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL <c:remove>

JSTL <c:catch>

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL <c:choose> ,<c:when> and <c:otherwise>

JSTL <c:forEach>

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL <c:forTokens>

JSTL <c:redirect>

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL Function Tags

The JSTL function provides a number of standard functions, most of these functions are
common string manipulation functions.

The syntax used for including JSTL function library in your JSP is:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>


JSTL Functions Description
fn:contains() It is used to test if an input string containing the specified substring in a program.

fn:containsIgnoreCase() It is used to test if an input string contains the specified substring as a case insensitive way.

fn:endsWith() It is used to test if an input string ends with the specified suffix.
fn:escapeXml() It escapes the characters that would be interpreted as XML markup.

fn:indexOf() It returns an index within a string of first occurrence of a specified substring.

fn:trim() It removes the blank spaces from both the ends of a string.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL Function Tags

fn:split() It splits the string into an array of substrings.


fn:toLowerCase() It converts all the characters of a string to lower case.

fn:toUpperCase() It converts all the characters of a string to upper case.

fn:substring() It returns the subset of a string according to the given start and end position.

fn:substringAfter() It returns the subset of string after a specific substring.

fn:substringBefore() It returns the subset of string before a specific substring.

fn:length() It returns the number of characters inside a string, or the number of items in a collection.

fn:replace() It replaces all the occurrence of a string with another string sequence.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

contains

containsIgnoreCase

endsWith

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

indexOf

trim

startsWith

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

split

toLowerCase

toUpperCase

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

split

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL Formatting Tags

The formatting tags provide support for message formatting, number and date
formatting etc.

The url for the formatting tags is http://java.sun.com/jsp/jstl/fmt and prefix is fmt.

The JSTL formatting tags are used for internationalized web sites to display and format
text, the time, the date and numbers.

The syntax used for including JSTL formatting library in your JSP is:

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL Formatting Tags

Formatting Tags Descriptions


fmt:parseNumber It is used to Parses the string representation of a currency,
percentage or number.
fmt:timeZone It specifies a parsing action nested in its body or the time zone for
any time formatting.
fmt:formatNumber It is used to format the numerical value with specific format or
precision.
fmt:parseDate It parses the string representation of a time and date.

fmt:bundle It is used for creating the ResourceBundle objects which will be


used by their tag body.
fmt:setTimeZone It stores the time zone inside a time zone configuration variable.

fmt:setBundle It loads the resource bundle and stores it in a bundle configuration


variable or the named scoped variable.

fmt:message It display an internationalized message.

fmt:formatDate It formats the time and/or date using the supplied pattern and styles.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL Formatting Tags

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL Formatting Tags

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL Formatting Tags

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL SQL Tags

The JSTL sql tags provide SQL support. The url for the sql tags is

The SQL tag library allows the tag to interact with RDBMSs (Relational Databases) such as
Microsoft SQL Server, mySQL, or Oracle.

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql " prefix=«sql» %>

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL SQL Tags

SQL Tags Descriptions


sql:setDataSource It is used for creating a simple data source suitable only for prototyping.

sql:query It is used for executing the SQL query defined in its sql attribute or the body.

sql:update It is used for executing the SQL update defined in its sql attribute or in the tag body.

sql:param It is used for sets the parameter in an SQL statement to the specified value.

sql:dateParam It is used for sets the parameter in an SQL statement to a specified java.util.Date value.

sql:transaction It is used to provide the nested database action with a common connection.

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL SQL Tags

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
JSP: JSTL

JSTL SQL Tags

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .
THANK YOU

Copyright © 2009 iPrimed, all rights reserved. This presentation contains information and data that is confidential and proprietary to iPRIMED .

You might also like