70 likes | 85 Views
In this chapter, students will learn about the general architecture and purpose of servlets, as well as how to create a basic servlet. Topics covered include servlet engines, responding to HTTP requests, passing parameters, generating output, servlet life cycle, and getting parameters from the request.
E N D
At the end of this chapter students will: • Know the general architecture and purpose of servlets • Understand how to create a basic servlet Objectives Ch. D - 1
A servlet is a Java object which resides within a servlet engine. A servlet engine is usually contained within a web server. Servlets are Java objects which respond to HTTP requests. Servlets may return data of any type but they often return HTML. Servlets are invoked through a URL which means that a servlet can be invoked from a browser. Servlets can be passed parameters via the HTTP request. http://www.somehost.com/servlet/search?word=Java&language=english Servlets - What are they? Servlet called search Keyword “servlet” indicates to web server that this request is for a servlet. Parameters encoded in an HTTP request Ch. D - 2
All servlets must import the following packages: • import java.io.*; • import javax.servlet.*; • import javax.servlet.http.*; • All servlets must extend the HttpServlet or Servlet class public class MyServlet extends HttpServlet • Servlets must provide and implementation for doGet, doPost or both. • public void doGet(HttpServletRequest request, • HttpServletResponse response) throws • ServletException, IOException • { • // … Getting Started Ch. D - 3
Set the content type of the return message. NOTE: this must be set before any data is returned from the request response.setContentType(“text/html”); HTML can be written as text to the response. Obtain the PrintWriter object from the response and write text to it PrintWriter out = response.getWriter(); out.println(“<HTML><HEAD><TITLE>Test</TITLE>”); out.println(“<BODY>><H1>My FirstServlet</H1>”); out.println(“</BODY></HTML>”); out.close(); Generating Output Ch. D - 4
Under HTTP, there are two methods available for sending parameters from the browser to the web server. They are POST and GET POST and GET are virtually the same except in terms of how parameter data is passed GET: Parameters are encoded within the URL. If data is passed via GET, it is limited in size to 2K POST: Parameters are sent AFTER the HTTP header in the request. There is no limit to the size of parameters sent via POST. The method of parameter passing is defined in the HTML loaded into the browser. Servlet authors can choose to implement the doGet method, doPost method or both. doPost and doGet Ch. D - 5
When a servlet is FIRST requested, it is loaded into the servlet engine. The init() method of the servlet is invoked so that the servlet may initialize itself. Once initialization is complete, the request is then forwarded to the appropriate method (ie. doGet or doPost) The servlet is then held in memory. Subsequent requests are simply forwarded to the servlet object. When the engine wishes to remove the servlet, its destroy() method is invoked. NOTE: Servlets can receive multiple requests for multiple clients at any given time. Therefore, servlets must be thread safe Servlet Life Cycle Ch. D - 6
All HTTP requests can contain data. • Data is in the form key=value • All data and keys are of String type • All HTTP data are encoded in a Hashtable and included with the request. • If the servlet knows the names of the keys, it can request the value of the given key • String name = request.getParameter(“name”); • If the key names are not know, the servlet can enumerate (iterate) through the keys and obtain data in that manner • Enumeration enum = request.getParameterNames(); • while(enum.hasMoreElements()) { • String key = (String) enum.nextElement(); • // … Getting Parameters from the Request Ch. D - 7