• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init() not getting invoked

 
Rohit Seth
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Servlet class that extends HttpServlet, My problem is while overriding init() method, i am not able to print anything. please see my code below
public class OneServlet extends HttpServlet{

public void init() throws ServletException{
System.out.println("Inside init");

}
---------
---------
}

Please suggest.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code looks good. what error you are getting?
 
Rohit Seth
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the complete class, not getting any error, just the statement is not printing in init method, so not sure if init written in class even gets called.

public class OneServlet extends HttpServlet{

public void init() throws ServletException{
System.out.println("Inside init");

}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{

System.out.println("Inside doGet() in OneServlet class");
response.setContentType("text/html");

PrintWriter out = response.getWriter();
out.println("<title>Example</title>" + "<body bgcolor=FFFFFF>");

out.println("<h2>Dummy Servlet</h2>");

out.close();

}

}
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please use code tags.
A servlet's init(ServletConfig) method is called by the server immediately after the server constructs the servlet's instance. Depending on the server and its configuration, this can be at any of these times:

1.When the server starts
2.When the servlet is first requested, just before the service() method is invoked
3.At the request of the server administrator

In any case, init() is guaranteed to be called before the servlet handles its first request.
Where are you trying to find this printed output?
 
Rohit Seth
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked but to my surprise, earlier I was deploying that in tomcat 5.15 and it was nor printing statement in init later when i deployed in weblogic 8.1, it did print that statement, dont know why this behavior?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use 'System.out' in webapplications. You should look at using correct logging instead. What you probably saw was that the output stream wasn't being sent where you thought it was. This led you to believing there was an error when there wasn't one.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"RSeth",
Please check your private messages.
-DOM
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic