Date Servlet: doGet method : Servlet Methods « Servlet « Java Tutorial






import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class DateServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    HttpSession hs = request.getSession(true);

    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.print("<B>");

    Date date = (Date) hs.getAttribute("date");
    if (date != null) {
      pw.print("Last access: " + date + "<br>");
    }

    date = new Date();
    hs.setAttribute("date", date);
    pw.println("Current date: " + date);
  }
}








25.2.Servlet Methods
25.2.1.Using Servlet Init Method
25.2.2.Date Servlet: doGet method
25.2.3.Returns any parameters and lists server properties.