Download presentation
Presentation is loading. Please wait.
Published byEgbert Chambers Modified over 9 years ago
1
UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material from http://java.sun.com http://java.sun.com and Deitel, Chapter 19
2
What is a Servlet? ä Servlets are somewhat of a server-side analog of client-side applets ä but, servlets have no GUI ä Java module that extends request/response- oriented servers ä Works with Java-enabled web (HTTP) servers that support the Java Servlet API ä executes in a web server thread
3
What is a Servlet’s Context? Java 2 Platform Standard Edition Enterprise Edition Micro Edition
4
What can a Servlet do? ä Handle HTTP client requests ä Handle multiple requests concurrently ä Forward requests to other servers and servlets ä Generate dynamic documents ä Work with databases (e.g. using JDBC)
5
How is a Servlet different from…? ä From Common Gateway Interface (CGI) script? ä Can perform similar tasks, but… ä Servlet: ä provides platform independence ä efficient: executes in a web server thread ä CGI script spawns a new process ä can access server resources
6
How is a Servlet different from…? ä From Java Server Pages (JSP)? JSP: ä is an extension of the Servlet concept ä in principle, can do JSP tasks with a Servlet ä but JSP is more convenient for some tasks ä allows mixing of static HTML with dynamically-generated HTML ä dynamic part is written in Java ä separates display aspects from content ä JSP for display aspects ä Servlet for content management ä supports XML
7
How is a Servlet different from…? ä From JavaScript? JavaScript: ä works on the client side ä generates HTML dynamically on the client side
8
How does a Servlet work? ä invoked via URL in.html file ä URL = web server address + location of Servlet.class file ä executes in a Java-enabled web server thread ä supports the Java Servlet API ä implements the Servlet interface ä extends (typically) HttpServlet Class ä uses HTTP request types/methods: ä GET: gets info. from the web server (e.g. HTML document) ä POST: sends info. to the web server ä overrides HttpServlet Class doGet( ) and/or doPost( ) ä doGet( ), doPost( ) called by HttpServlet service( )
9
How does a Servlet work? ä HttpServletRequest interface (key methods) ä doDelete( ): called for HTTP DELETE request (e.g. file) ä doOptions( ): called for HTTP OPTIONS request ä doPut( ): called for HTTP PUT request ä doTrace(): called for HTTP TRACE request ä String getParameter( String parameterName ) ä Returns value for a parameter sent to Servlet in GET or POST ä Enumeration getParameterNames( ) ä Returns names of all parameters sent to Servlet in POST ä String[ ] getParameterValues( String name ) ä Returns values for a parameter sent to Servlet in GET or POST ä Cookie[ ] getCookies( ): Cookie objects stored on client by server ä HttpSession getSession( boolean create ): client session object
10
How does a Servlet work? ä HttpServletResponse interface (key methods) ä void addCookie( Cookie cookie ) ä ServletOutputStream getOutputStream( ) ä Sets up byte-based output stream for sending data to client ä PrintWriter getWriter( ) ä Sets up character-based output stream for sending data to client ä void setContentType( String type ) ä Specifies type of response to browser ä MIME type (e.g. text/html)
11
Servlet Code Samples & Demos http://kdaniels:8080 Deitel Chapter 19 sample code
12
Resources ä http://java.sun.com ä UML CS Internet & Web Systems courses
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.