Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

How to write user Msg in init in Servlets?

Thread view: 
Tomas - 15 Nov 2005 22:57 GMT
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
Rhino - 16 Nov 2005 05:23 GMT
> hi,
>
[quoted text clipped - 8 lines]
> 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
HalcyonWild - 16 Nov 2005 15:13 GMT
> hi,
>
[quoted text clipped - 10 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.