Constructors in servlets

how to use constructors in servlets.i am getting an error message like ""problem with instanciate the servlet........""can any one know we we are using init() method in place of constructor?i am new to java plz help me

No, you can't replace int() by constructor.
BECAUSE
In order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init() method and pass along an object that implements the ServletConfig interface. Also, Java doesn't allow interfaces to declare constructors. This means that the javax.servlet.Servlet interface cannot declare a constructor that accepts a ServletConfig parameter. It has to declare another method, like init(). It's still possible, of course, for you to define constructors for your servlets, but in the constructor you don't have access to the ServletConfig object or the ability to throw a ServletException.This ServletConfig object supplies a servlet with information about its initialization (init) parameters. These parameters are given to the servlet itself and are not associated with any single request. They can specify initial values, such as where a counter should begin counting, or default values, perhaps a template to use when not specified by the request. Hope this explaination solves your doubt.
Cheers,
Dinesh

Similar Messages

  • Parameterized constructor in servlets

    why can't we use parameterized constructor in servlets?
    please describe it briefly?

    Why should you use parametrized constructor with servlet? If you want to pass any parameter then use <init-param> in web.xml. Even if you make parameterized constructor, how it would be invoked? Servlet container creates instance of servlet by invoking common constructor.
    So do you intent to rewrote container to invoke parameterized constructor?
    Please refer servlet documentation for more information on servlets and it would help you.

  • Constructor in servlet

    Can we use the constructor, instead of init(), to initialize servlet?

    But remember to due a supper().The compiler will implicitly add a call to super() for you. Personally, I think it's clutter to add one when it's not necessary.
    Or use the init method. Actually it�s like a constructor. Not really. At least, no more so than any other method. It simply gets called by the servlet container as part of the servlet lifecycle, by specification.
    ~

  • Programming JSP using JDeveloper 10.1.3 and JSTL

    Hello,
    I new in JDeveloper. I am creating a web proyect and I am programming a JSP. This JSP includes JSTL 1.1 Core, exactly the next line:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    When I compile my proyect, obtain the next error:
    Error: java.lang.NoSuchMethodError: javax.servlet.jsp.tagext.TagAttributeInfo.<init>(Ljava/lang/String;ZLjava/lang/String;ZZ)V
    I ensured that this class (javax.servlet.jsp.tagext.TagAttributeInfo) has no that constructor (String, String, boolean) but when I consult the Sun documentation for that class, it is said that the class contain the constructor.
    My proyect include the next libraries defined in JDeveloper:
    · J2EE
    · JSTL 1.1
    · Strut Runtime
    · JSTL 1.1 tags
    Please, what should I do to do my proyect work?

    Constructor javax.servlet.jsp.tagext.TagAttributeInfo (String, String, boolean)
    is not defined. The JSP 2.0 constructor is
    TagAttributeInfo(java.lang.String name,
    boolean required,
    java.lang.String type,
    boolean reqTime,
    boolean fragment)
    Another constructor is
    TagAttributeInfo(java.lang.String name,
    boolean required,
    java.lang.String type,
    boolean reqTime)

  • URGENT: Jsp:Useben Not working with HashMap : Please HELP!

    Hi,
    I have a bean called DocBean (name, id - properties);
    another bean called DocListBean (in that add() will put the DocBean to a hashtable- hashtable is created in its constructor.
    the servlet creates the DocList & Doc Beans and by using add method, put all the doc in the doclist bean.
    then, i put that in the request
    req.setAttribute("doclistbean", doclistbean);
    I use System.out.println for counting the no.of docbeans in the doclist. in java file it displays
    System.out.println(docList.count() );
    (displays .. : 5)
    I have a jsp file, which has
    <jsp:useBean id="docList" class="docListBean" scope="request"/>
    then i display,
    System.out.println(docList.count() );
    this displays - 0
    I don't understand why the bean has no(size=0) docBeans(hashtable) in JSP file ; but the servlet file displays all the docBeans(hashtable) with size 5.
    Please help me..

    Probably because your servlet put it into the session with the name of "doclistbean", but your JSP tries to use it with the name of "docList".

  • Why do we use init() method instead of constructor to initialize a servlet?

    Why do we use init() method instead of constructor to initialize a servlet?

    I suspect the reasons are partly historical. A servlet is loaded dynamically, by class name and, once you've loaded the Class, it's easier to to a newInstance, using the default construtor than it is to search for a particular matching constructor and invoke it.

  • Constructor in my servlet class

    Can i write a constructor in my servlet class inherited from HttpServlet.
    eg:
    class myServlet extends HttpServlet
    myServlet()
    doGet(......)
    }

    And who will call the derived class no args constructor?
    Remember Servlet classes are managed by the container which is responsible for initiating the servlets and calling appropriate life cycle methods. The container initiates the Servlet using the no args constructor. For all practical purposes you would choose the init() method rather than the constructor for initialization logic.
    ram.

  • "constructor not found" error when building servlet

    Greetings,
    We are encountering the following error when trying to make or run a project that displays a simple crosstab as a servlet:
    Error(45,9): constructor ServletRenderingContext(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.ServletConfig) not found in class oracle.cabo.ui.ServletRenderingContext
    Clicking on this error brings you to the following lines of source in ApplicationRenderingContext.java:
    super (request, response, servlet.getServletConfig() );
    Can anyone assist?
    Regards,
    Steve Locke

    This happens in JDeveloper; can't deploy due to this error.
    Here's my version info:
    BI Beans Diagnostics(v1.0.0.0) 4/15/04
    ===============================================================================
    JDEV_ORACLE_HOME ............................. = c:\oracle\ora92
    JAVA_HOME .................................... = c:\oracle\ora92\jdk
    JDeveloper version ........................... = 9.0.3.3.1205
    BI Beans version ............................. = 9.0.3.6.0A
    BI Beans internal version .................... = 2.7.0.13.1
    Connect to database .......................... = Successful
    JDBC driver version .......................... = 9.2.0.3.0
    JDBC JAR file location ....................... = c:\oracle\ora92\jdbc\lib
    Database version ............................. = 9.2.0.4.0
    OLAP Catalog version ......................... = 9.2.0.4.1
    OLAP AW Engine version ....................... = 9.2.0.4.1
    OLAP API Server version ...................... = 9.2.0.4.1

  • Reg Servlet's inti method and Constructor method

    Hi,
    My friend asks me 'Is init method of servlet is necessary? Can't we do whatever we want to do in Init method, in the constructor of the servlet? Why is init method is necessary?'
    Thanks in advance,
    Guhan

    you can get the config object from the init method which u cant get from constructors. The environment details of the servlet can be had from it...

  • Why Servlets having Init() even though its having constructors

    Hi friends , i ve one doubt,
    in servlets all initilization part can be handled by Iinit() method,
    my qn is each servlet is singleton oriented i.e, each has only one instance, it means only one time constructor will called, and init() also called only once, so instead of init() we can use constructor, in this case why sun people provide us init() method,
    please give me soln.

    -nevermind-
    I've found something interesting in Google:
    http://www.unix.org.ua/orelly/java-ent/servlet/ch03_03.htm
    The init() method is typically used to perform servlet initialization--creating or loading objects that are used by the servlet in the handling of its requests. Why not use a constructor instead? Well, in JDK 1.0 (for which servlets were originally written), constructors for dynamically loaded Java classes (such as servlets) couldn't accept arguments. So, in order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init() method and pass along an object that implements the ServletConfig interface. Also, Java doesn't allow interfaces to declare constructors. This means that the javax.servlet.Servlet interface cannot declare a constructor that accepts a ServletConfig parameter. It has to declare another method, like init(). It's still possible, of course, for you to define constructors for your servlets, but in the constructor you don't have access to the ServletConfig object or the ability to throw a ServletException.

  • How Servlet container check which constructor has been implemented ?

    How Servlet container checks which constructor has been implemented ?

    http://forum.java.sun.com/thread.jspa?threadID=554796&tstart=0

  • Why servlet has init() method insteadof constructor

    Why servlet has init() method insteadof constructor

    Depends completely on the servlet. If the servlet implements SingleThreadedModel, then multiple instances of the servlet will be created.
    Reason: Most servlets work quite well with multiple threads, the bulk of the variables are declared locally within the service method. Sometimes, however, the servlet needs to be able to handle multiple threads even though it is not thread safe. In such as case the SingleThreadedModel is implemented to tell the container to use a new (or one from a pool) servlet. In this case the init method will be called each time.
    [As an aside: there is of course one (not obscure, but not normal) scenario where you would call the constructor of a servlet directly - this would be where you are writing a container.  'Ah!' you say, 'I will leave that kind of madness to the boys and girls at Jakarta' - however, you may wish to write a container as a test frame for you servlet, which you should be doing.]

  • How to load a client certificate into a servlet to access a Web Service

    Hi,
    I am having the following problem:
    I am trying to use a Web Service client (Axis) within a servlet running under
    WebLogic 8.1.
    I would like to have mutual SSL-based authentication between the client and the
    server hosting the Web Service. Thus, my client has to send a certificate to the
    server.
    My problem is: how to get the certificate into the request? I know that, for example,
    the HttpsURLConnection class of WebLogic has a loadIdentity method. But I can't
    use this class.
    Is there any other method to make sure that SSL requests use my client certificates?
    By the way, I am receiving the following error message from the server:
    <Apr 13, 2004 5:35:10 PM EEST> <Debug> <TLS> <000000> <Required peer certificate
    s not supplied by peer>
    <Apr 13, 2004 5:35:10 PM EEST> <Warning> <Security> <BEA-090508> <Certificate
    ch
    ain received from 127.0.0.1 - 127.0.0.1 was incomplete.>
    Anyone has an idea?
    Thanks for any hints,
    Zoltan Schreter
    Nokia

    Hi all,
    I have solved this problem basically by using weblogic's SSLSocketFactory instead
    of the default one used by Axis. I created a custom HttpSender (MyHttpSender)
    which uses this Factory. I then created a custom Config class which I pass to
    the constructor of Service. The Config class looks like this:
    public class MyConfig extends SimpleProvider {
    * Constructor - deploy client-side basic transports.
    public MyConfig() {
    deployTransport("java", new SimpleTargetedChain(new JavaSender()));
    deployTransport("local", new SimpleTargetedChain(new LocalSender()));
    deployTransport("http", new SimpleTargetedChain(new MyHttpSender()));
    The relevant code within MyHttpSender looks something like this:
    SSLClientInfo sslinfo = new SSLClientInfo();
    File ClientKeyFile = new File("C:/certificates/testkey.pem");
    File ClientCertsFile = new File("C:/certificates/testcert.pem");
    InputStream[] ins = new InputStream[2];
    ins[0] = new FileInputStream(ClientCertsFile);
    ins[1] = new FileInputStream(ClientKeyFile);
    String pwd = "mykeypass";
    sslinfo.loadLocalIdentity(ins[0], ins[1], pwd.toCharArray());
    javax.net.SocketFactory sockf = weblogic.security.SSL.SSLSocketFactory.getJSSE(sslinfo);
    sock = sockf.createSocket(host, port) ;
    By the way, this change also solved the other problem I posted about (not being
    able to tunnel through the https proxy).
    Cheeers,
    Zoltan Schreter
    Nokia
    "Tony" <TonyV> wrote:
    Which API's are you currently using for the SSL communication in the
    client
    side?
    Tony
    "Zoltan Schreter" <[email protected]> wrote in message
    news:[email protected]...
    Hi,
    I am having the following problem:
    I am trying to use a Web Service client (Axis) within a servlet runningunder
    WebLogic 8.1.
    I would like to have mutual SSL-based authentication between the clientand the
    server hosting the Web Service. Thus, my client has to send a certificateto the
    server.
    My problem is: how to get the certificate into the request? I knowthat,
    for example,
    the HttpsURLConnection class of WebLogic has a loadIdentity method.But I
    can't
    use this class.
    Is there any other method to make sure that SSL requests use my clientcertificates?
    By the way, I am receiving the following error message from the server:
    <Apr 13, 2004 5:35:10 PM EEST> <Debug> <TLS> <000000> <Required peercertificate
    s not supplied by peer>
    <Apr 13, 2004 5:35:10 PM EEST> <Warning> <Security> <BEA-090508><Certificate
    ch
    ain received from 127.0.0.1 - 127.0.0.1 was incomplete.>
    Anyone has an idea?
    Thanks for any hints,
    Zoltan Schreter
    Nokia

  • Javax.servlet.ServletException: The name "" is not legal for JDOM/XML namespaces

    Dear all,
    First of all sorry, if this is not the right place for my question.
    I am facing some problem with the XFire Webservices. When i am trying to access the WSDL through the url. server is throwing the following exception :
    javax.servlet.ServletException: The name "" is not legal for JDOM/XML namespaces: Namespace URIs must be non-null and non-empty Strings.
         org.codehaus.xfire.transport.http.XFireServletController.doService(XFireServletController.java:143)
         org.codehaus.xfire.transport.http.XFireServlet.doGet(XFireServlet.java:107)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
         org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    root cause
    org.jdom.IllegalNameException: The name "" is not legal for JDOM/XML namespaces: Namespace URIs must be non-null and non-empty Strings.
         org.jdom.Namespace.getNamespace(Namespace.java:164)
         org.codehaus.xfire.util.NamespaceHelper.getUniquePrefix(NamespaceHelper.java:58)
         org.codehaus.xfire.aegis.type.basic.BeanType.writeSchema(BeanType.java:560)
         org.codehaus.xfire.wsdl.AbstractWSDL.addDependency(AbstractWSDL.java:224)
         org.codehaus.xfire.wsdl.AbstractWSDL.addDependency(AbstractWSDL.java:233)
         org.codehaus.xfire.wsdl11.builder.WSDLBuilder.createDocLitPart(WSDLBuilder.java:403)
         org.codehaus.xfire.wsdl11.builder.WSDLBuilder.createPart(WSDLBuilder.java:355)
         org.codehaus.xfire.wsdl11.builder.WSDLBuilder.writeParameters(WSDLBuilder.java:509)
    cont.......
    I am not able to figure out the root cause for this. The service.xml file looks like below:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- START Service.xml -->
    <beans xmlns="http://xfire.codehaus.org/config/1.0">
    <!-- Construct the castor service factory by Spring -->
    <bean id="castorTypeRegistry" class="org.codehaus.xfire.castor.CastorTypeMappingRegistry"/>
    <bean id="bindingProvider" class="org.codehaus.xfire.aegis.AegisBindingProvider">
    <constructor-arg ref="castorTypeRegistry"/>
    </bean>
    <bean id="castorServiceFactory" class="org.codehaus.xfire.service.binding.ObjectServiceFactory">
    <constructor-arg index="0" ref="xfire.transportManager"/>
    <constructor-arg index="1" ref="bindingProvider"/>
    </bean>
    <service>
    <name>ReleaseManager</name>
    <namespace>ReleaseManager</namespace>
    <serviceClass>com.pinkroccade.jfoundation.calculation.releases.ReleaseManager</serviceClass>
    <implementationClass>com.pinkroccade.jfoundation.calculation.releases.ReleaseManagerImpl</implementationClass>
    <schemas>
    <schema>META-INF/schema/release-impact-worksheet-3.2.0.xsd</schema>
    </schemas>
    <style>document</style>
    <serviceFactory>#castorServiceFactory</serviceFactory>
    </service>
    </beans>
    <!-- END Service.xml-->
    The issue which i am facing is it due to the problem with the service.xml (Which i dont think so..), Or is it any thing to do with the XSD file which i am using.
    Can any body give me some guide lines for this ????
    Thanks in advance.
    Thanks and Regards,
    Manjunath.

    Any one any thoughts..
    I need to find out the solution for this as soon as possible; Not able to proceed further...
    Thanks and Regards,
    Manjunath.

  • MVC Design Help, Single Servlet, How do I access the Model and DB

    Hi all. New here and looking for some help.
    I am currently writing a website that allows the creation of users, that may upload articles and post comments on articles. Im trying to develop using MVC. I have a single controller servlet that processes POST actions throughout the site (eg. InsertArticle, DeleteArticle, ModifyArticle, etc..)
    Now my problem is, just how do I retrieve the articles from the database? .. for example, if I load up a page http://localhost/articles.jsp which should display all the articles currently in the site.. I would have a function say getAllArticles() which should return a collection of all the articles. I can then iterate through the articles in the jsp page.
    I am trying to use <jsp:useBean...etc.> BUT.. my articles bean constructor takes a databaseconnection object as a parameter. If I use useBean I cant pass the databaseconnection object to the bean and I get an error because it cannot create the bean.
    Any help on this would be appreciated as well as any tutorial links. I also looked at the petstore blueprint program on the sun website, but that program has me completely lost with all the xml. I would prefer not to use custom tags or struts for now. I would like to keep it simple with snippets of java code in the jsp pages for data retrieval and display while keeping all the business logic in the beans of the model. I would also like to keep this relatively secure. I am developing using Oracle Jdeveloper 10g.
    Thanks
    Jazz

    Hey steve, thanks for that.. but thats exactly what i
    dont want to do. I also realize that ive been writing
    the ejb's incorrectly to begin with and have begun to
    rewrite them. However maybe I can make it easier..
    what im trying to do is precisely what is in this
    diagram..
    http://gsraj.tripod.com/jsp/jsp.html
    the problem im having is where it passes data back to
    the jsp page.. entity beans cant (or shouldnt?) be
    accessed directly from jsp pages.. which means i
    create a session bean which interacts with the entity
    bean and can return information to the jsp page.. i
    understand what i have to do.. i just dont know how to
    do it.. been searching for google for tutorials and
    etc.. this site is the closest ive come but .. as you
    can see it says "more to come" ..
    thanks again guys really appreciate itAhhh. EJBs. Enterprise Java Beans and JavaBeans are two completely different beasts. I know nothing about EJBs, except:
    1) They are much harder to use (and serve a different purpose I assume) then JavaBeans
    2) Tomcat (the server I use) doesn't support them.
    Sorry I can't be of more help.

