--------------------------------------------------------------------------------------------------------------------------
Developed By : Uday Shah (HOD - IT)
E-Mail : rupareleducation@gmail.com
Contact No : 7600044051
Advanced Java Programming (J2EE)
JSP Introduction
·
It stands for Java Server Pages.
·
It is a server side technology.
·
It is used for creating web application.
·
It is used to create dynamic web content.
·
JSP tags are used to insert JAVA code into HTML pages.
·
It is an advanced version of Servlet Technology.
·
It is a Web based technology helps us to create dynamic
and platform independent web pages.
·
They are easy to maintain.
·
No recompilation or redeployment is required.
·
JSP has access to entire API of JAVA.
·
JSP are extended version of Servlet.
·
Using JSP, one can easily separate Presentation and
Business logic.
·
JSP pages are converted into Servlet by
the Web Container.
·
JSP pages can be directly accessed, and web.xml mapping
is not required like in servlets.
·
JSP is built on Java technology, so it is platform
independent.
Difference
between JSP V/s Servlet
JSP
|
SERVLET
|
JSP
is a html based code
|
Servlet is a java based code.
|
It
is easy to code as it is java in html.
|
Writing code for servlet is harder than JSP as it is html
in java.
|
JSP
is the view in MVC Architecture for showing output.
|
Servlet plays a controller role in MVC Architecture.
|
JSP
is slower than Servlet.
|
Servlet is faster than JSP.
|
JSP
only accept http requests.
|
Servlet can accept all protocol requests.
|
In
JSP, we cannot override its service() method.
|
In Servlet, we can override the service() method.
|
JSP
Architecture
·
The web server needs a JSP engine, i.e, a container to
process JSP pages.
·
The JSP container is responsible for intercepting
requests for JSP pages.
·
A JSP container works with the Web server to provide the
runtime environment and other services a JSP needs.
·
Java Server Pages are part of a 3-tier architecture.
·
JSP contains both Java and HTML in the same file.
·
JSP can be easily managed because business logic is
separated from presentation logic.
·
The following diagram shows the JSP architecture.
·
According to above architecture.
·
As with a normal page, your browser sends an HTTP request
to the web server.
·
The web server recognizes that the HTTP request is for a
JSP page and forwards it to a JSP engine.
·
This is done by using the URL or JSP page which ends
with .jsp instead of .html.
·
The JSP engine loads the JSP page from disk and converts
it into a servlet content.
·
This conversion is very simple in which all template text
is converted to println( ) statements and all JSP elements are converted to
Java code.
·
The JSP engine compiles the servlet into an executable
class and forwards the original request to a servlet engine.
·
And finally, the web browser handles the
dynamically-generated HTML page inside the HTTP response exactly like static
page
JSP Life Cycle
·
JSP Life Cycle is like the servlet, each JAVA EE part
will have its own particular life cycle.
·
A JSP life cycle can be portrayed as the entire system
from its introduction till the decimation which resembles a servlet life cycle
with an additional step which is required to arrange a JSP into the servlet.
·
A JSP life cycle is defined as the process from its
creation till the destruction.
·
This is similar to a servlet life cycle with an
additional step which is required to compile a JSP into servlet.
·
Following steps are involved in JSP life cycle:
·
Translation of JSP page to Servlet
·
Compilation of JSP page
·
Class loading
·
Instantiation means Object of the generated Servlet is
created.
·
Initialization means jspInit() method is invoked by the
container.
·
Request processing jspService() is invoked by the container.
·
JSP Cleanup jspDestroy() method is invoked by the
container.
JSP Elements
·
JSP Scripting element are written inside <%
%> tags.
·
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.
·
There are five different types of scripting elements
Scripting Element
|
Example
|
Comment
·
JSP Comment is used when you are creating a JSP page
and want to put in comments about what you are doing.
·
JSP comments are only seen in the JSP page.
|
<%--
comment --%>
|
Directive
Directive
Tag gives special instruction to Web Container at the time of page
translation. Directive tags are of three
types: page, include and taglib.
<%@ 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
|
<%@
directive %>
|
Declaration
- A
declaration tag is a piece of Java code for
declaring variables, methods and classes.
- If we
declare a variable or method inside declaration tag it means that the
declaration is made inside the servlet class but outside the service
method.
|
<%!
declarations %>
|
Scriptlet
·
Scriptlet Tag allows you to write java code inside JSP
page.
- A
Scriptlet contains java code that is executed every time JSP is invoked.
|
<%
scriplets %>
|
Expression
- Expression
tag evaluates the expression placed in it.
- It
accesses the data stored in stored application.
- It
allows create expressions like arithmetic and logical.
- It
produces scriptless JSP page.
|
<%=
expression %>
|
JSP Action
Tags
- The
action tags are used to control the flow between pages and to use Java
Bean.
- The Jsp
action tags are given below
- jsp:param
: sets the parameter value. It is used in forward and include mostly.
- jsp:include
: includes another resource.
- jsp:forward:
forwards the request and response to another resource.
- jsp:plugin:
embeds another components such as applet.
|
<jsp:include page="printdate.jsp" />
<jsp:forward page="printdate.jsp" />
|
JSP Implicit Objects
- 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.
- JSP Implicit Objects are also
called pre-defined variables.
- There are 9 jsp implicit
objects. These objects are created by the web container that is
available to all the jsp pages.
No.
|
Description
|
1
|
Request: This is
the HttpServletRequest object associated with the request.
|
2
|
Response: This is
the HttpServletResponse object associated with the response to the
client.
|
3
|
Out: This is the PrintWriter object
used to send output to the client.
|
4
|
Session: This is the HttpSession object
associated with the request.
|
5
|
Application: This is
the ServletContext object associated with the application context.
|
6
|
Config: This is the ServletConfig object
associated with the page.
|
7
|
PageContext: This encapsulates use of
server-specific features like higher performance JspWriters.
|
8
|
Page: This
is simply a synonym for this, and is used to call the methods defined by
the translated servlet class.
|
9
|
Exception: The Exception object
allows the exception data to be accessed by designated JSP.
|
JSP Scope
·
The
availability of a JSP object for use from a particular place of the application
is defined as the scope of that JSP object.
·
Every
object created in a JSP page will have a scope.
·
Object
scope in JSP is segregated into four parts and they are page, request, session
and application.
Page
·
Page
scope means the JSP object can be accessed only from within the same page where
it was created.
·
The
default scope for JSP objects created using <jsp:useBean> tag is page.
·
JSP
implicit objects out, exception, response, pageContext, config and page have page
scope.
Request
- A JSP
object created using the request scope can be accessed from any pages that
serve that request.
- More than
one page can serve a single request.
- The JSP
object will be bound to the request object.
- Implicit
object request has the request scope.
Session
·
Session
scope means, the JSP object is accessible from pages that belong to the same
session from where it was created.
·
The
JSP object that is created using the session scope is bound to the session
object.
·
Implicit
object session has the session scope.
Application
- A JSP
object created using the application scope can be accessed from any pages
across the application.
- The JSP
object is bound to the application object.
- Implicit
object application has the application scope.
Including and Forwarding
from JSP Pages
- These
actions use constructs in XML syntax to control the behavior of the
servlet engine.
- You can
dynamically insert a file, reuse JavaBeans components, forward the user to
another page, or generate HTML for the Java plugin.
- Following
is syntax
<jsp:action_name
attribute = "value" />
- According
to above there are two attributes that are common to all Action elements.
- Thai is id attribute
and the scope attribute.
Id attribute
- The id
attribute uniquely identifies the Action element, and allows the action to
be referenced inside the JSP page.
- If the
Action creates an instance of an object, the id value can be used to
reference it through the implicit object.
Scope attribute
- This attribute identifies the lifecycle of the Action element.
- The id attribute and the scope attribute are directly related, as the
scope attribute determines the lifetime of the object associated with the
id.
- The scope attribute has four possible values that is
·
Page
·
Request
·
Session
·
Application
Jsp : include Action
- The jsp:
include action tag is used to include the content of another resource
it may be jsp, html or servlet.
- The jsp
include action tag includes the resource at request time so it
is better for dynamic pages because there might be changes in future.
- The jsp:
include tag can be used to include static as well as dynamic pages.
- Main
Advantages of JSP code is reusability .
- We can use
a page many times such as including header and footer pages in all pages.
So it saves a lot of time.
Working with Session
& Cookie in JSP
·
Cookies are text files stored on the client
computer and they are kept for various information tracking purposes.
·
JSP
transparently supports HTTP cookies using underlying servlet technology.
·
There
are three steps involved in identifying and returning users
·
Server
script sends a set of cookies to the browser. For example, name, age, or
identification number, etc.
·
Browser
stores this information on the local machine for future use.
·
When
the next time the browser sends any request to the web server then it sends
those cookies information to the server and server uses that information to
identify the user or may be for some other purpose as well.
·
In
Session JSP makes use of the servlet provided HttpSession Interface.
·
This
interface provides a way to identify a user across.
·
By
default, JSPs have session tracking enabled and a new HttpSession object is
instantiated for each new client automatically
·
The
JSP engine exposes the HttpSession object to the JSP author through the
implicit session object.
·
Since session object
is already provided to the JSP programmer, the programmer can immediately begin
storing and retrieving data from the object without any initialization or getSession
().
Error Handling and
Exception Handling with JSP
·
The
exception is normally an object that is thrown at runtime.
·
Exception
Handling is the process to handle the runtime errors.
·
There
may occur exception any time in your web application.
·
So
handling exceptions is a safer side for the web developer.
·
In
JSP, there are different ways to perform exception handling.
·
Using Error Page
·
JSP
gives you an option to specify Error Page for each JSP. Whenever the
page throws an exception, the JSP container automatically invokes the error
page.
·
To
set up an error page, use the <%@ page errorPage = "xxx"
%> directive.
·
And
for error-handling page includes the directive <%@ page isErrorPage =
"true" %>
·
Using JSTL Tags
·
You
can make use of JSTL tags to write an error page ShowError.jsp.
·
This
page has almost same logic as in the ErrorPage, with better structure and more
information
·
In
this case, either specifies exception-type or error-code with the location
element.
·
If
you want to handle all the exception, you will have to specify the
java.lang.Exception in the exception-type element.
·
Using Try...Catch Block
·
If
you want to handle errors within the same page and want to take some action
instead of firing an error page, you can make use of
the try....catch block.
·
Exception
handling in JSP is same as in java where we manage exceptions using try catch
blocks.
:: Best Of Luck ::
https://myshopprime.com/uday.shah3/shop
Please share and comment it. Thank You...