Post on 08-Nov-2015
description
Java Servlets
What Are Servlets?Basically, a java program that runs on the serverCreates dynamic web pages
What Do They Do?Handle data/requests sent by users (clients)Create and format resultsSend results back to user
Who Uses Servlets?Servlets are useful in many business oriented websites
and MANY others
HistoryDynamic websites were often created with CGICGI: Common Gateway InterfacePoor solution to todays needsA better solution was needed
Servlets vs. CGIServlet AdvantagesEfficientSingle lightweight java thread handles multiple requestsOptimizations such as computation caching and keeping connections to databases openConvenientMany programmers today already know javaPowerfulCan talk directly to the web serverShare data with other servletsMaintain data from request to requestPortableJava is supported by every major web browser (through plugins)InexpensiveAdding servlet support to a server is cheap or free
Servlets vs. CGICGI AdvantagesCGI scripts can be written in any languageDoes not depend on servlet-enabled server
What Servlets NeedJavaServer Web Development Kit (JSWDK)Servlet capable serverJava Server Pages (JSP)Servlet code
Java Server Web Development KitJSWDKSmall, stand-alone server for testing servlets and JSP pagesThe J2EE SDKIncludes Java Servlets 3.0
Servlet capable serverApachePopular, open-source serverTomcatA servlet container used with ApacheGlass FishOpen-source application server by Sun Microsystems
BrowserHTTP ServerStatic ContentServletContainerHTTP RequestHTTP ResponseServletServlet Container Architecture
ReceiveRequestis servletloaded?is servletcurrent?Send ResponseProcess RequestLoad ServletYesYesNoNoHow Servlets Work
Types of ServletGeneric Servlet javax.servlet (package)extends javax.servlet.Servlet service methodHttp Servletjavax.servlet.http (package)extends javax.servlet.HttpServletdoget(), doPost().
Types of servlets (cont..)Generic servletservice(Request, Response) throws ServletException, IOExceptionHttpServletdoGet(HttpServletRequest req, HttpServletResponse res)
GenericServletservice ( )ServerClientHTTPServletservice ( )HTTP ServerBrowserrequestresponsedoGet ( )doPost( )requestresponseGeneric Servlet & HTTP Servlet
Servlet CodeWritten in standard JavaImplement the javax.servlet.Servlet interface
Main Concepts of Servlet ProgrammingLife CycleClient InteractionSaving StateServlet CommunicationCalling ServletsRequest Attributes and ResourcesMultithreading
Life CycleInitializeServiceDestroy
Life Cycle: InitializeServlet is created when servlet container receives a request from the client
Init() method is called only once
Life Cycle: ServiceAny requests will be forwarded to the service() methoddoGet()doPost()doDelete()doOptions()doPut()doTrace()
Life Cycle: Destroydestroy() method is called only onceOccurs whenApplication is stoppedServlet container shuts downAllows resources to be freed
Client InteractionRequestClient (browser) sends a request containingRequest line (method type, URL, protocol)Header variables (optional)Message body (optional)ResponseSent by server to clientresponse line (server protocol and status code) header variables (server and response information) message body (response, such as HTML)
Saving StateSession TrackingA mechanism that servlets use to maintain state about a series of requests from the same user (browser) across some period of time. CookiesA mechanism that a servlet uses to have clients hold a small amount of state-information associated with the user.
Calling ServletsTyping a servlet URL into a browser window Servlets can be called directly by typing their URL into a browser's location window.Calling a servlet from within an HTML pageServlet URLs can be used in HTML tags, where a URL for a CGI-bin script or file URL might be found.
Request Attributes and ResourcesRequest AttributesgetAttributegetAttributeNames setAttribute Request Resources - gives you access to external resourcesgetResourcegetResourceAsStream
*Adapted fromhttp://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Overview.html*Adapted fromwww.cis.upenn.edu/~matuszek/ cit597-2004/Lectures/21-servlets.ppt****Adapted fromhttp://learning.unl.ac.uk/im269/lectures/week6servletsp1.ppt