20
SERVLETS

Servlets

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Servlets

SERVLETS

Page 2: Servlets

Servlet

• Platform- independent Server side components , written in Java which extends the standard web server and provide a general framework for services build on the Request –Response paradigm

• An Efficient and Powerful technology for creating dynamic content for the Web and fundamental part of all Java web technologies

Page 3: Servlets

Servlet Container

The servlet container is a part of a Web server or application server that provides the network services over which requests and responses are sent, decodes MIME-based requests, and formats MIME-based responses. A servlet container also manages servlets through their lifecycle.

Page 4: Servlets
Page 5: Servlets
Page 6: Servlets

Servlet API

Page 7: Servlets

javax.servlet.GenericServlet• Signature: public abstract class GenericServlet extends

java.lang.Object implements Servlet, ServletConfig, java.io.Serializable

• GenericServlet defines a generic, protocol-independent servlet.

• GenericServlet gives a blueprint and makes writing servlet easier.

• GenericServlet implements the log method, declared in the ServletContext interface.

• To write a generic servlet, it is sufficient to override the abstract service method.

Page 8: Servlets

javax.servlet.http.HttpServlet• Signature: public abstract class HttpServlet extends

GenericServlet implements java.io.Serializable• HttpServlet defines a HTTP protocol specific servlet. • HttpServlet gives a blueprint for Http servlet and makes

writing them easier. • HttpServlet extends the GenericServlet and hence

inherits the properties GenericServlet.

Page 9: Servlets

Servlet Life Cycle

init()

...()service(

)

doGet()

doPost()

doDelete()

destroy()

doPut()

New Destroyed

Running

The Web container manages the life cycle of servlet instances

Page 10: Servlets

The init ( ) method• Called by the Web container when the servlet instance is first created• The Servlets specification guarantees that no requests will be processed by

this servlet until the init method has completed• Override the init() method when:

i) You need to create or open any servlet-specific resources that you need for processing user requests

ii)You need to initialize the state of the servlet

//A simple servlet lifecycle counter

// int count ;

public void init(ServletConfig config) throws ServletException {

super.init(config);getServletContext().log("init() called");count=0;

}

Page 11: Servlets

The service ( ) method

• Called by the Web container to process a user request• Dispatches the HTTP requests to doGet(…), doPost(…), etc. depending on the HTTP request method (GET, POST, and so on)– Sends the result as HTTP response

• Usually we do not need to override this method

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

getServletContext().log("service() called");count++;response.getWriter().write("Incrementig the count: Count = "+count);

}

Page 12: Servlets

The destroy() Method

• Called by the Web container when the servlet instance is being eliminated ( All threads within the servlets service method have exited (or) Time-out period have passed )

• The Servlet specification guarantees that all requests will be completely processed before this method is called

• Override the destroy method when:– You need to release any servlet-specific resources that you had opened in the

init() method– You need to persist the state of the servlet

public void destroy() {getServletContext().log("destroy() called");

}

Page 13: Servlets

servletConfig

• Object of servletConfig used to pass initialization and context information to servlets

• getServletConfig ( ) method allows the servlet to retrieve the object and obtain configuration at anytime

• Init () method stores the servletConfig object in a private transient instance variable called config

Page 14: Servlets

ServletContext

• The javax.servlet.ServletContext interface provides a set of methods that the servlet can use to communicate with the web server.

• The ServletContext object is contained within javax.servlet.ServletConfig Object which is provided to the servlet when it is initialized.

• Define URI to name mappings• Allow Data to be shared between different servlets• Contains a servlet log• Access to global servlet config and other application servlet context

are accessible• It communicates with the server in a non request specific manner

Page 15: Servlets

Http Request and Response Structure

• HTTP is a stateless protocol.• Server does not have the overhead of tracking client

connections.• HTTP transactions are either a request or response.• Servlet can overcome the stateless nature of HTTP by

tracking client state using session information stored in URL, hidden fields or cookies.

Page 16: Servlets

Http Transactions

A single request or response line– <HTTP Method>/<document address>HTTP/

<Version No> e.g.– GET /index.html HTTP/1.1– Response line contains an HTTP status code.HTTP/1.1 200 OKDate:……………………….GMTServer:Last-modified:Content-type: text/htmlContent-length:….bytes

Page 17: Servlets

Http Servlet Methods

• doGet()• doPost()• doHead()• doDelete()• doOptions()• doPut()• doTrace()• getLastModified()• service()

Page 18: Servlets

Http Servlet Request

• getMethod()– Returns get/post with which request was made.

• getQueryString()• getRemoteUser()• getRequestSessionId()• getSession(boolean)

– If FALSE returns current valid session otherwise creates a new session.

• isRequestedSessionIdValid()• getCookies()

Page 19: Servlets

Http Servlet Response

• addCookie(Cookie)– Add cookie to response

• encodeUrl(String)• sendRedirect(String)• sendError(int)

Page 20: Servlets

Thread Safety

• Multithreaded = one servlet, multiple requests simultaneously

• Thread safe – not using class variables since one copy of these variables is shared by all threads

• Synchronized code blocks , all threads wait until they can enter, one at a time

• Servlet 2.4 deprecates SingleThreadModel interface – could not resolve all potential threading issues.