Java Servlets

  • Uploaded by: akirank1
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java Servlets as PDF for free.

More details

  • Words: 631
  • Pages: 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 BROWSER

Java Enabled Webserver

HTML

J V

Servlet

M

Data Base

Invoking a servlet • <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)

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() getHeaderNames() getHeader() getDateHeader() getIntHeader() getMethod() getQueryString() getRemoteUser()

• • • •

getRequestURI() getServletPath() getPathInfo() getPathTranslated()

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)

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

Related Documents

Java Servlets
May 2020 9
Java Servlets Ch09
November 2019 9
Servlets
November 2019 10

More Documents from ""

Javascript
May 2020 19
Ch8 Structures
April 2020 24
Ch4 Functions
April 2020 24
Cold Fusion Ii
May 2020 21