42
1

Servlet and Jsp Technology -Part1

Embed Size (px)

DESCRIPTION

Servlet and Jsp Technology -Part1

Citation preview

Page 1: Servlet and Jsp Technology -Part1

1

Page 2: Servlet and Jsp Technology -Part1

2

Page 3: Servlet and Jsp Technology -Part1

3

Page 4: Servlet and Jsp Technology -Part1

•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

Page 5: Servlet and Jsp Technology -Part1

Servlet programmers implement their servlet classes simply by extending the HttpServlet class and overriding the desired methods.

5

Page 6: Servlet and Jsp Technology -Part1

6

Page 7: Servlet and Jsp Technology -Part1

7

Page 8: Servlet and Jsp Technology -Part1

8

Page 9: Servlet and Jsp Technology -Part1

9

Page 10: Servlet and Jsp Technology -Part1

•Override doGet() method of HttpServlet: Client should be able to access the servletusing 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 ServletOutputStreamreturned 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

Page 11: Servlet and Jsp Technology -Part1

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

Page 12: Servlet and Jsp Technology -Part1

12

Page 13: Servlet and Jsp Technology -Part1

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

Page 14: Servlet and Jsp Technology -Part1

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

Page 15: Servlet and Jsp Technology -Part1

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

Page 16: Servlet and Jsp Technology -Part1

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

Page 17: Servlet and Jsp Technology -Part1

17

Page 18: Servlet and Jsp Technology -Part1

•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

Page 19: Servlet and Jsp Technology -Part1

•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

Page 20: Servlet and Jsp Technology -Part1

Refer: http://download.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServletRequest.html for description of other methods

Some other important methods:

Enumeration getParameterNames();

String getMethod();

Cookie[] getCookies()

RequestDispatcher getRequestDispatcher(String path);

String getRequestURI();

Enumeration getHeaderNames();

20

Page 21: Servlet and Jsp Technology -Part1

Refer: http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html.

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

Page 22: Servlet and Jsp Technology -Part1

22

Page 23: Servlet and Jsp Technology -Part1

Refer :http://download.oracle.com/javaee/6/api/javax/servlet/ServletConfig.html for information about methods of ServletConfig.

23

Page 24: Servlet and Jsp Technology -Part1

•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

Page 25: Servlet and Jsp Technology -Part1

25

Page 26: Servlet and Jsp Technology -Part1

•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

Page 27: Servlet and Jsp Technology -Part1

27

Page 28: Servlet and Jsp Technology -Part1

Answer:

1) C

2) c, d

28

Page 29: Servlet and Jsp Technology -Part1

Answer:

3) C

4) c

29

Page 30: Servlet and Jsp Technology -Part1

Answer:

5) FALSE

6) TRUE

7) EJB

30

Page 31: Servlet and Jsp Technology -Part1

Answer:

8) C

9) C

10) “image/jpeg”

31

Page 32: Servlet and Jsp Technology -Part1

11) User interaction

12) doGet(HttpServletRequest req, HttpServletReponse res)

32

Page 33: Servlet and Jsp Technology -Part1

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 turn-taking 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

Page 34: Servlet and Jsp Technology -Part1

34

Page 35: Servlet and Jsp Technology -Part1

35

Page 36: Servlet and Jsp Technology -Part1

36

Page 37: Servlet and Jsp Technology -Part1

37

Page 38: Servlet and Jsp Technology -Part1

38

Page 39: Servlet and Jsp Technology -Part1

39

Page 40: Servlet and Jsp Technology -Part1

40

Page 41: Servlet and Jsp Technology -Part1

41

Page 42: Servlet and Jsp Technology -Part1

42