Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Servlets Code Examples

Http client pull model

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
Http client pull model package com.ack.web.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * * In this example, we demonstrate the client pull model. * * Here we have a client reading a book, specifying a * refresh every two seconds. The current page is * stored in the client's HttpSession. * * When we have finished the book, we redirect the client * to the acknowledge web site * * Client Pull sees the client pulling content from the * server using the Refresh HTTP header directive */ public class HttpClientPullModel extends HttpServlet { public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { res.setContentType( "text/html" ); PrintWriter pw = res.getWriter(); // get a session to hold the current page HttpSession session = req.getSession(); Integer pageNumber = (Integer) session.getAttribute( "page" ); if( pageNumber == null ) { pageNumber = new Integer( 1 ); } else { pageNumber = new Integer( pageNumber.intValue() + 1 ); } pw.println( "Page: " + pageNumber ); // finish the book when its gets to page 10 if( pageNumber.intValue() == 10 ) { // tell client we're done and kill the session pw.println( "we are done reading.." ); session.removeAttribute( "page" ); session.invalidate(); // finally, send the client to the client site after 5 secs res.setHeader( "Refresh", "5; URL=http://www.xxx" ); } else { // if we have more pages, signal refresh of current page res.setHeader( "Refresh", "2" ); session.setAttribute( "page", pageNumber ); } // output some time information pw.println( "<p><hr><p>" ); pw.println( new java.util.Date( System.currentTimeMillis() ) ); } }