lec15 - COMP201 Java Programming Part III: Advanced...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP201 Java Programming Part III: Advanced Features Topic 15: Servlets Materials Based on The Java Tutorial http://java.sun.com/docs/books/tutorial//servlets/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 15 / Slide 2 Outline Introduction A simple servlet General information about servlets HTTP Servlets Session tracking and cookies Running servlets
Background image of page 2
COMP201 Topic 15 / Slide 3 Introduction Servlets are modules that extend (Java-enabled web) servers. E.g. Respond to client requests by consulting a database Most widely used within HTTP servers. Duke’s Book store
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 15 / Slide 4 Introduction Traditionally, HTTP servers handle client requests by using CGI (common gateway interface) scripts. 1. User fills out a form and submit. 1. HTTP server gets URL requests from the net. 1. HTTP server finds the CGI script specified in the HTML file, runs it with parameters from requesting URL 1. HTTP server takes output from the CGI program (most often output is HTML text), fixes it up with a full complete HTTP header, and sends it back to the original requesting client HTML: <form action=“cgi_bin/rpe" method= POST > First Name: <input type=text size=20 name=firstname> Last Name: <input type=text size=20 name=lastname> <input type=submit>
Background image of page 4
COMP201 Topic 15 / Slide 5 Introduction Advantages of servlets (over CGI scripts) More efficient VM always running, servlet objects persist instead of being continually created/destroyed as CGI processes are More secure SecurityManager can constrain servlets in manner similar to applets Portable: written in Java, while CGI scripts in C, Perl, shell script State-conscious – Can track session information easily. Can access vast Java packages such as networking, threads, etc .
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 15 / Slide 6 Servlet vs. Applet Servlets are to servers what applets are to browsers: small Java programs compiled to bytecode that can be loaded dynamically and that extend the capabilities of the host. But: Applets run by browser, servlets run by server. Applets are “client-side java”, servlets are “server-side java”. Applets makes appearance of web pages alive, servlets makes contents of web pages dynamic. Unlike applets, however, servlets have no graphical user interface. Implement only back-end processing.
Background image of page 6
COMP201 Topic 15 / Slide 7 A Simple Servlet import java.io.*; import javax.servlet .*; public class SimpleGenericServlet extends GenericServlet { public void service ( ServletRequest request, ServletResponse response) throws ServletException, IOException { response. setContentType ("text/plain"); PrintWriter out = response. getWriter (); out.println("Hello World"); out. close (); } } Note: no main method. Servlet run by server, just as applet run by browser
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 15 / Slide 8 A Simple Servlet service : The most important method in a servlet, Determines what the servlet does. Invoked automatically when a request comes in.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/14/2010 for the course COMP COMP 201 taught by Professor Nil during the Spring '02 term at HKUST.

Page1 / 32

lec15 - COMP201 Java Programming Part III: Advanced...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online