Servlets and JavaServer Pages JSP - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Servlets and JavaServer Pages JSP

Description:

A Servlet is a Java class that provides special server side service. ... throws ServletException, IOException { response.setContentType('text/html; charset=UTF-8' ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 13
Provided by: IDE66
Category:

less

Transcript and Presenter's Notes

Title: Servlets and JavaServer Pages JSP


1
Lecture 5
  • Servlets and JavaServer Pages (JSP)

2
Servlet?
  • A Servlet is a Java class that provides special
    server side service.
  • Servlets runs in a server and returns answer to
    clients in a web page.
  • Servlets build webpages....so, its contains HTML

3
Structure
  • import java.io.import java.net.import
    javax.servlet.import javax.servlet.http.pub
    lic class Welcome extends HttpServlet
    public void doGet( HttpServletRequest request,
    HttpServletResponse response) throws
    ServletException, IOException
  • response.setContentType("text/html
    charsetUTF-8") PrintWriter out
    response.getWriter() String
    message1request.getParameter("Order")
    //TODO output your page here
    out.println("lthtmlgt") out.println("ltheadgt
    ") out.println("lttitlegtorderlt/titlegt")
    out.println("lt/headgt")
    out.println("ltbodygt") out.println("lth1gtlts
    tronggtI like to order ltbrgtGet Order Servlet
    lt/h1gt") out.println("lt/bodygt")
    out.println("lt/htmlgt")
    out.close()

4
How its works
  • public void doGet( HttpServletRequest request,
  • HttpServletResponse response)
  • ..
  • -HTLM forms let the user submit data to romote
    size
  • - Servlets use HTTP proprieties, request ,
    respons to comunicate
  • Request, incapsulate data from the client
  • Respons, incapsulate the response information
    from the server

5
  • JSPs are essential an HTML page with special JSP
    tags embedded.
  • These JSP tags can contain Java code.
  • The JSP file extension is .jsp rather than .
  • The JSP engine parses the .jsp and creates a Java
    servlet source file.
  • HTML from the Servlet output is sent via the
    Internet.
  • HTML results are displayed on the user's web
    browser.

6
Structure
  • lthtmlgt
  • ltheadgt
  • lttitlegtMy first JSP page
  • lt/titlegt
  • lt/headgt
  • ltbodygt
  • lt_at_ page language"java" gt
  • lt out.println("Hello World") gt
  • lt/bodygt
  • lt/htmlgt

7
Using JSP tags
  • Declaration tag
  • Expression tag
  • Directive tag
  • Scriptlet tag
  • Action tag

8
  • 1) Declaration tag ( lt!  gt ) . This tag allows
    the developer to declare variables or methods.
  • lt!
  •           private int counter 0
  •           private String get Account ( int
    accountNo)
  • gt
  • 2) Expression tag ( lt   gt) This tag allows
    the developer to embed any Java expression and is
    short for out.println().
  • Date lt new java.util.Date() gt will print
    the date

9
  • 3) Directive tag ( lt_at_ directive ... gt)
  •  
  • A JSP directive gives special information about
    the page to the JSP Engine.
  • Directives do not produce any visible output when
    the page is requested but
  • change the way the JSP Engine processes the file.
  •  
  • There are three main types of directives
  •  
  • 1)     page - processing information for this
    page. lt_at_ page language "java" gt
  • 2)     Include - files to be included. lt_at_
    include file "include/privacy.html" gt
  • 3)     Tag library - tag library to be used in
    this page. lt_at_ taglib uri "tag library
  • URI" prefix "tag Prefix" gt
  •  

10
  • 4) Scriptlet tag ( lt ... gt )
  •  Between lt and gt tags,any valid Java code is
    called a Scriptlet..   
  • lt 
  •         String username "visualbuilder"
  •         out.println ( username )
  •   gt
  • 5) Action tag
  •  
  •   There are three main roles of action tags
  •  
  • 1)     enable the use of server side Javabeans
  • 2)     transfer control between pages
  • 3)     browser independent support for applets.

11
(No Transcript)
12
  • JSP compared to ASP
  • JSP and ASP are fairly similar in the
    functionality that they provide. JSP may have
    slightly higher learning curve. Both allow
    embedded code in an HTML page,session variables
    and database access and manipulation. Whereas ASP
    is mostly found on Microsoft platforms i.e.
    NT,JSP can operate on any platform that conforms
    to the J2EE specification. JSP allow component
    reuse by using Javabeans and EJBs. ASP provides
    the use of COM / ActiveX controls.
  •  
  • JSP compared to ASP.NET
  • ASP.NET is based on the Microsoft .NET
    framework. The .NET framework allows
    applications to be developed using different
    programming languages such as Visual Basic,C and
    JavaScript. JSP and Java still has the advantage
    that it is supported on many different platforms
    and the Java community has many years of
    experience in designing and developing Enterprise
    quality scalable applications. This is not to say
    that ASP.NET is bad,actually it is quite an
    improvement over the old ASP code.
  •  
Write a Comment
User Comments (0)
About PowerShow.com