Anda di halaman 1dari 5

Important objects in Servlet Programming : ServletContext: Whenever an application is deployed Servlet Engine creates ServletContext object.

There is only one object of this type. All the servlets share this object. ServletContext object is not directly available for servlets. To get its reference we have to call the ServletContext sc=getServletContext(); Even though the above method is called on the servlet instance, internally it is called on the ServletConfig object i.e.getServletContext() belongs to ServletConfig object. ServletContext object can act as shared informatory repository for application level data. One servlet can store data into this object and another servlet of the same application can access that data. A servlet uses this object for the following purposes: 1.To communicate with the web container 2.To talk to other servlets 3.To share data (in the form of attributes)with other servlets. 4.To get application level initialiation parameters known as context parameters. Some important methods:

1.getServerInfo(): Whenever a servlet wants to know the web container details in which it is deployed, it can call this method on the ServletContext object. 2. log() : To write any informaton into container log files we can call log() on 'sc' object.(ServletContext object) 3.setAttribute() : One servlet can store a data item into the ServletContext object by calling this method. 4. getAttribute() : one servlet can get the data stored by another servlet by calling getAttribute method on sc object. 5.getRequestDispatcher(): Whenever a servlet wants to communicate with other servlet, it can call this method on the ServletContext object which produces RequestDispatcher. Using this object one servlet can communicate with other servlets of the same web application. 6.getInitParameter() : Initialization parameters are individual for each servlet. They are supplied by web.xml. A servlet retrieves them by calling getInitParameter() on the ServletConfig object. When we want to supply common init parameters for all the servlets we use context parameters. <context-param> <param-name>anyname</param-name> <param-value>anyvalue</param-value>

</context-param>

On the ServletContext we call getInitParameter() to retrieve the context parameters.

RequestDispatcher object: RequestDispatcher object is used in the servlets to implement request dispatching. A servlet receives the client request, does the processing partially and hands over the request processing duty to another servlet. This mechanism is known as request dispatching. Inter servlet communication is implemented using RequestDispatcher object. In a servlet we can get RequestDispatcher object references in two ways: 1.RequestDispatcher rd=context.getRequestDispatcher(String absolutepath); 2 RequestDispatcher rd=request.getRequestDispatcher(String relativepath); If we are using the ServletContext object to get the RequestDispatcher, we have to give the absolute URL of the target resource. If we are using HttpServletRequest to get the RequestDispatcher object, we give only relative url of the target resource. Request dispatching can be implemented in two ways:

1. forward mechanism 2. include methanism In forward mechanism, a servlet receives the client request , does the preliminary processing and request processing duty is delegated to another servlet/jsp. The target resource is responsible to send the response to the client. To implement forward mechanism the code to be written is: ServletContext sc=getServletContext(); RequestDispatcher rd=sc.getRequestDispatcher("/targetservletpublicname"); rd.forward(request,response); In include mechanism, the content of a target resource(a Servlet, a jsp, an html file) is included in the current servlet response. In essence, this mechanism enables programmatic server side-includes. In this mechanism the first servlet that receives the client request is responsible to send the response to the client. To implement include mechanism the code to be written is: ServletContext sc=getServletContext(); RequestDispatcher rd=sc.getRequestDispatcher("/targetservletpublicname"); rd.forward(request,response); In an object oriented system, objects communicate with message passing. One object gets the reference of the

other object and makes a method call(send a message) on teh other object. This is how inter-object communication occurs.In case o servlets, one servlet instance cannot get the reference of the other servlet instance. More over, oner servlet cannot call the life cycle methods of other servlets. Through RequestDispatcher object such communication is made possible in servlets.

Anda mungkin juga menyukai