Java server pages
Mrs.N.Kavitha
Head,Department of Computer Science,
E.M.G.Yadava Women’s College
Madurai-14.
DEFINITION FOR JSP
Java Server Pages (JSP) is a technology
for developing web pages that support dynamic
content which helps developers insert java code
in HTML pages by making use of special JSP tags,
most of which start with <% and end with %>.
EXAMPLE
We can collect input from users through
web page forms
In the early days of the Web, the Common
Gateway Interface (CGI) was the only tool for
developing dynamic web content. However, CGI is
not an efficient solution.
For every request that comes in, the web
server has to create a new operating-system
process, load aninterpreter and a script, execute
the script, and then tear it all down again.
This is very taxing for the server and doesn't
scale well when the amount of traffic increases.
 Active Server Pages (ASP)
 PHP
 ColdFusion
 Server-Side Includes (SSI)
 JavaScript
 Pure Servlet
 Static HTML
The advantages of JSP are twofold.
First, the dynamic part is written in Java,
not Visual Basic or other MS specific
language, so it is more powerful and easier
to use. Second, it is portable to other
operating systems and non-Microsoft Web
servers.
PHP1 is an open source web scripting language.
Like JSP and ASP, PHP allows a page author to
include scripting code in regular web pages to
generate dynamic content.
PHP has a C-like syntax with some features
borrowed from Perl, C++, and Java.
The current version, compiles a page when it's
requested, executes it and merges the result of
executing the scripts with the static text in the
page, before it's returned to the browser.
The dynamic parts of a page are generated by
inserting HTML/XML -like elements, known as the
ColdFusion Markup Language (CFML), into web pages.
The latest version of ColdFusion also includes
elements for communication with Java servlet and
Enterprise JavaBeans.
Custom elements can be developed in C++ or Java to
encapsulate application specific functions, and CFML
extensions are available from third parties.
Server Side Iincludes is really
only intended for simple inclusions,
not for "real" programs that use form
data, make database connections,
and the like.
JavaScript can generate
HTML dynamically on the client but
can hardly interact with the web
server to perform complex tasks like
database access and image
processing etc.
It is more convenient to write
(and to modify!) regular HTML than
to have plenty of println statements
that generate the HTML.
Regular HTML, of course, cannot
contain dynamic information.
A JSP life cycle can be defined as
the entire process from its creation
till the destruction which is similar
to a servlet life cycle with an
additional step which is required to
compile a JSP into servlet.
Compilation
Initialization
Execution
Cleanup
When a browser asks for a JSP, the JSP
engine first checks and the JSP engine compiles
the page. If the page has never been compiled
and JSP has been modified.
The compilation process involves three steps:
 Parsing the JSP.
 Turning the JSP into a servlet.
 Compiling the servlet.
When a container loads a JSP it invokes the
jspInit() method before servicing any requests. If
you need to perform JSP-specific initialization,
override the jspInit() method.
Example:
public void jspInit()
{ // Initialization code...
}
Typically initialization is performed only
once and as with the servlet init method
This phase of the JSP life cycle represents all
interactions with requests until the JSP is destroyed.
Whenever a browser requests a JSP and the page
has been loaded and initialized, the JSP engine
invokes the _jspService() method in the JSP.
The _jspService() method takes an
HttpServletRequest and an HttpServletResponse as
its parameters as follows
Example
void _jspService (HttpServletRequest request,
HttpServletResponse response)
{
// Service handling code...
}
The destruction phase of the JSP life cycle
represents when a JSP is being removed from use
by a container.
The jspDestroy() method is the JSP
equivalent of the destroy method for servlets.
Example
public void jspDestroy()
{
// Your cleanup code goes here.
}
SCRIPLET
A scriptlet can contain any number of
JAVA language statements, variable or
method declarations, or expressions that are
valid in the page scripting language.
Syntax of Scriptlet
<% code fragment %>
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " +
request.getRemoteAddr());
%>
</body>
</html>
A declaration declares one or more
variables or methods that you can use in Java
code later in the JSP file.
Syntax of JSP DECLARATIONS
<%! declaration; %>
Example
<%! int a, b, c=0; %>
<%! Circle a = new Circle(2.0); %>
<%! int day = 3; %>
<html>
<head><title>SWITCH...CASE Example</title></head>
<body>
<%
switch(day) {
case 0:
out.println("It's Sunday.");
break;
case 1:
out.println("It's Monday.");
break;
case 2:
out.println("It's Tuesday.");
break;
case 3:
out.println("It's Wednesday.");
break;
case 4:
out.println("It's Thursday.");
break;
case 5:
out.println("It's Friday.");
break;
default:
out.println("It's Saturday.");
}
%>
</body>
</html>
This would produce following result:
It's Wednesday.
The Java Server Page in Java Concept.pptx

