23 November 2006

Test Code for OTN Question

http://forums.oracle.com/forums/thread.jspa?threadID=446801&tstart=0

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

response.setContentType(CONTENT_TYPE);

HttpSession session = request.getSession();
Date now = new Date(System.currentTimeMillis());
SimpleBean smb = null;

smb = (SimpleBean)session.getAttribute("when");
if (smb==null) {
smb = new SimpleBean();
}
smb.setId(session.getId());
smb.setWhen(now);
session.setAttribute("when", smb);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
index.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>index</title>
</head>
<body>

<%
Object o = session.getAttribute("when");
if(o !=null) {
%>

When: <%=o%>

<%}
%>
<p>
<a href="setsessionservlet">Access the Servlet</a>
</p>
</body>
</html>
SimpleBean:
package oracle.otn.example;

import java.sql.Date;
import java.text.DateFormat;

public class SimpleBean {
private Date when;
private String id;
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);

public SimpleBean() {
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}


public Date getWhen() {
return when;
}

public void setWhen(Date when) {
this.when = when;
}

public String toString() {
return id + "::" + df.format(when);
}
}
This produces output such as the following on a series of requests:
When: 0abb70c7231c61d925be0b52462a8f04cf9600f3cff7::23 November 2006 12:15:47
When: 0abb70c7231c61d925be0b52462a8f04cf9600f3cff7::23 November 2006 12:15:58
When: 0abb70c7231c61d925be0b52462a8f04cf9600f3cff7::23 November 2006 12:16:07

No comments: