Download presentation
Presentation is loading. Please wait.
1
Servlets CS-328 Dick Steflik
2
What is a servlet A Java application run on a thread of the webserver in response to an HTTP GET or POST request. The servlet is used to generate Dynamic HTML to return to the requesting browser
3
The Servlet Model BROWSER Java Enabled Webserver Servlet JVMJVM HTTP Get or Post HTML Data Base
4
Invoking a servlet tag –webserver must be enabled for server side embeds “action” attribute of tag –can be GET or POST (HTTP 1.0) –can also be “PUT” (HTTP 1.1)
5
Servlets Provide Protocol independent and platform independent server side components A Common API (Servlet API) Dynamic loading either locally or over the network Security (servlet security model) More safe than CGI scripts –runs as thread not as child process
6
Life cycle Once invoked a servlet has a life cycle that is supported by a set of life cycle methods that are automatically run by the webserver –init() - run when initially loaded –service() - for performing the servlets service –destroy() - for destroying the servlet object –doGet() - only for HTTP servlets and is used for processing HTTP GET requests –doPost() - only for HTTP servlets and is used for processing HTTP POST requests
7
Servlet Interface All servlets must implement the Servlet interface Methods: –init() –service() –destroy() –getServletConfig() –getServletInfo()
8
ServletConfig Interface Implemented by network services to pass configuration info to a servlet when it is first loaded Methods: –getInitParameter(String name) - returns a String value of the requested parameter –getInitParameterNames() - returns an enumeration of the names of the initialization parameters –getServletContext() - returns a ServerContext object that contains the servlet’s context
9
ServletContext Interface Implemented by network services, gives servlets access to environmental information and allows logging of events. Methods: –getServlet() - returns an initialized and ready to run Servlet object. –getServlets() - returns an enumeration of all of the Servlet objects in this server that are in the same name space (always includes the servlet itself) –getServerInfo() - returns info about the network service the Servlet is running under –getAttribute(String name) - returns the value of the named attribute –getRealPath(String name) - returns the actual path of the named virtual path –getMimeType(String file) - returns mime type of the named file –log(String msg) - writes msg into the servlet log file
10
ServletRequest Interface Provides means to get information from the HTTP request header Methods: –getProtocol() –getScheme() –getParameterNames() –getParameter() –getParameterValues() –getAttribute() –getContentLength() –getContentType() –getInputStream() –getRemoteAddr() –getRemoteHost()
11
(more) getServerName() getServerPort() getRealPath()
12
ServletResponse Interface Provides means to build HTTP response header Methods: –setContentLength(int len) –setContentType(String type) –getOutputStream() - returns a ServletOutputStream for writing the response data
13
GenericServlet Class Abstract class that implements Servlet and ServletConfig
14
HttpServlet Class Abstract class that extends GenericServlet Class to provide a framework for the HTTP Protocol doGet(HttpServletRequest req, HttpServletResponse resp) doPost (HttpServletRequest req, HttpServletResponse resp) getLastModified((HttpServletRequest req)
15
HttpServletRequest Interface getAuthType() getHeaderNames() getHeader() getDateHeader() getIntHeader() getMethod() getQueryString() getRemoteUser() getRequestURI() getServletPath() getPathInfo() getPathTranslated()
16
HttpServletResponse Interface containsHeader(String name) setHeader(String name,String value) setDateHeader(String name, long value) setIntHeader(String name, int value) setStatus(int sc) setStatus(int sc, String msg) sendError(int sc) sendError(int sc, String msg) sendRedirect(String location)
17
HttpUtils Class getRequestURL(HttpServletRequest req) parsePostData(int ServletStream) - returns a hash table containing the name/value pairs parseQueryString(String s) - returns a hash table containing the name/value pairs
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.