Displaying the Current User Comments


When chat.html wants to display the current user comments in its top frame, it sends chat.jsp the parameter t, set to "1". Here's how the code at the beginning of chat.jsp checks that parameter:

 <% if(request.getParameter("t") != null &&     request.getParameter("t").equals("1")){ %>         .         .         . 

If it's time to display the current user comments, they need to be fetched from the application object. Chat keeps track of eight user comments at a time, named "text0" through "text7", as stored in the application object, and they can be retrieved using application.getAttribute("text0") and so forth. Besides simply displaying this text, chat.jsp must also tell the browser to come back in 5 seconds and get the user comments again to keep them current. It does that with an HTML tag <META> tag in the <HEAD> section of this HTML page, setting the HTTP header HTTP-EQUIV to "Refresh" and the CONTENT attribute to "5" (for 5 seconds). Note that you can set this time yourself to shorten or lengthen the time between refreshes. Here's how chat.jsp retrieves the user data from the application object and sets up the refresh rate:

 <% if(request.getParameter("t") != null &&     request.getParameter("t").equals("1")){ %>     <HTML>         <HEAD>             <meta HTTP-EQUIV="Refresh" CONTENT="5">         </HEAD>         <BODY> <%     String text0 = (String) application.getAttribute("text0");     String text1 = (String) application.getAttribute("text1");     String text2 = (String) application.getAttribute("text2");     String text3 = (String) application.getAttribute("text3");     String text4 = (String) application.getAttribute("text4");     String text5 = (String) application.getAttribute("text5");     String text6 = (String) application.getAttribute("text6");     String text7 = (String) application.getAttribute("text7");         .         .         . 

Now you've got the text that's to be displayed; to actually display that text, chat.jsp uses an HTML table this way:

 <% if(request.getParameter("t") != null &&     request.getParameter("t").equals("1")){ %>     <HTML>         <HEAD>             <meta HTTP-EQUIV="Refresh" CONTENT="5">         </HEAD>         <BODY> <%     String text0 = (String) application.getAttribute("text0");     String text1 = (String) application.getAttribute("text1");     String text2 = (String) application.getAttribute("text2");     String text3 = (String) application.getAttribute("text3");     String text4 = (String) application.getAttribute("text4");     String text5 = (String) application.getAttribute("text5");     String text6 = (String) application.getAttribute("text6");     String text7 = (String) application.getAttribute("text7");     out.println("<center><h1>Chat Room</h1></center>");     out.println("<center><table width='90%'>");     out.println("<tr><td><font size=2>");     if(text0 != null){         out.println(text0);     }     out.println("</font></td></tr>");     out.println("<tr><td><font size=2>");     if(text1 != null){         out.println(text1);     }     out.println("</font></td></tr>");     out.println("<tr><td><font size=2>");     if(text2 != null){         out.println(text2);     }     out.println("</font></td></tr>");     out.println("<tr><td><font size=2>");     if(text3 != null){         out.println(text3);     }     out.println("</font></td></tr>");     out.println("<tr><td><font size=2>");     if(text4 != null){         out.println(text4);     }     out.println("</font></td></tr>");     out.println("<tr><td><font size=2>");     if(text5 != null){         out.println(text5);     }     out.println("</font></td></tr>");     out.println("<tr><td><font size=2>");     if(text6 != null){         out.println(text6);     }     out.println("</font></td></tr>");     out.println("<tr><td><font size=2>");     if(text7 != null){         out.println(text7);     }     out.println("</font></td></tr>");     out.println("</table></center>"); %>         </BODY>     </HTML>         .         .         . 

That takes care of the upper frame in chat.html, which displays the current user comments, if there are any. What about accepting new comments when a user types something and clicks the Submit button?



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net