Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 2nd, 2003, 10:52 AM
Registered User
 
Join Date: Dec 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default constructor in servlets

what is the use of using constructors in servlets as we have init method for initialization purpose?

 
Old December 2nd, 2003, 12:25 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

All servlets have a default no-args constructor, which gets called before the init(...) method. The Servlet container can't know what sort of parameters you're expecting as part of the servlet-instance construction step, so it has to assume you have a no-arg constructor that's available to create a "default" instance of the servlet.

Cheers

Martyn
 
Old October 19th, 2005, 02:24 AM
Registered User
 
Join Date: Oct 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sunilphanimanne
Default

If we write only a constructor with arguments in our servlet, the servlet container will be unable to instantiate our servlet. Is there any way to enforce the user not to write an argumented constructor??

Regards,
Sunil Phani Manne

Many of life's failures are of people who did not realize how close
they were to success when they gave up.       &nbsp ;        & nbsp;       
        &n bsp;       &nbs p;       - Thomas Edison
 
Old November 3rd, 2005, 07:55 AM
abc abc is offline
Registered User
 
Join Date: Mar 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by sunilphanimanne
 If we write only a constructor with arguments in our servlet, the servlet container will be unable to instantiate our servlet. Is there any way to enforce the user not to write an argumented constructor??

Regards,
Sunil Phani Manne

Many of life's failures are of people who did not realize how close
they were to success when they gave up.       &nbsp ;        & nbsp;       
        &n bsp;       &nbs p;       - Thomas Edison

:D:D

 
Old November 3rd, 2005, 08:01 AM
abc abc is offline
Registered User
 
Join Date: Mar 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As u know by defaultjava gives a no argument constructor for every class. but if u provide a constuctor java will not give u the default constructor. In such a case u should provide one. Here when a servlet is instantiated the servlet container will call its no-arg constructor which is not present coz u have given an argument constructor. so the only option is u have to givea no arg constructor explicitly if u have given any other constructor for the servlet. Hope u have got my point. Thank You.
rgds Jiji

 
Old January 16th, 2006, 12:27 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by sunilphanimanne
 If we write only a constructor with arguments in our servlet, the servlet container will be unable to instantiate our servlet. Is there any way to enforce the user not to write an argumented constructor??
You could forbid users from extending javax.servlet.http.HttpServlet, and instead require them to extend an HttpServlet instance you have designed. Design your instance to have a public no-arg constructor -- this way there's always a no-arg constructor.

Jon Emerson
http://images.burningman.com/index.cgi?q_photog=emerson





Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning Nothing from a constructor Imar Pro VB.NET 2002/2003 8 June 29th, 2012 02:00 PM
Constructor nayeem69 .NET Framework 2.0 1 July 17th, 2007 12:33 AM
private constructor in C# n.nsivakumar C# 1 August 16th, 2006 07:08 AM
static constructor sreenu.pocha C# 1 July 26th, 2006 07:21 AM
Constructor ramess C++ Programming 1 March 18th, 2006 07:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.