FileDocCategorySizeDatePackage
ShowSession.javaAPI DocExample1627Mon May 01 14:41:50 BST 2000None

ShowSession.java

//file: ShowSession.java
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.util.Enumeration;

public class ShowSession extends HttpServlet {

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

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
      throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter(  );

        out.println(
          "<html><head><title>Show Session</title></head><body>");
        HttpSession session = request.getSession(  );

        out.println("<h1>In this session:</h1><ul>");
        String [] names = session.getValueNames(  );
        for (int i=0; i< names.length; i++)
            out.println(
              "<li>"+names[i]+" = "+session.getValue( names[i] ));

        // add new name-value to session
        String name = request.getParameter("Name");
        if ( name != null ) {
            String value = request.getParameter("Value");
            session.putValue( name, value );
        }

        out.println(
          "</ul><p><hr><h1>Add String</h1>"
          + "<form method=\"POST\" action=\""
          + request.getRequestURI(  ) +"\">"
          + "Name: <input name=\"Name\" size=20><br>"
          + "Value: <input name=\"Value\" size=20><br>"
          + "<br><input type=\"submit\" value=\"Submit\"></form>"
        );

        out.close(  );
    }
}