my dog learned polymorphism
The moose likes Servlets and the fly likes Which one gets first executed ????????? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Which one gets first executed ?????????" Watch "Which one gets first executed ?????????" New topic
Author

Which one gets first executed ?????????

rajesh katti
Greenhorn

Joined: May 24, 2001
Posts: 10
Hi,
I have written a simple servlet file, in which I have overriden all methods like doGet, service, init.
According to servlet documentation, init() is the method which suppose to be get exceuted. But in my case..it didn't. Instead, everytime, service() method gets exceuted.
Could anyone please let me know why such thing is happening?
Actually I have to write connection method in init() method..but bcos of this problem..I am not able to do that...
Please advice...
Thanks
Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
The init() method, should and does, get executed the first time your servlet is created. Not everytime it is accessed.
Shutdown and restart your server.. Put some out.write in the init() and you will see that happening the <b>first</b> time you access the servlet.
HTH
rajesh katti
Greenhorn

Joined: May 24, 2001
Posts: 10
Thanks Mak....But I have tried that. Could u please check my code and see anything wrong in this...
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("<br>In init");
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("<br>In get");
}
public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("<br>In Service");
}
}
In this case...ONLY service gets executed and not anything else...Please check it at ur end.
Thanks
Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
try this..
rajesh katti
Greenhorn

Joined: May 24, 2001
Posts: 10

That code didn't work...
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

What are you using as your servlet runner?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hmm.... I should mention I've duplicated this result using Resin2.0.0

I'm not sure where to put "plain old servlets" in Tomcat, like they are easy to do in Resin, so I'll check Tomcat next.
You are right... your code only produces an 'in service'
I even tried System.out.println's and flushing the out. Nothing would make anything output for the init method.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

As was stated previuorly, the init method gets exceuted the first time the servlet is accessed, ie when the Server loads the servlet.
It has nothing to do with a request or response, so it can't return anything to the client, it's purpose is to set up data specific to this servlet.
The original code:

overloaded the init method but since it doesn't match the method that gets called it did nothing.
The version that Mak suggested:

was the version that would be called, except that the variable 'out' was not defined and so this version wouldn't compile.
What you need to try is something like this:

Normally I wouldn't recommend using System.out in a servlet but this is fine for testing...
Does this help?
Dave.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which one gets first executed ?????????
 
Similar Threads
Doubt in Servlets
Init method getting called multiple times
Final value in a Servlet
Child Frame's compoenent (JFrame) is not displayed till parent frame exceution
Doubt in request Vs response