This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying using constructor

 
Abhay Choubey
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am getting it only once. please put the code so that it can be easy for identifying the problem !!!
 
Abhay Choubey
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just trying what will be happen if i will do like this.

Thanks for your reply
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic