Servlets Java

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( "


" );
pw.println( new java.util.Date( System.currentTimeMillis() ) );
}
}