Maybe you are looking for

  • HT1848 can i transfer gift card funds from one itunes account to another

    We just got my son an iPhone and have it under the same iTunes account as his iPod.  Last night he was trying to download a new ringtone but kept getting asked answers to security questions.  We thought we knew the answers, but they wouldn't work.  A

  • Ipod touch camera does not open

    have ipod touch, model PC547ZP version 5 upgraded to OS 5 had all worked till other day now the camera will not open. tried powering on and off with no change know can restore but conserned might lose data etc anyone know of anything I can do to reso

  • How to list all files in my system

    Hi there, I want to list all files in my system. My system is Windows XP, can anybody help me please? Any can you recommend me if java is suit for such processing? Thx in advance.

  • Dump occured in standard program SAPMV45A (order creation)

    hi, In order creation, a dump is occured in standard program SAPMV45A. Because of in FM "Pricing_Dialog_Tabstrips" is incorrect and TAXI_TABSTRIP_C and TAXI_TABSTRIP_ITEM" specified here is a different field type.This is standard program we can not c

  • Illustrator CS5 crashes on quit in Mac

    I saw the other thread (the one marked answered) - but fixing the permissions as it suggested did not fix the problem for me.  I am running Adobe Illustrator CS5 on a MacBook Pro and everything is up to date as far as I know.  Upon quitting, Illustra