Servlets and JavaServer Pages JSP - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Servlets and JavaServer Pages JSP

Description:

A Servlet is a Java class that provides special server side service. ... Det r d lig uppdelning i roller att blanda html och java eftersom det ofta inte ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 17
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
Servers jobb
Read data sent by client Generate the
results Send data back to client (HTML)
4
How its works
  • init
  • Executed once when the servlet is first loaded.
  • Not called for each request.
  • service
  • Called in a new thread by server for each
    request.
  • Dispatches to doGet, doPost, etc.
  • Do not override this method!
  • doGet, doPost
  • Handles GET, POST, etc. requests.
  • Override these to provide desired behavior.
  • destroy
  • Called when server deletes servlet instance.
  • Not called after each request.

5
Structure
  • import java.io.import java.net.import
    javax.servlet.import javax.servlet.http.publ
    ic class GetOrder 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()

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

7
Use form and html to conect ...
  • lthtmlgt
  • ltheadgtlttitlegtGet Orderlt/titlegtlt/headgt
  • ltbodygt
  • lth3gtEnter an orderlt/h3gt
  • ltform action"http//localhost8100/servlet/GetOrd
    er"

  • methodGETgt
  • Order ltinput typetext nameOrder size20gt
  • lt/formgtlt/bodygtlt/htmlgt

8
Varför inte
  • Det är dÃ¥lig design att ha ett objekt som har
    flera olika uppgifter, till exempel att hantera
    indata och generera utdata.
  • Det är dÃ¥lig design att blanda olika sprÃ¥k (java
    och html) i samma fil.
  • Det är dÃ¥lig uppdelning i roller att blanda html
    och java eftersom det ofta inte är samma person
    som hanterar båda.
  • Det är ett elände att fÃ¥ html-strängar genom
    kompilatorn på grund av radbrytningar och
    "-tecken.
  • Det gÃ¥r inte att använda nÃ¥got verktyg för att
    skriva html-sidor.

9
  • 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.

10
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

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

12
  • 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

13
  • 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
  •  

14
  • 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.

15
(No Transcript)
16
  • 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