Tuesday, 30 August 2011

Accessing Date in Servlet - Program

//Accessing Date In Servlet
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class AccessedDateServlet extends HttpServlet
{
      public void doGet(HttpServletRequest request, HttpServletResponse response)
                                          throws ServletException, IOException
    {
        PrintWriter pw = response.getWriter();
        HttpSession session = request.getSession(true);
        Date create = new Date(session.getCreationTime());
        Date accessed = new Date(session.getLastAccessedTime());
        pw.println("ID " + session.getId());
        pw.println("Create: " + create);
        pw.println("Last Accessed: " + accessed);
        String dataName = request.getParameter("dataName");
        if (dataName != null && dataName.length() > 0) {
          String dataValue = request.getParameter("dataValue");
          session.putValue(dataName, dataValue);
        }
        String[] valueNames = session.getValueNames();
        if (valueNames != null &&  valueNames.length > 0)
    {
              for (int i = 0; i < valueNames.length; i++)
        {
                String str = valueNames[i];
                String str1 = session.getValue(str).toString();
                pw.println(str + " = " + str1);
              }
        }
      }

No comments:

Post a Comment