JSP: Java Server Pages - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

JSP: Java Server Pages

Description:

... written in Java, not Visual Basic or other MS-specific ... Java code that is executed every time the JSP is invoked. HTML BODY // This is a scriptlet. ... – PowerPoint PPT presentation

Number of Views:649
Avg rating:3.0/5.0
Slides: 12
Provided by: bec46
Category:
Tags: jsp | basic | code | html | java | pages | server

less

Transcript and Presenter's Notes

Title: JSP: Java Server Pages


1
JSP Java Server Pages
  • JSP helps in generating dynamic content, based
    on user input, time of day, or any other runtime
    conditions.
  • Web application a program on the server
    processes requests and generates response.
  • Problem with servlets
  • 1. Detailed Java programming knowledge is
    needed.
  • 2. To change the look and feel, change the
    servlet code and recompile.
  • 3. Advantage of the web page development tools
    are utilised.
  • JSP separates the request processing and the
    business logic code from the presentation. In
    servlet, HTML is embedded, here JSP elements are
    added to genearate the dynamic content.

2
Hello world servlet public class
HelloWorldServlet implements servelet public
void service(ServletRequest request, ServletResp
onse response) throws ServletException,
IOException response.setContentType(text/htm
l) PrintWriter outresponse.getWriter() ou
t.println(lthtmlgt out.println(
ltheadgt out.println ( lttitlegtHello
worldlt/titlegt out.println(
lt/headgt out.println( ltbodygt out.printl
n( lth1gtHello Worldlt\h1gt out.println
( It's (new java.util.date().toStrin
g()) and all is well) out.println(
lt/bodygt out.println(lt/htmlgt
Hello world JSP Page lthtmlgt
ltheadgt lttitlegtHello world lt\titlegt lt/headgt
ltbodygt lth1gtHello Worldlt\h1gt It,s lt new
java.util.date().toString() gt and all is well.
lt/bodygt lt/htmlgt
3
Anatomy of a JSP Page
  • A JSP page is a regular web page with JSP
    elements for generating the parts of the page
    that differ for each request.
  • JSP separates the request processing and the
    business logic code from the presentation. In
    servlet, HTML is embedded, here JSP elements are
    added to genearate the dynamic content.

4
(No Transcript)
5
(No Transcript)
6
What are the Advantages of JSP? vs. Active
Server Pages (ASP). ASP is a similar technology
from Microsoft. Advantages of JSP, 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. vs. Pure
Servlets. JSP can do everything that a
servlet can do. But it is more convenient to
write (and to modify!) regular HTML than to have
a zillion println statements that generate the
HTML. Plus, by separating the look from the
content you can put different people on different
tasks your Web page design experts can build
the HTML, leaving places for your servlet
programmers to insert the dynamic content.
vs. Server-Side Includes (SSI). SSI is a
widely-supported technology for including
externally-defined pieces into a static Web
page. JSP is better because it lets you use
servlets instead of a separate program to
generate that dynamic part. Besides, SSI is
really only intended for simple inclusions, not
for "real" programs that use form data, make
database connections, and the like. vs.
JavaScript. JavaScript can generate HTML
dynamically on the client. This is a useful
capability, but only handles situations where
the dynamic information is based on the client's
environment. With the exception of cookies, HTTP
and form submission data is not available to
JavaScript. And, since it runs on the client,
JavaScript can't access server-side resources
like databases, catalogs, pricing information,
and the like. vs. Static HTML. Regular
HTML, of course, cannot contain dynamic
information. JSP is so easy and convenient that
it is quite feasible to augment HTML pages that
only benefit marginally by the insertion of
small amounts of dynamic data. Previously, the
cost of using dynamic data would preclude its
use in all but the most valuable instances.
7
Adding dynamic content via expressions each time
you reload the page in the browser, it comes up
with the current time. The character sequences
lt and gt enclose Java expressions, which are
evaluated at run time. time.jsp ltHTMLgt ltBODYgt
Hello!  The time is now lt new java.util.Date()
gt lt/BODYgt lt/HTMLgt
8
Scriptlets It is difficult to do much
programming just by putting Java expressions
inside HTML. JSP also allows you to write blocks
of Java code inside the JSP, by placing your
Java code between lt and gt characters (without
the sign at the start of the sequence.) This
block of code is known as a "scriptlet". A
scriptlet contains Java code that is executed
every time the JSP is invoked. ltHTMLgt ltBODYgt lt
// This is a scriptlet. Notice that the
"date" // variable we declare here is
available in the // embedded expression later
on. System.out.println( "Evaluating date now"
) java.util.Date date new
java.util.Date() gt Hello! The time is now lt
date gt lt/BODYgt lt/HTMLgt
9
Mixing Scriptlets and HTML
ltHTMLgt ltBODYgt ltTABLE BORDER2gt lt
int n5 for ( int i 0 i lt n
i ) gt ltTRgt
ltTDgtNumberlt/TDgt ltTDgtlt i1 gtlt/TDgt
lt/TRgt lt gt
lt/TABLEgt lt/BODYgt lt/HTMLgt
10
JSP Directives It is possible to use "import"
statements in JSPs lt_at_ page import"java.util."
gt ltHTMLgt ltBODYgt lt System.out.println(
"Evaluating date now" ) Date date new
Date() gt Hello! The time is now lt date
gt lt/BODYgt lt/HTMLgt The first line is a JSP
"directive", starts with lt_at_ characters. Three
types of directives page, include, taglib. This
one is page directive, can contain a list of
imported packages. The include directive is used
to physically include the contents of another
file. ltHTMLgt ltBODYgt Going to include
time.jsp...ltBRgt lt_at_ include file"time.jsp"
gt lt/BODYgt lt/HTMLgt
11
JSP Declarations The JSP you write turns into a
class definition.All the scriptlets you write are
placed inside a single method of this class. You
can also add variable and method declarations to
this class. You can then use these variables and
methods from your scriptlets and expressions. To
add a declaration, you must use the lt! and gt
sequences to enclose your declarations. lt_at_ page
import"java.util." gt ltHTMLgt ltBODYgt lt!
Date theDate new Date() Date getDate()
System.out.println( "In getDate()
method" ) return theDate
gt Hello! The time is now lt getDate()
gt lt/BODYgt lt/HTMLgt Declared the date variable and
the method getDate. The time does not change.
Write a Comment
User Comments (0)
About PowerShow.com