Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Which one gets first executed ?????????  RSS feed

 
rajesh katti
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this..
 
rajesh katti
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That code didn't work...
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you using as your servlet runner?
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!