1
2
3
•The Servlet interface defines methods to initialize a Servlet, to service requests, and to remove a Servlet from the server. These are known as life-cycle methods and are called in the following sequence. •The getServletInfo() method allows the Servlet to return basic information about itself, such as author, version, and copyright. •GenericServlet defines a generic, protocol-independent Servlet. •GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle methods init and destroy. •To write a generic servlet, you need only override the abstract service method.
•HttpServlet provides an abstract class to create an HTTP servlet. •A subclass of HttpServlet must override at least one method, usually one of these: doGet, if the servlet supports HTTP GET requests
doPost, for HTTP POST requests doPut, for HTTP PUT requests doDelete, for HTTP DELETE requests
4
Servlet programmers implement their servlet classes simply by extending the HttpServlet class and overriding the desired methods.
5
6
7
8
9
•Override doGet() method of HttpServlet: Client should be able to access the servlet using address bar or hyperlink. •Specify the content type generated by the servlet “text/html”. •Open a Character stream to the response using response.getWriter(); •To send character data, use the PrintWriter object returned by getWriter() •To send binary data in a MIME body response, use the ServletOutputStream returned by getOutputStream(). •Write dynamic content to the response. •MIME: Multipurpose Internet Mail Extensions (MIME), The content types defined by MIME standards are important for communication protocols like HTTP for the World Wide Web. •Default MIME for servlets is “text/plain”.
10
Refer: http://download.oracle.com/docs/cd/E13222_01/wls/docs81/webapp/web_xml.html These deployment descriptors are validated using DTD or Schema.
Note:
The persistence.xml is the deployment descriptor for Java Persistence API. This file should reside in META-INF folder. The application.xml is the deployment descriptor for complete Enterprise application. This file should reside in META-INF folder.
11
12
Address Bar -> http://server:port:webApplicationName/url-pattern a) http: Specifies the protocol used to make a request to web server b) //server: here localhost specifies that the server is running on the same machine from where client is making a request. [ can also use //127.0.0.1 (loop back address) instead of //localhost.
c) port: Port 80 is the default HTTP port. The port 8080 is the default port number for the tomcat which is configured to listen on , it can be modified to use other port number by making changes in server.xml. d) webApplicationName: BasicWebApp is the web application created which contains the resources. e) /url-pattern: /Sample is the url-pattern which we have configured to invoke com.mindtree.web.SampleServlet servlet. You can also use wild-card for URL pattern.
Example : “*.do” means any URL ending with “.do” will invoke the servlet. “login.do”, “register.do” will call a servlet whose url pattern is configured as “*.do”.
13
Refer – Using the Eclipse Web Tools Platform with Apache Tomcat - http://goo.gl/CmKdN (Link to the tutorial) – This tutorial should be referred in addition to the above mentioned video tutorial.
14
Web Container identifies all web applications based on their structure. Web Container parses WEB-INF/web.xml, and loads all the servlets configured in that DD file. For every Servlet one ServletConfig object is created. Web Container creates an instance of Servlet by invoking no argument constructor. Web Container invokes init(ServletConfig config) method, this method inturn invokes init() with no arguments. As a programmer you need to override init() for any one-time initialization. Once execution of init() method is complete, clients can make a request to the servlet.
15
The Web container manages the life cycle of a servlet instance. These methods should not be called by your code. The init method is called by the Web container when the servlet instance is first created. No requests will be processed by this servlet until the init method has completed.
The Web container calls the init(config) method, which is handled by the GenericServlet class. This method stores the config object and then calls the init() method. Override the init() method in your servlet class. Do not override the init(config) method. The service() method is called by the Web container to process a user request. The HttpServlet class implements the service method by dispatching to doGet, doPost, and so on depending on the HTTP request method (GET, POST,HEAD, and so on).
The destroy method is called by the Web container when the servlet instance is being eliminated. All requests will be completely processed before the destroy method is called.
16
17
•When Client makes a GET request, Web container creates ServletRequest and ServletResponse for that GET request and associates these objects to the Thread. •Web Container calls service(ServletRequest,ServletResponse) method of Servlet instance. •service(ServletRequest,ServletResponse) method inturn calls service(HttpServletRequest,HttpServletResponse) method. •service(HttpServletRequest,HttpServletResponse) method calls doGet(HttpServletRequest,HttpServletResponse). •The doGet() uses the request and response objects created by the container. Once response is committed, the servlet request and servlet response objects are created by the container.
18
•When Client makes a POST request, Web container creates ServletRequest and ServletResponse for that POST request and associates these objects to the Thread. •Web Container calls service(ServletRequest,ServletResponse) method of Servlet instance. •service(ServletRequest,ServletResponse) method inturn calls service(HttpServletRequest,HttpServletResponse) method. •service(HttpServletRequest,HttpServletResponse) method calls doPost(HttpServletRequest,HttpServletResponse). •The doPost() uses the request and response objects created by the container. Once response is committed, the servlet request and servlet response objects are created by the container.
19
Refer: http://download.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServletRequest.ht ml for description of other methods Some other important methods: Enumeration getParameterNames();
String getMethod(); Cookie[] getCookies() RequestDispatcher getRequestDispatcher(String path); String getRequestURI(); Enumeration getHeaderNames();
20
Refer: http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.ht ml. For information of other HttpServletResponse methods Some other important methods: void setContentLength(int len);
void setContentType(String type); String encodeURL(String url); String encodeRedirectURL(String url); void addCookie(Cookie cookie);
21
22
Refer :http://download.oracle.com/javaee/6/api/javax/servlet/ServletConfig.html for information about methods of ServletConfig.
23
•The method public void init(ServletConfig config) is called by the servlet container to indicate to a servlet that the servlet is being placed into service. This implementation stores the ServletConfig object it receives from the servlet container for later use. •The public void init() method should be overridden, this method is called by init(ServletConfig config). •The ServletConfig object can be retrieved using getServletConfig().
24
25
•Do not implement the Single Thread Model interface. •This practice causes the Web container to create multiple servlet instances, one per request. This could cause severe performance problems. •It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources.
26
27
Answer: 1) C 2) c, d
28
Answer: 3) C 4) c
29
Answer: 5) FALSE 6) TRUE 7) EJB
30
Answer: 8) C 9) C 10) “image/jpeg”
31
11) User interaction 12) doGet(HttpServletRequest req, HttpServletReponse res)
32
13) One of the reasons that servlets scale so well is that only one copy is in memory, even when several requests occur at once. Threading handles the sharing and turntaking issues for you. A is incorrect because no error messages are sent when servlets are shared. B is incorrect because two requests can be handled at the same time. C is incorrect because two copies will not be created.
33
34
35
36
37
38
39
40
41
42