Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes init() not getting invoked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "init() not getting invoked" Watch "init() not getting invoked" New topic
Author

init() not getting invoked

Rohit Seth
Greenhorn

Joined: Sep 08, 2008
Posts: 4
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

Joined: Jan 28, 2008
Posts: 5575

your code looks good. what error you are getting?
Rohit Seth
Greenhorn

Joined: Sep 08, 2008
Posts: 4
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

Joined: Dec 16, 2007
Posts: 151
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?


Cheers!
RSR
Rohit Seth
Greenhorn

Joined: Sep 08, 2008
Posts: 4
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 06, 2001
Posts: 13459

"RSeth",
Please check your private messages.
-DOM
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init() not getting invoked