Date post: | 30-May-2018 |
Category: |
Documents |
Upload: | biswabrata-banerjee |
View: | 226 times |
Download: | 0 times |
of 17
8/9/2019 JSP part1
1/17
Java Server Pages Technology(Part-1)
Biswabrata Banerjee
8/9/2019 JSP part1
2/17
What is this JSP anyway !?
JavaServer Pages (JSP) is a java technology that provides a simplified,fast way to create dynamic web content. JSP technology enables rapiddevelopment of web-based applications that are server- and platform-independent.
It helps software developers serve dynamically generated web pagesbased on HTML, XML, or other document types. Released in 1999 as Sun's
answer to ASP and PHP, JSP Specification was designed to address theperception that the Java programming environment didn't providedevelopers with enough support for the Web.
JSP , like other Java APIs, is a specification by Sun for vendors toimplement. The JSP specification builds on the functionality provided by theservlet specification
Biswabrata Banerjee
8/9/2019 JSP part1
3/17
A sample JSP page (code ref-1.1):-
Biswabrata Banerjee
The Current Date and Time is:
Today.jsp
8/9/2019 JSP part1
4/17
JSP can dynamically invoke other JSPs
Biswabrata Banerjee
JSP
JSP
servlet
JSP
TranslatedClient
8/9/2019 JSP part1
5/17
Biswabrata Banerjee
8/9/2019 JSP part1
6/17
JSP architecture
Biswabrata Banerjee
8/9/2019 JSP part1
7/17
No central component
Has to embed Big chunk of Java-code to implementbusiness logic
Designers arent comfortable with server-sideprogrammig.
Reusability of application components are is notpromoted.
Biswabrata Banerjee
8/9/2019 JSP part1
8/17
JSP architecture
2/6
3
4
uses bean
5
Biswabrata Banerjee
8/9/2019 JSP part1
9/17
MVC Architecture Explained
In MVC model, the request is sent by the browser to thecontroller
or the servlet. Thisrequest is instantiated by the servlet as a Java Bean. The main aspect is JSP are compiled
into servlets at the back end and the front end tasks are not interrupted. The servlet engine
takes up the responsibility of compiling JSP Servlet and producing the final JSP servlet class
for usage. The front end presentation modules are handled by JSP forviewing and the
manipulation of data is handled by Java Bean(model) and passed back to JSP when
needed. The Presentation part of the MVC Model has no processing logic. It performs the
task of extracting beans or objects that may have been initially created by the controller. It
also extracts the dynamic content within for insertion within its static templates.
TheApplication Managerin the MVC Model is the Controllerthat processes HTTP
requests. They are not responsible for presentation tasks. That can be either servlets or JSP.
They take the task of managing the application state, security, and presentation uniformity
and thus, have a single point of entry.
8/9/2019 JSP part1
10/17
JSP Life-cycle
Phase Name Description
Page Translation A Servlet (java) file is created
Page compilation The java file compiled.
Load class Compiled class loaded
Create Instance An instance of Servlet is created
Call jspInit()Syn : public void jspInit() { //.. }
This method is called before other method to allowinitialization.
Call _jspService()
Syn : public void _jspService(HttpServletRequest req,
HttpServletResponse res )
throws ServletException,IOException { //..}
This method is called for each request
Call jspDestory()Syn : public void jspDestroy() { //.. }
This method is called when Servlet decided to take the
Servlet out of service
8/9/2019 JSP part1
11/17
// not exact code ! Depends on vendor specification.
import java.util.Date;
public final class Todat_jsp implements javax.servlet.Servlet,javax.servlet.jsp.HttpJspPage {
public void jspInit(){
// initialization parameters
}public void _jspService( javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response )
throws javax.servlet.ServletException , java.io.IOException
{
javax.servlet.ServletConfig config = ; // Get the servlet config
out.wite( (new Date().toString() ) ;}
public void jspDestroy(){
//
}
}
8/9/2019 JSP part1
12/17
JSP Nuts & Bolts
Directives
Scripting Elements
Actions
include
taglib
page
Declaration
ScriptletExpression
Comments
useBean
setProperty
getProperty
param
include
forward
plugin
8/9/2019 JSP part1
13/17
Attribute Description Default value
language For defining scripting language java
extends To indicate which clas the generated servlet will
extend
Omitted
Import Reason is Same as import in a java class Omitted
session specify whether a page participates in a session True
buffer models Buffer for the output stream of the client Implementation
dependent;at least 8kb
autoFlush To empty the buffer if full True
isThreadSafe Defines the level of thread safety trueInfo Defines an informative string [ servlet.getServletInfo() ] Omitted
errorPage To invoke another JSP page on error Omitted
isErrorPage If this is an error page of some other JSP page false
contentType The character encoding & MIME (MultipurposeInternet Mail Extensions) type. MIME type is text/htmlCHARSET is ISO-8859-1
8/9/2019 JSP part1
14/17
An example:
< !- - do anything here as you Like -->
8/9/2019 JSP part1
15/17
The include directive
Include Directive Includes a file of text or code when the JSPpage is translated i.e., at the time of compilation.
JSP Syntax
Examplesinclude.jsp:
An Include Test
The current date and time are
8/9/2019 JSP part1
16/17
The include directive Contd
date.jsp:
Displays in the page:
The current date and time are
Aug 30, 1999 2:38:40
Description
The directive inserts a file of text or code in a JSP file attranslation time, when the JSP file is compiled. When you use the directive, the include process is static. A static include means that the text of theincluded file is added to the JSP file. The included file can be a JSP file, HTML file,
or text file. If the included file is a JSP file, its JSP elements are parsed and theirresults included (along with any other text) in the JSP file.
8/9/2019 JSP part1
17/17
The taglib directive
Taglib Directive defines a tag library and prefix for the customtags used in the JSP page.
JSP Syntax
Examples
Description
The directive declares that the JSP file uses customtags, names the tag library that defines them, and specifies theirtag prefix.