What is Java Server Page - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

What is Java Server Page

Description:

... simply a HTML page that contains snippets of code that execute application logic ... tag in an HTML page, actual snippets of servlet code can be embedded. ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 18
Provided by: kseth
Category:
Tags: java | page | server | snippets

less

Transcript and Presenter's Notes

Title: What is Java Server Page


1
What is Java Server Page?
  • JSP Page is simply a HTML page that contains
    snippets of code that execute application logic
    to generate dynamic content
  • This application logic could be present in a Java
    Bean, Enterprise Java Bean, RMI Object or Corba
    Object

2
JSP Servlets
  • Creating dynamic HTML output in servlets is a
    tedious work
  • If changes are required in the dynamic HTML
    output of a servlet, the servlet has to be
    recompiled
  • If dynamic output is generated using JSP, changes
    could be made and need not be recompiled.
  • It is just like making changes to basic HTML.

3
Java Server Pages
  • JSP is similar to server-side includes.
  • The main difference is that instead of embedding
    a ltSERVLETgt tag in an HTML page, actual snippets
    of servlet code can be embedded.
  • Each block of servlet code is surrounded by
    leading lt tag and a closing tag gt

4
Four predefined variables
  • request
  • The servlet request, an HttpServletRequest
    object
  • response
  • The Servlet response, an HttpServletResponse
    object
  • out
  • The Output writer, a PrintWriter object
  • in
  • The input reader, a Bufferedreader object
  •  

5
Example(hello.jsp)
  • ltHTMLgt
  • ltHEADgtltTITLEgtHellolt/TITLEgtlt/HEADgt
  • ltBODYgt
  • ltH1gt
  • lt if ( request.getParameter (name )
    null)
  • out.println(Hello World)
  • else
  • out.println(Hello,request.getParameter(name)
    )
  • gt
  • lt/H1gt
  • lt/BODYgtlt/HTML

6
Background Servlet
  • When the page is run, the server automatically
    creates, compiles , loads, and runs a special
    servlet corresponding to hello.jsp.
  • The static portions of the HTML pages are
    generated by the workhorse servlet using
    out.println() commands.

7
  • import java .io.
  • import java.x.servlet.
  • import javax.servlet.
  • import javax.servlet.http.
  • public class _hellojsp extends HttpServlet
  • public void _jspservice (HttpServletrequest
    request,HttpServletResponse response)
  • response.setContentType(text/html)
  • PrintWriter out response.getWriter()
  • Bufferedreader in request.getReader()
  • out.println(ltHTMLgt)
  • out.println(ltHEADgtltTITLEgtHellolt/TITLEgtlt/HEADgt
    )
  • out.println(ltBODYgt)
  • out.println(ltH1gt)
  • if (request.getParameter(name) null)
  • out.println(Hello World)
  • else
  • out.println(Hello,request.getParameter(name
    ))
  • out.println(lt/H1gt )
  • out.println(lt/BODYgtlt/HTMLgt)

8
  • When the JSP page is accessed for the first time,
    server takes a short time to respond because the
    server has to create compile the Background
    Servlet.
  • When the JSP files contents changes, the server
    takes a short time because the server has to
    recompile the BackgroundServlet.

9
Expression
  • Expressions are variables or constants that are
    inserted into the data returned by the web server
  • Expressions are defined by lt and gt
  • Any Java expressions between the two tags is
    evaluated, the result is converted to a string,
    and the text is included directly in the page.
  • For example,lt h gt prints the value of the
    variable h.

10
Directives
  • A JSP directive begins with lt_at_ and ends with gt.
  • It allows a JSP page to control certain aspects
    of Background Servlet.
  • Directives can be used from JSP to set
    background Servlets content type, import a
    package, extend a different superclass, implement
    an interface and handle either GET or POST
    REQUESTSgt

11
Content Type
  • specifies the content type of generated page
  • lt _at_ content_type text/plain gt
  • The default content type is text/html
  •  

12
import keyword
  • specifies a list of classes the servlet should
    import.
  • Multiple classes can be given in a
    comma-seperated list or given through multiple
    import directives.
  • For example
  • lt_at_ import java.io.util.Hashtablegt
  • lt_at_ import java.io., java.io.util.Hashtable
    gt

13
extends
  • Specifies the superclass the servlet should
    extend.
  • For example
  • lt_at_ extends GenericServletgt
  • The Default superclass is HttpServlet

14
implements
  • Implements
  • specifies a list of interfaces the servlet should
    implement.
  • Multiple interfaces can be given in a
    comma-separated list or given through multiple
    implement directives.
  • For example
  • lt_at_ implements Serializablegt
  • The default is not to implement anything

15
method
  • Specifies the servlet method that should contain
    the generated code and handle client requests.
  • The default is service,which handles all
    requests.
  • For example
  • lt_at_ method doPost gt

16
language
  • Specifies the scripting language used by the
    back-end. The default language is java.
  • Some servers can allow other languages.
  • For example
  • lt language javagt

17
  • Example
  •  
  • lt_at_ methoddoPost gt
  • ltHTMLgt
  • ltHEADgtltTITLEgtHellolt/TITLEgtlt/HEADgt
  • ltBODYgt
  • ltH1gt
  • lt if (request.getParameter(name)null) gt
  • Hello World
  • lt else gt
  • Hello, ltrequest.getParameter(name) gt
  • lt gt
  • lt/H1gt
  • lt/BODYgtlt/HTMLgt
  •  
  •  
Write a Comment
User Comments (0)
About PowerShow.com