The Java Server Page in Java Concept.pptx

  • 1.
    Java server pages Mrs.N.Kavitha Head,Departmentof Computer Science, E.M.G.Yadava Women’s College Madurai-14.
  • 2.
    DEFINITION FOR JSP JavaServer Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>. EXAMPLE We can collect input from users through web page forms
  • 3.
    In the earlydays of the Web, the Common Gateway Interface (CGI) was the only tool for developing dynamic web content. However, CGI is not an efficient solution. For every request that comes in, the web server has to create a new operating-system process, load aninterpreter and a script, execute the script, and then tear it all down again. This is very taxing for the server and doesn't scale well when the amount of traffic increases.
  • 4.
     Active ServerPages (ASP)  PHP  ColdFusion  Server-Side Includes (SSI)  JavaScript  Pure Servlet  Static HTML
  • 5.
    The advantages ofJSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.
  • 6.
    PHP1 is anopen source web scripting language. Like JSP and ASP, PHP allows a page author to include scripting code in regular web pages to generate dynamic content. PHP has a C-like syntax with some features borrowed from Perl, C++, and Java. The current version, compiles a page when it's requested, executes it and merges the result of executing the scripts with the static text in the page, before it's returned to the browser.
  • 7.
    The dynamic partsof a page are generated by inserting HTML/XML -like elements, known as the ColdFusion Markup Language (CFML), into web pages. The latest version of ColdFusion also includes elements for communication with Java servlet and Enterprise JavaBeans. Custom elements can be developed in C++ or Java to encapsulate application specific functions, and CFML extensions are available from third parties.
  • 8.
    Server Side Iincludesis really only intended for simple inclusions, not for "real" programs that use form data, make database connections, and the like.
  • 9.
    JavaScript can generate HTMLdynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc.
  • 10.
    It is moreconvenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML.
  • 11.
    Regular HTML, ofcourse, cannot contain dynamic information.
  • 12.
    A JSP lifecycle can be defined as the entire process from its creation till the destruction which is similar to a servlet life cycle with an additional step which is required to compile a JSP into servlet.
  • 13.
  • 15.
    When a browserasks for a JSP, the JSP engine first checks and the JSP engine compiles the page. If the page has never been compiled and JSP has been modified. The compilation process involves three steps:  Parsing the JSP.  Turning the JSP into a servlet.  Compiling the servlet.
  • 16.
    When a containerloads a JSP it invokes the jspInit() method before servicing any requests. If you need to perform JSP-specific initialization, override the jspInit() method. Example: public void jspInit() { // Initialization code... } Typically initialization is performed only once and as with the servlet init method
  • 17.
    This phase ofthe JSP life cycle represents all interactions with requests until the JSP is destroyed. Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP. The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its parameters as follows
  • 18.
    Example void _jspService (HttpServletRequestrequest, HttpServletResponse response) { // Service handling code... }
  • 19.
    The destruction phaseof the JSP life cycle represents when a JSP is being removed from use by a container. The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Example public void jspDestroy() { // Your cleanup code goes here. }
  • 20.
    SCRIPLET A scriptlet cancontain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language. Syntax of Scriptlet <% code fragment %>
  • 21.
    <html> <head><title>Hello World</title></head> <body> Hello World!<br/> <% out.println("YourIP address is " + request.getRemoteAddr()); %> </body> </html>
  • 23.
    A declaration declaresone or more variables or methods that you can use in Java code later in the JSP file. Syntax of JSP DECLARATIONS <%! declaration; %> Example <%! int a, b, c=0; %> <%! Circle a = new Circle(2.0); %>
  • 24.
    <%! int day= 3; %> <html> <head><title>SWITCH...CASE Example</title></head> <body> <% switch(day) { case 0: out.println("It's Sunday."); break; case 1: out.println("It's Monday."); break; case 2: out.println("It's Tuesday."); break;
  • 25.
    case 3: out.println("It's Wednesday."); break; case4: out.println("It's Thursday."); break; case 5: out.println("It's Friday."); break; default: out.println("It's Saturday."); } %> </body> </html>
  • 26.
    This would producefollowing result: It's Wednesday.