aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes can any tell what is passed to init() method... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "can any tell what is passed to init() method..." Watch "can any tell what is passed to init() method..." New topic
Author

can any tell what is passed to init() method...

kajal mukergi
Ranch Hand

Joined: Mar 22, 2007
Posts: 102
hi friends

the process starts like this..

container see the...DD and see the servlet config and it is passed as.. at.. argument to init()

then after that only .. the OBJECT becomes a Servlet..

if i am wrong please correct.. page 103 said so..


and i have doubt...

When they say Servletconfig it ok but if we dont use any servletconfig and we use servletContext only then what is process...

what will be passed to the init()

as we also know we have. init(ServletConfig method..)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
kajal mukergi,

This is not simple answer to give;

FACTs:

ServlertContext is encapsulated in ServletConfig.

So ServletContext is available to Servlet through ServletConfig.
and remember

GenericServlet has one method getServletContext() which will fetch ServletContext from ServletConfig stored in it.


For this I think we have to start with GenericServlet which is extended by our HttpServlet.

GenricServlet has two overloaded init methods

1. init(ServletConfig)
2. init()

**** init(ServletConfig) job:
1. it will store the ServletConfig( into a instance variable).
2. it will call init()


Now In our HttpServlet(extends GenericServlet)
we can do initialization in two ways...

1. override init().
-- its recommended way becuase init(ServletConfig) available to this from Generic Servlet will store ServletConfig amd call this overridden init() (remember this is polymorphic call).

2. override init(ServletConfig)

-- this is not recommended. but if we override our we have to call super
implementation of GenericServlet to store the ServletConfig.
like this super.init(ServletConfig).
After that we can do normal HttpServlet specific initializations.



Hope it makes good base.
[ August 18, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
kajal mukergi
Ranch Hand

Joined: Mar 22, 2007
Posts: 102
Thanks Srinivasan

it is really good.. now i completely.. understood what is the flow.. and what for they are used.....
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 430
Hi,
It is mentioned as the container sees the DD and then look on to seeing the servlet-config. My question is: Where is servlet-config configured in the web.xml file?


Padma priya N.G.
Be the change you want to be - Mahatma Gandhi
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 430
Hi,
It is mentioned as the container sees the DD and then look on to seeing the servlet-config. My question is: Where is servlet-config configured in the web.xml file?
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

There is no <servlet-config> entry in servlet. The servlet configuration in DD refer to <servlet> entry. You can define <init-param> for to define parameters which are accessible through ServletConfig object.

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 430
Hi Narendra,
Thanks for the explanation.
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 430
Hi,
When the container sees the servlet entry, will it automatically pass it as a parameter to the init() method?
Thanks in advance.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Originally posted by Padma priya Gururajan:
Hi,
When the container sees the servlet entry, will it automatically pass it as a parameter to the init() method?
Thanks in advance.


Not getting your question properly. The ServletConfig is meant for to store and provide access to servlet environment, when servlet is initialized in the container.

At the time of initialization, after the execution of constructor , the init (ServletConfig) method is invoked for the servlet. Actual implemtation of ServletConfig interface is provided by the container, which contains the parameter entries defined in DD. And these entries are available in all servlet methods after the init(ServletConfig) method invoked.
After this the init() method is called by init(ServletConfig) method.

So, you can access the servlet configuration object using getServletConfig of servlet in any method of servlet.

ServletConfig config = getServletConfig();
String name = config.getInitParameter("name");
String email = getInitParameter("email");

The corresponding DD entries are

<servlet>
<servlet-name>CheckServeltConfig</servlet-name>
<servlet-class>mypackage.CheckServeltConfig</servlet-class>
<init-param>
<param-name>email</param-name>
<param-value>mymail@myserver.com</param-value>
</init-param>
<init-param>
<param-name>name</param-name>
<param-value>Narendra Dhande</param-value>
</init-param>
<init-param>
</servlet>
<servlet-name>CheckServeltConfig</servlet-name>
<url-pattern>/checkcerveltconfig.do</url-pattern>
</servlet-mapping>

Hope this help

Thanks
krishnamoorthy kitcha
Ranch Hand

Joined: Feb 04, 2006
Posts: 96
Hi padma priya


If you deployed the war file in the container , then it creates the servlet context

while you invoke the servlet it will create the init method

Suppose you are deployed some .war file

It will created context like tmp3555565


While the time of servlet calling only create the init method
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi Priya


Originally posted by Padma priya Gururajan:
When the container sees the servlet entry, will it automatically pass it as a parameter to the init() method?


Actually It never passes anything as a parameter directly to the servelet. It adds them as parameters to the either to the ServletConfig or ServletContext (container generated) objects depending upon the declaration in web.xml . And since we can access these objects within the init() method, we can access those parameters via these objects.

Regards,
Khushhal


rgrds,
Khushhal
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 430
Hi,
After adding init-param as parameters to the ServletConfig and ServletContext objects, are the ServletRequest and ServletResponse objects created? Or, ServletRequest and ServletResponse objects created before passing as parameters? Which is the order of precedence?
Thanks.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Originally posted by Padma priya Gururajan:
Hi,
After adding init-param as parameters to the ServletConfig and ServletContext objects, are the ServletRequest and ServletResponse objects created? Or, ServletRequest and ServletResponse objects created before passing as parameters? Which is the order of precedence?
Thanks.


ServletConfig and SevletContext onjects are created at the time of the servlet in initalized in container. After the initialization of servlet, the servlet is ready to take the client requests. These objects are available to all requests processed by this servlet.

The ServletRequest and ServletResponse objects are created when container passed the client request to the client. The scope of these objects are limited to the client request.

The init-param are not added to the ServletConfig or ServletContext. But there are references to param objects in these object, and you can access the environment throu these object methods.

Thanks
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 430
Hi Narendra,
Thanks for the explanation.
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 430
The ServletRequest and ServletResponse objects are created when container passed the client request to the client. The scope of these objects are limited to the client request.



Can you please elaborate on this?
Thanks.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

Take the following sequence roughly for the request execution. For more details see the specification how the request is processed.

1. The client send the HTTP request to server ( GET/POST etc.)
2. The web server found that the requset is for servlet /JSP and passed the request to servlet container.
3. The servlet container create the HttpServletRequest object for the incoming request, and initialized it for the request parameters supplied through client request, also it create HttpServletResponse object for the client request.
4. The the container pass both objects as a parameter to doGet/doPost (service method) method depending on the type of request.
5. After the request is processed by these method the response is passed back to web server.
6. The HttpservletRequest and HttpServletResponse objects are discarded.
7. In the mean time the response is passed to client throu' web server.

Hope this help

Thanks
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 430
Hi Narendra,
Thanks for the explantion. Now, I understood it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can any tell what is passed to init() method...
 
Similar Threads
init method of servlets
Regarding the init() methods
Why is ServletConfig obj passed to init() of GenericServlet when the class implements the interface?
Questions about ServletContext and ServletConfig
init() and http related questions