• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

call third servlet

in servlet chaining i have 5 servlet.

supoose i want to call 3 servlet directly at that time what i would do.


when public void init method is called.


use of destroy method.


when i restart the server how could i call init method
0
Manikandan Thiagarajan
Asked:
Manikandan Thiagarajan
  • 7
  • 6
  • 5
3 Solutions
 
rrzCommented:
>in servlet chaining  
Please tell us what you are trying to accomplish. Maybe you could use Filters.  Look at
http://www-128.ibm.com/developerworks/java/library/j-tomcat2/
>when public void init method is called.  
Could be at start up of your Servlet container or when a request is made for the Servlet. Look at
http://www.stardeveloper.com/articles/display.html?article=2001061901&page=1 
>use of destroy method.  
see last link
>when i restart the server how could i call init method  
You don't call it. The Servlet container calls it. You can arrange for it to be called at startup by setting the load-on-startup tag , see last link  
Please ask follow up questions here.   rrz
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
could you explain with simple

what is the need for load on start up  tag
0
 
muthu_tigerfireCommented:
The servlet will be loaded by the class(loaded by the class loader of the container) then the
init method is called by the container after the class is loaded.
The basic necessity of init method is that u can never define constructor in ur servlet.(this will be provided by the containter)
So we need some place to initialize the servlet.(like creating connection object to a database etc..)
and init method is the perfect place to do it.
U also get a servletconfig in ur init method to get all the init configuration form ur web.xml file.....

Destory method is just opposite to the init method.
Once the servlet has finished on its init method it is said to be initialized.
Then its service method will be called by the container when ever a request for that servlet comes in..
When the container feels that this servlet should be unloaded it calls the destroy method first. So it is like a destructor in c++..
u can clean up all the resources that u got in init method...


Regarding loadonstartup tag.....

i need to tell u about the containers loading policy..
the servlet spec says that the servlet should be loaded when the first request comes in..
this can be done in two ways either it can load the servlet at the startup only or can load the servlet when the first request comes in...
but most of the containers gives us an option to make the servlet load when the container loads.

this is where the load on startup tags comes into picture..
if u provide load on startup tag in ur servlet declaration in the web.xml then this servlet will be loaded when the container starts only ..this pervents
the loading on the first request so that the first request will also be faster...
if this is not provided and consider u r getting database connections in ur init method...
then at the first request the container will create that connection and the first request will take a lot of time...



regarding calling init method ...
u will never call a servlets init method ... this will be called by the container when the servlet is loaded...
at any case the servlets service method wont be called before the init method completes...
in other words only when the init method is over the servlet will be in servicing state...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Manikandan ThiagarajanSenior consultantAuthor Commented:
<web-app>

  <servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>com.stardeveloper.servlets.TestServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

</web-app>


According to that web.xml, what specification i want to specify in  load on start up tag
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
could you specify  the code how could i initialize database connection in public void init

what is the use of config object in public void init
0
 
muthu_tigerfireCommented:
Hi stmani2005,


The load-on-startup element indicates that this
servlet should be loaded (instantiated and have
its init() called) on the startup of the web
application. The optional contents of these
element must be an integer indicating the order in
which the servlet should be loaded. If the value
is a negative integer, or the element is not
present, the container is free to load the servlet
whenever it chooses. If the value is a positive
integer or 0, the container must load and
initialize the servlet as the application is
deployed. The container must guarantee that
servlets marked with lower integers are loaded
before servlets marked with higher integers. The
container may choose the order of loading of
servlets with the same load-on-start-up value.

Cheers!
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
in servlet chaining i have  5 servlets

i want to call 3rd servlet instead of first servlet

like the order should be 3-4-5

what i specify in upload start up tag
0
 
muthu_tigerfireCommented:
Hi stmani2005,
what do u mean by creating database connection..u can create database connection in a number of ways..
by using datasource or by creating connection using the driver manager... any way...

the ServletConfig object that is the parameter for the init method have a function as

getInitParameter(String)

which can be used to retrieve the values of the parameter that is specified in the web.xml...

u can specify the parameter in the web.xml using two tags as follows

<init-param>
<param-name>ConnectionString</param-name>
<param-value>theconnectionstringtobeused</param-value>
</init-param>

after this in the init method u can get the value as follows...


public void init(ServletConfig config)
{
String connectionurl = config.getInitParameter(ConnectionString);
}

Cheers!
0
 
muthu_tigerfireCommented:
Hi stmani2005,
what do u mean by calling...
u can call any servlet at any time...
u need to use requestdispatcher(servletname).forward(reqest,response);

it is containers job to load the servlet before calling its service method...
actually u dont have to specify any load on startup tag at all for servlet chaining...

Cheers!
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
for deployment purpose load on start up tag is used

is it correct
0
 
rrzCommented:
>in servlet chaining i have  5 servlets
Wnat are trying to accomplish in this chain ?  
0
 
rrzCommented:
What are trying to accomplish in this chain ?
0
 
rrzCommented:
The load-on-startup tag is just one way to initialize resources before requests are serviced. You could also use Filters or Listeners for this purpose.
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
>>The load-on-startup tag is just one way to initialize resources

could you give me one simple example
0
 
rrzCommented:
Put this into your web app's web.xml
    <servlet>    
                   <servlet-name>testExample</servlet-name>
                   <servlet-class>test.TestServlet</servlet-class>
                   <load-on-startup/>
    </servlet>  

and with code

package test;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet {
    public void init(ServletConfig config){
                   try{
                       super.init(config);
                       System.out.println("init of  TestServlet");
                       ServletContext context = config.getServletContext();
                       context.setAttribute("resourcePath","This could be a file path to a resource.");
                      }catch(ServletException se){System.out.println("error in TestServlet init");}
    }
}

you can now find the path to your resouce by using
getServletContext().getAttribute("resourcePath");     in your web app's servlets  and  by using  
application.getAttribute("resourcePath");      in your web app's JSPs

0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
<servlet>    
                   <servlet-name>testExample</servlet-name>
                   <servlet-class>test.TestServlet</servlet-class>
                   <load-on-startup/>
    </servlet>  


you couldnt specify any name in load onstartup tag

is it like that
0
 
rrzCommented:
You can write the tag either way. If you have more than one servlet to be initialized at startup, then you can use  
<web-app>
          <servlet>
                        <servlet-name>test</servlet-name>
                        <servlet-class>com.stardeveloper.servlets.TestServlet</servlet-class>
                       <load-on-startup>1</load-on-startup>
       </servlet>
      <servlet>    
                   <servlet-name>testExample</servlet-name>
                   <servlet-class>test.TestServlet</servlet-class>
                   <load-on-startup>2</load-on-startup/
>
      </servlet>  

</web-app>
and like muthu tigerfire posted above here the servlets will be initialized in the order you choose.
Here is simpler example of  servlet.

package test;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet2 extends HttpServlet {
    public void init(){
                       System.out.println("init of  TestServlet2");
                       ServletContext context = getServletContext();
                       context.setAttribute("resourcePath","This could be a file path to a resource.");
    }
}
0
 
muthu_tigerfireCommented:
Hi stmani2005,

I think u dont have to have load on startup tag at all for ur server chaining...

Cheers...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now