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

C

carlos80

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
 
T

Tony Morris

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.
 
N

Nigel Wade

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?
 
J

John C. Bollinger

Karel said:
(e-mail address removed) schreef:


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.
 
J

John C. Bollinger

Nigel said:
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.
 
L

los

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
 
L

los

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
 
J

John C. Bollinger

los said:
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">
....
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top