Seems like my servlet isn't overloading the init() method..can someone plz help?

Discussion in 'Java' started by carlos80@gmail.com, Mar 3, 2005.

  1. Guest

    Hi,

    I've been struggling with this problem all night, and I feel like I'm
    just missing something.

    I'm basically trying to run some code in the init() method in my
    servlet. However the method doesn't seem to be getting called. I know
    that the init() method is only called once the first time the servlet
    gets called, and that is exactly why I'm stuck with this problem.

    Here is a sample of my code

    public class MyServletA extends HttpServlet {
    // Page names
    private static int a;

    public void init() throws ServletException {
    super.init();
    a = 4;
    System.out.println("init");
    }

    public void doPost(...) {
    System.out.println(a);
    }
    }

    When I start the server and load the jsp that calls MyServletA I never
    get the "init" to print out, and when doPost gets called a = 0 and not
    4.

    Can someone please shed some light in this? Thanks
    , Mar 3, 2005
    #1
    1. Advertising

  2. Tony Morris Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > I've been struggling with this problem all night, and I feel like I'm
    > just missing something.
    >
    > I'm basically trying to run some code in the init() method in my
    > servlet. However the method doesn't seem to be getting called. I know
    > that the init() method is only called once the first time the servlet
    > gets called, and that is exactly why I'm stuck with this problem.
    >
    > Here is a sample of my code
    >
    > public class MyServletA extends HttpServlet {
    > // Page names
    > private static int a;
    >
    > public void init() throws ServletException {
    > super.init();
    > a = 4;
    > System.out.println("init");
    > }
    >
    > public void doPost(...) {
    > System.out.println(a);
    > }
    > }
    >
    > When I start the server and load the jsp that calls MyServletA I never
    > get the "init" to print out, and when doPost gets called a = 0 and not
    > 4.
    >
    > Can someone please shed some light in this? Thanks
    >


    The application server has almost certainly redirected the standard output
    stream.
    Also, you haven't overloaded the init method, you have overridden it - an
    important distinction.
    Search the application server documentation for where it redirects the
    standard output stream - typically, some file in the logs.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
    Tony Morris, Mar 3, 2005
    #2
    1. Advertising

  3. Karel Suikers, Mar 3, 2005
    #3
  4. Nigel Wade Guest

    wrote:

    > Hi,
    >
    > I've been struggling with this problem all night, and I feel like I'm
    > just missing something.
    >
    > I'm basically trying to run some code in the init() method in my
    > servlet. However the method doesn't seem to be getting called. I know
    > that the init() method is only called once the first time the servlet
    > gets called, and that is exactly why I'm stuck with this problem.
    >
    > Here is a sample of my code
    >
    > public class MyServletA extends HttpServlet {
    > // Page names
    > private static int a;
    >
    > public void init() throws ServletException {
    > super.init();
    > a = 4;
    > System.out.println("init");
    > }
    >
    > public void doPost(...) {
    > System.out.println(a);
    > }
    > }
    >
    > When I start the server and load the jsp that calls MyServletA I never
    > get the "init" to print out, and when doPost gets called a = 0 and not
    > 4.
    >
    > Can someone please shed some light in this? Thanks


    To get init() to be invoked you either need to set <load-on-startup> to 1 in
    web.xml, or have a client access the servlet. Has this been done? Where
    are you looking for the output?


    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, Mar 3, 2005
    #4
  5. Re: Seems like my servlet isn't overloading the init() method..cansomeone plz help?

    Karel Suikers wrote:
    > schreef:
    >
    >> When I start the server and load the jsp that calls MyServletA ...

    >
    > that's the problem, your browser should call the servlet:
    > http://carlos.gmail.com:8080/mywebapp/servlet/MyServletA


    It depends on how the JSP "calls" the servlet. If it uses one of the
    allowed techniques, then the servlet container is obliged to have or put
    a servlet instance into service, and this involves invoking it's init()
    method. The allowed techniques are <jsp:include />, <jsp:forward /> (or
    RequestDispatcher-based equivalents in scriptlet code), or placing the
    servlet's URL into the HTML output in an appropriate place. The JSP
    should never attempt to access a servlet class directly.

    --
    John Bollinger
    John C. Bollinger, Mar 3, 2005
    #5
  6. Re: Seems like my servlet isn't overloading the init() method..cansomeone plz help?

    Nigel Wade wrote:

    > To get init() to be invoked you either need to set <load-on-startup> to 1 in
    > web.xml, or have a client access the servlet.


    Not quite so: the init() method will be invoked in the process of
    putting a servlet instance into service, no matter the reason this is
    done. Both of the scenarios you describe will cause the servlet
    container to put a servlet instance into service, but so should a
    <jsp:include />, <jsp:forward />, or the RequestDispatcher-based
    equivalents.

    --
    John Bollinger
    John C. Bollinger, Mar 3, 2005
    #6
  7. los Guest

    Thanks for all the replies!

    Like I said I'm calling the servlet from my jsp. Which basically has a
    call to the doPost() method as an action in a form. When the form gets
    submitted the instance of the servlet gets called...something like this

    <%@ page language="java" import="MyPackage.MyServletA"%>
    ....
    <% MyServletA a = new MyServletA(); %>
    ....
    <FORM name="create" action="<% a.doPost(request, response); %>"
    method=post>
    ....
    </form>

    In my web.xml file I have
    <servlet>
    <servlet-name>MyServletA</servlet-name>
    <servlet-class>MyPackage.MyServletA</servlet-class>
    </servlet>

    is one of these lines necessary for all the servlets I want to be able
    to overload?

    In response to the redirect of the output stream, do you mean that the
    output stream in the init() is different then in the doPost()? Because
    I am able to see the output for the doPost() when it happens.

    Thanks
    los, Mar 3, 2005
    #7
  8. los Guest

    Let me explain what I'm trying to do, perhaps there is a better way
    someone knows how to do.

    In this application I'm working on sometimes I want to send out some
    email reminders. It could be the case that there are several emails
    that need to be sent. Instead of having the doPost "hang" while the
    server is sending out all of these emails, I wanted to have the server
    return and forward the user to the next page and put all of the emails
    to be sent out in a queue. So essentially I was thinking of kicking
    off a Timer object in the init() method that would go through the queue
    and send out emails in the background in a batch mode every x seconds.


    Does this make sense? Does it sound like a good idea? And am I on the
    right track?

    Any feedback would be greatly appreciated! thanks again
    los, Mar 3, 2005
    #8
  9. Re: Seems like my servlet isn't overloading the init() method..cansomeone plz help?

    los wrote:

    > Thanks for all the replies!
    >
    > Like I said I'm calling the servlet from my jsp. Which basically has a
    > call to the doPost() method as an action in a form.


    You must not do this.

    > When the form gets
    > submitted the instance of the servlet gets called...something like this
    >
    > <%@ page language="java" import="MyPackage.MyServletA"%>
    > ...
    > <% MyServletA a = new MyServletA(); %>
    > ...
    > <FORM name="create" action="<% a.doPost(request, response); %>"
    > method=post>
    > ...
    > </form>


    That is completely bogus. In the first place, it produces a form with
    an empty action attribute, which is not HTML-compliant. More
    importantly, however, the servlet is "called" when the page containing
    the form is created, not when the form is submitted. You should never
    instantiate a servlet class -- only the servlet container ever should
    do. You should also never invoke methods directly on a servlet class
    from outside that class.

    >
    > In my web.xml file I have
    > <servlet>
    > <servlet-name>MyServletA</servlet-name>
    > <servlet-class>MyPackage.MyServletA</servlet-class>
    > </servlet>


    Then you want your form element to look something like this:

    <form name="create" action="<%=request.getContextPath()%>/MyServletA"
    method="POST">
    ....
    </form>

    > is one of these lines necessary for all the servlets I want to be able
    > to overload?


    I don't see where overloading comes into it. Yes, you need a servlet
    declaration in web.xml for every servlet that is part of your web
    application. You also need a servlet-mapping for each one -- the JSP
    fragment I offered above assumes that you will map the servlet to the
    name you declared for it (which is by no means necessary).

    > In response to the redirect of the output stream, do you mean that the
    > output stream in the init() is different then in the doPost()? Because
    > I am able to see the output for the doPost() when it happens.


    Many servlet containers will set System.out to point at a stream
    connected to a log file. If you are seeing output on the console when
    you test the abomination you have put together then it may be a sign
    that it is more broken than I thought.

    --
    John Bollinger
    John C. Bollinger, Mar 4, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mickey Segal
    Replies:
    0
    Views:
    369
    Mickey Segal
    Aug 21, 2004
  2. Mitch
    Replies:
    2
    Views:
    542
    Mitch
    Mar 13, 2006
  3. Replies:
    4
    Views:
    340
    Martin Ambuhl
    Oct 30, 2005
  4. Replies:
    1
    Views:
    365
  5. Replies:
    2
    Views:
    323
    Nick Keighley
    Nov 24, 2006
Loading...

Share This Page