wood burning stoves 2.0*
The moose likes Servlets and the fly likes Displaying using constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Displaying using constructor" Watch "Displaying using constructor" New topic
Author

Displaying using constructor

Abhay Choubey
Ranch Hand

Joined: Jun 02, 2011
Posts: 34
hi all,

I have created one constructor in servlet class and displaying hello world. In doGet() method, i am creating instance of this servlet. when I am executing this servlet class it is displaying two time "hello world" in server side. i did not understand why it is happening.
can any one help me please

Thanks in advance
Abhay
Vigneswaran Marimuthu
Greenhorn

Joined: Aug 30, 2011
Posts: 24

i am getting it only once. please put the code so that it can be easy for identifying the problem !!!


Regards,

Vigneswaran.M
Abhay Choubey
Ranch Hand

Joined: Jun 02, 2011
Posts: 34
This is my code -

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class textbox1 extends HttpServlet{


public textbox1(){

System.out.println("Hello world");
}

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

PrintWriter pw = response.getWriter();

textbox1 box=new textbox1();

pw.println("box object is " + box);

response.setContentType("text/html");

pw.println("<html>");
pw.println("<head><title>Hello</title></title>");
pw.println("<body>");
pw.println("<h2 align=center>Welcome To Tecnotree</h2>");
pw.println("</body></html>");

}
}
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

a servlet is meant to be a controller...
From the code you posted, i understand that you want to make a textbox from a servlet or something??

why do you use

public textbox1(){

System.out.println("Hello world");
}

and

textbox1 box=new textbox1();

and

pw.println("box object is " + box);

??


again, a servlet is a kind of controller, not a textbox-object or something...

what are you actually trying to do?

delete the lines of code that i mentioned, and your servlet will be ok...

and for your topic...try using a servlet's init() method
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Abhay Choubey wrote:hi all,

I have created one constructor in servlet class and displaying hello world. In doGet() method, i am creating instance of this servlet. when I am executing this servlet class it is displaying two time "hello world" in server side. i did not understand why it is happening.
can any one help me please
Thanks in advance
Abhay


You should notinstantiate a servlet yourself, it is the containers job. Recommended Reading
Abhay Choubey
Ranch Hand

Joined: Jun 02, 2011
Posts: 34
I was just trying what will be happen if i will do like this.

Thanks for your reply
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Abhay Choubey wrote:I was just trying what will be happen if i will do like this.

As usual, when you do something nonsensical, it won't work.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
jQuery in Action, 2nd edition
 
subject: Displaying using constructor