ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzamy_5.4.0.1/50/program/sessamp.htm

55 lines
1.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Example: SessionSample.java</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><a name="sessamp"></a>Example: SessionSample.java</h5>
<pre>import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionSample extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Step 1: Get the Session object
boolean create = true;
HttpSession session = request.getSession(create);
// Step 2: Get the session data value
Integer ival = (Integer) session.getValue (&quot;sessiontest.counter&quot;);
if (ival == null) {
ival = new Integer (1);
}
else {
ival = new Integer(ival.intValue () + 1);
}
session.putValue (&quot;sessiontest.counter&quot;, ival);
// Step 3: Output the page
response.setContentType(&quot;text/html&quot;);
PrintWriter out = response.getWriter();
out.println(&quot;&lt;html&gt;&quot;);
out.println(&quot;&lt;head&gt;&lt;title&gt;Session Tracking Test&lt;/title&gt;&lt;/head&gt;&quot;);
out.println(&quot;&lt;body&gt;&quot;);
out.println(&quot;&lt;h1&gt;Session Tracking Test&lt;/h1&gt;&quot;);
out.println(&quot;You have hit this page &quot; + ival + &quot; times&quot; + &quot;&lt;br&gt;&quot;);
out.println(&quot;Your &quot; + request.getHeader(&quot;Cookie&quot;));
out.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);
}
}</pre>
</body>
</html>