• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Notify JSP that ServletContext been changed?

 
Annina WG
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means whenever the object is changed, the output should be refreshed.
 
Annina WG
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Annina WG
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean I need another Servlet to poll on the data and call JSP? this is an idea. I am trying.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic