You are on page 1of 17

Servlets

CS-328
Dick Steflik
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
content to return to the requesting browser
– may be HTML
– may be XML
– may be POT
The Servlet Model

HTTP
Get or Post
Java
BROWSER J
Enabled Servlet
V
Webserver
M
HTML

Data
Base
Invoking a servlet

• <servlet> tag
– webserver must be enabled for server side
embeds
• “action” attribute of <form> tag
– can be GET or POST (HTTP 1.0)
– can also be “PUT” (HTTP 1.1)
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
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
Servlet Interface

• All servlets must implement the Servlet


interface
• Methods:
– init()
– service()
– destroy()
– getServletConfig()
– getServletInfo()
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
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

ServletRequest Interface
• Provides means to get information from the HTTP request header
• Methods:
– getProtocol()
– getScheme()
– getParameterNames()
– getParameter()
– getParameterValues()
– getAttribute()
– getContentLength()
– getContentType()
– getInputStream()
– getRemoteAddr()
– getRemoteHost()
(more)
• getServerName()
• getServerPort()
• getRealPath()
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
GenericServlet Class

• Abstract class that implements Servlet and


ServletConfig
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)
HttpServletRequest Interface
• getAuthType() • getRequestURI()
• getHeaderNames() • getServletPath()
• getHeader() • getPathInfo()
• getDateHeader() • getPathTranslated()
• getIntHeader()
• getMethod()
• getQueryString()
• getRemoteUser()
HttpServletResponse Interface
• containsHeader(String • setStatus(int sc, String
name) msg)
• setHeader(String • sendError(int sc)
name,String value) • sendError(int sc, String
• setDateHeader(String msg)
name, long value) • sendRedirect(String
• setIntHeader(String name, location)
int value)
• setStatus(int sc)
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

You might also like