• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Displaying using constructor

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 24
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>");

}
}
 
Ranch Hand
Posts: 81
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was just trying what will be happen if i will do like this.

Thanks for your reply
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic