Internet - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Internet

Description:

Java Servlets are a more recent alternative to CGI programming. ... may be difficult to write servlets to produce attractive well 'styled' web pages. ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 19
Provided by: supp161
Category:
Tags: internet | styled

less

Transcript and Presenter's Notes

Title: Internet


1
Internet Communications
  • Server-side programming for
  • the WWW (lecture 7)
  • Java Servlets
  • JSP

2
Servlets
  • Java Servlets are a more recent alternative to
    CGI programming.
  • Java servlets are programs that run on the Web
    server and build web pages on the fly (much
    like a CGI program).
  • Java servlets typically take input from forms on
    Web pages and write out a new Web page in
    response.
  • They should not be confused with Java Applets -
    which run on the client.

3
Servlets vs CGI
  • Java servlets have following advantages over CGI
  • More efficient -
  • using CGI a new process is started for each
    request.
  • Using servlets the java program stays running and
    new requests are handled by a new thread.
  • More powerful -
  • For example, Java servlets can handle sessions
    and maintain information about the user across
    requests.
  • More secure

4
Writing and Running Servlets
  • How you set up servlets depends very much on
    local setup.
  • We will look briefly at an example servlet for
    handling HTTP requests, and how it is set up.
  • First write your servlet a Java program which
    uses special servlet library classes.
  • Example below writes Hello World!

5
Hello World Servlet
  • import java.io.
  • import javax.servlet.
  • import javax.servlet.http.
  • public class HelloWorld extends HttpServlet
  • public void doGet(HttpServletRequest request,
  • HttpServletResponse
    response)
  • throws ServletException, IOException
  • response.setContentType("text/plain")
  • PrintWriter out response.getWriter()
  • out.println(Hello World")
  • out.close()

Standard stuff
Set file type to plain Get hold of stream for
writing back to web page, and write Hello World
6
Explanation
  • Standard servlet mantra involves
  • importing standard servlet classes
  • creating a subclass of HttpServlet
  • creating a doGet method which deals with
    requests to that Servlet.
  • This must have a request and a response argument,
    which provide handles to input from the page, and
    the output stream to the page.
  • Setting the content type to be text/plain or
    text/html
  • Creating a PrintWriter such that writing to this
    will write back a Web page to the client.

7
Servlets at MACS
  • Now set it up You need a special server running
    to handle the servlet requests, e.g., TOMCAT
    server.
  • We then compile the servlet code, and place
    servlets in the appropriate directory.
  • We can now run it using a URL like the following
  • http//www2.macs.hw.ac.uk8080/examples/servlet/He
    lloWorld

8
Dealing with Form Input
  • It is easy to extend our servlet to handle input
    from a form.
  • There is a special getParameter method that we
    can use to get hold of the values entered given
    the name of the input element.
  • E.g. given ltinput typetext nameage /gt
  • use request.getParameter(age)

9
Servlets Summary
  • Servlets provide many useful facilities for
    creating dynamic web pages.
  • They are efficient, as once running the program
    stays running dealing with all requests. CGI
    programs are started afresh with each request.
  • Moderately easy to program once you master the
    mantra.
  • But much more than has been described here!

10
Java Server Pages (JSP)
  • Java Servlets can be awkward to use.
  • The Java programs often consist mostly of
    statements to write out HTML (with just a few
    dynamic bits to do calculations, database access
    etc).
  • It may be difficult to write servlets to produce
    attractive well styled web pages.
  • JSP allows you to mix standard static HTML pages
    with dynamically generated HTML.
  • Hybrid of HTML and Java Servlets

11
JSP - Example
  • Following example illustrates how we can get some
    servlet Java code embeded in the HTML.

lthtmlgt ltheadgt lt/headgt ltbodygt lth1gt Todays date
lt/h1gt lt new java.util.Date()
gt lt/bodygt lt/htmlgt
Bit of JSP
12
JSP
  • JSP avoids having to write servlets consisting
    mostly of println statements to write out HTML.
  • JSP pages are automatically converted into
    servlet code on first access then stored as
    servlets on the server.
  • JSP not to be confused with JavaScript, which is
    handled on the client side.
  • To use them effectively you need to understand
    servlets.

13
To Use JSP
  • JSP files end in .jsp and are stored in special
    location.
  • Code is embedded in lt gt brackets.
  • Some special variants of this for special
    purposes.
  • Especially useful is lt . gt which evaluates an
    expression and sticks it in the output.
  • Some useful variables are automatically
    available.
  • In particular the servlet response parameter
    which contains data from forms etc.
  • And the out variable which is a printwriter
    sending output back to web browser to be
    incorporated in web page.

14
More JSP Examples
lthtmlgt ltheadgtlt/headgt ltbodygt lth1gt One plus one
lt/h1gt 1 1 lt 11 gt lt/bodygt lt/htmlgt
Evaluates the expression 11 and puts the
result in the output.
15
More JSP Examples
lthtmlgt ltheadgtlt/headgt ltbodygt lth1gt And the title
is lt/h1gt lt request.getParameter("title")
gt lt/bodygt lt/htmlgt
16
Other Features of JSP
  • JSP includes built in features that allow
  • database access
  • cookie handling
  • session management
  • mail
  • etc etc

17
Other server side languages
  • Other server side languages include ASP, PHP. All
    allow us to mix HTML with code. The code may LOOK
    like JavaScript, but is interpreted on SERVER,
    not client.
  • ASPlthtmlgt ltbodygt lt response.write("Hello
    World!") gt lt/bodygt lt/htmlgt
  • PHP ltbodygt lt?php echo Hello World ?gt

18
Summary
  • JSP provides convenient form of server side
    scripting, allowing mix of HTML and java.
  • Converted on first use to a servlet.
  • Good for fairly simple things. Can get convoluted
    for more complex things.
  • Really need to understand servlets to do complex
    JSP applications.
  • Many other languages with similar syntax.
Write a Comment
User Comments (0)
About PowerShow.com