• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..)
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
kajal mukergi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Srinivasan

it is really good.. now i completely.. understood what is the flow.. and what for they are used.....
 
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Narendra,
Thanks for the explanation.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Narendra,
Thanks for the explanation.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Narendra,
Thanks for the explantion. Now, I understood it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic