*
The moose likes Servlets and the fly likes Notify JSP that ServletContext been changed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Notify JSP that ServletContext been changed? " Watch "Notify JSP that ServletContext been changed? " New topic
Author

Notify JSP that ServletContext been changed?

Annina WG
Greenhorn

Joined: Dec 12, 2002
Posts: 19
I posted this in JSP forum, but I wonder if it has more concern with Servlet. If forum leader find it is redundante then just delet it.

I mean,I pass an object from a Servlet to a JSP by using ServletContext. Is there method to notify JSP when this object has been changed in side of Servlet that cause JSP to read the object again? Thanks for your advice!
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi Annina, welcome to the ranch!
"I posted this in JSP forum, but I wonder if it has more concern with Servlet. If forum leader find it is redundante then just delet it."
You're right, redundant posts are not all too welcome around here.
You might not realize it yet, but you have the power to delete your own posts. If you think your question would be better placed in the Servlets forum, you could delete the duplicate in JSP (and win the hearts of the busy volunteers who moderate JavaRanch ).
Just click on the little icon that looks like a notepad and pencil, just above the post, and you can make your own post disappear.
Any minute now you'll be hearing from a friendly rancher who can help you with your real concern...

Pauline
Annina WG
Greenhorn

Joined: Dec 12, 2002
Posts: 19
yes, you are right. I have deleted my post there.
Now I really appreciate advices to my question.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
There is no need for any special notification, your JSP automatically gets a variable named "application" that is the ServletContext shared by servlets and JSP in the "web application". Retrieving objects from application for every execution of the JSP is the preferred approach, you don't save anything by having the JSP hold on to a reference.
Bill
Annina WG
Greenhorn

Joined: Dec 12, 2002
Posts: 19
thank you William!
at the moment I donnot know something about "application" variable.
but I need to get continuous output by JSP. that is the reason I want to update JSP right after the Servlet changes the object.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
The variable named "application" is automatically available on all JSP. It is the ServletContext for the web application.
What do you mean by "continuous output by JSP"?
Bill
Annina WG
Greenhorn

Joined: Dec 12, 2002
Posts: 19
That means whenever the object is changed, the output should be refreshed.
Annina WG
Greenhorn

Joined: Dec 12, 2002
Posts: 19
the flow is client-> servlet->Bean, servlet->client, servlet->JSP->Bean.
client calls servlet, servlet sends response back to client and writes data to Bean, mean while servlet also calls JSP to read from Bean.
i know if I use response.sendRedirect() in Servlet, then there are some <meta> I can use to make JSP refresh. but in my case, since response has been sent to client, response.sendRedirect() wonn't work in Servlet.
then how can I do it?
[ December 16, 2002: Message edited by: Annina WG ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Well, thats a classical problem. HTML is inherently a request - response cycle, it requires action on the client end to start the cycle. People have used applets or Javascript on the client side to poll the server to see if new data is available - if it is a new request for the JSP would be made.
There are alot of options here - how much formatting does the JSP do?
Bill
Annina WG
Greenhorn

Joined: Dec 12, 2002
Posts: 19
yes, what you said is exactly what I need to do.
do you have any example code either in JavaScript or Applet?
what do you mean by "formatting"?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
With respect to formatting - I was thinking that if the JSP was not too complex you might display the data in an applet which could poll the server every now and then. I don't have any exact example code handy, maybe somebody who has done this before can comment.
Bill
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Originally posted by Annina WG:
the flow is client-> servlet->Bean, servlet->client, servlet->JSP->Bean.
client calls servlet, servlet sends response back to client and writes data to Bean, mean while servlet also calls JSP to read from Bean.
i know if I use response.sendRedirect() in Servlet, then there are some <meta> I can use to make JSP refresh. but in my case, since response has been sent to client, response.sendRedirect() wonn't work in Servlet.
then how can I do it?
[ December 16, 2002: Message edited by: Annina WG ]

The <meta> tag that causes the page to refresh will in fact cause it to re-fetch the URL that originally generated the page. If there is a servlet at that URL that looks up the data and places it in the application context, then the JSP that it then calls will display the correct value.
This is basically why people use the MVC design pattern. If you always handle looking up data in a Servlet and then just forward() to a JSP passing information only in the request context, then you will never have a problem like this -- <meta> tags will always refer to the servlet, and the servlet will always fetch the correct data and hand it off to the JSP.
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Annina WG
Greenhorn

Joined: Dec 12, 2002
Posts: 19
you mean I need another Servlet to poll on the data and call JSP? this is an idea. I am trying.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Notify JSP that ServletContext been changed?
 
Similar Threads
how to know which servlet and jsp version
session problem in jsp
Session Scope
Correct answer
Servlet Context