How to write user Msg in init in Servlets?

T

Tomas

hi,

I wonder how to write messages to users(i.e in explorer) the in init() method
in a Servlet:

public void init() throws ServletException{
//Her i want to do something like this:
response.setContentType("text/html");
PrintWriter out = res.getWriter();//res =HttpServletResponse
}
But you dont get a HttpServletResponse object in init() or can this be done
in some way? Or how do you write output messages to the user in init()?
Thanks in advance
/Tomas
 
R

Rhino

Tomas said:
hi,

I wonder how to write messages to users(i.e in explorer) the in init() method
in a Servlet:

public void init() throws ServletException{
//Her i want to do something like this:
response.setContentType("text/html");
PrintWriter out = res.getWriter();//res =HttpServletResponse
}
But you dont get a HttpServletResponse object in init() or can this be done
in some way? Or how do you write output messages to the user in init()?

You haven't described what kind of message you're trying to send and where
it should appear. I'm not sure if you want to give the user a friendly
message that the init() is proceeding okay or tell him/her about an error
that you've encountered. I'm also not sure if you want the message to appear
on the web page or if you could live with the message appearing elsewhere,
like in a log.

I'm pretty rusty with servlets but, if I remember correctly, there are (at
least) two ways to send a message from a servlet during an init():
1) Write the message to whatever log(s) you are using. This would be the
typical place to record an error message.
2) Write the message to the console.

In a production environment, the user wouldn't see either message but the
administrator could see them and respond to them. That might be a reasonable
way to communicate some things.

I *think* you can also force the servlet to display an error page if there
is an error within an init().

We could probably do a better job in answering your question if you were a
little more specific about what sort of message you wanted to send and where
it had to appear.

Rhino
 
H

HalcyonWild

Tomas said:
hi,

I wonder how to write messages to users(i.e in explorer) the in init() method
in a Servlet:

public void init() throws ServletException{
//Her i want to do something like this:
response.setContentType("text/html");
PrintWriter out = res.getWriter();//res =HttpServletResponse
}
But you dont get a HttpServletResponse object in init() or can this be done
in some way? Or how do you write output messages to the user in init()?
Thanks in advance
/Tomas


I havent seen anything like this being done. Did you try using the
init() that you have written above. I dont think so.

Also, remember that this will be called only once in the life of the
servlet. If you think, every time the user opens a new browser window,
and he is greeted by the output of init(), this wont work. Only when a
servlet instance is created in the container, init() will be called.
That instance can now serve many requests, from different clients,
without ever calling init().

BTW, did you try,

doGet/* (or doPost) */ (HttpServletRequest request, HttpServletResponse
response)
{
init();
//some other code
}

I am not sure what happens if you manually call init() of a servlet.
The behaviour may be unpredictable, or it may just work fine.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top