%@ page contentType="text/html; charset=iso-8859-1" language="java" errorPage="" import="java.sql.*,javax.sql.DataSource,java.util.Date,java.util.Calendar,java.util.Locale,java.text.SimpleDateFormat,java.util.TimeZone" %><%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
// Database initialisation
Connection connection = (Connection) request.getAttribute("connection");
Statement statement = connection.createStatement();
String id = request.getParameter("id");
// OK, firstly, lets get the top most news feed. This will give us our Last-Modified date
ResultSet rs;
if (id != null && id.length() > 0)
rs = statement.executeQuery("select DateStamp from RSS where RSSID='" + id + "'");
else
rs = statement.executeQuery("select DateStamp from RSS order by DateStamp DESC limit 0, 1");
Date dtHeader = null;
try{
rs.next();
dtHeader = rs.getTimestamp("DateStamp");
}catch(Exception e){}
rs.close();
// If we did return a record, set the value of the Last Modified and Expires in the Response header
// Otherwise we use the implicit values created by the JSP host.
if (dtHeader != null){
response.setDateHeader("Last-Modified",dtHeader.getTime());
Calendar cal = Calendar.getInstance();
cal.setTime(dtHeader);
cal.add(Calendar.YEAR,1);
response.setDateHeader("Expires",cal.getTimeInMillis());
}
// Let's check the HTTP 1.1 Request Header, to see if we have got a modified since date.
long lRequestTime = request.getDateHeader("If-Modified-Since");
// If the database contains newer information than the client has, or there is no data
// on the database, then lets send out the RSS feed (even if won't contain any items)
if (lRequestTime == -1 || dtHeader == null || lRequestTime < dtHeader.getTime()) {
// Lets fetch the top 15 news stories
// Note, we cover the case that a new story could have been added since we
// ran the first select stament. To prevent against a possibility that the
// last modified date would be less than the most recent date in the stories
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.UK);
if (id != null && id.length() > 0)
rs = statement.executeQuery("select * from RSS where RSSID='" + id + "'");
else
rs = statement.executeQuery("select * from RSS where RSS.DateStamp <= cast('"+df.format(dtHeader)+"' as datetime) order by DateStamp DESC limit 0, 15");
// We need to setup a converter to convert dates to the RFC-822 standard
df = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.UK);
// Define the local variables that we'll need to use for the feed.
int rssid = 0;
String strTitle = "";
String strDescription = "";
String strLink = "";
Date dateRecord = null;
%>
<%
// OK now we need to go through all of the records
while(rs.next()){
strTitle = rs.getString("title");
strDescription = rs.getString("description");
strLink = rs.getString("link");
dateRecord = rs.getTimestamp("DateStamp");
%>
<% out.write(strTitle); %>
<% out.write(strDescription); %>
Reported: <% out.write(df.format(dateRecord)); if (strLink != null && strLink.length() > 0) {%>
More<%}%>
<%
}
%>
<%
}
else{
// If the client is up to date, then lets return them nothing, and inform them
// that they are already up to date.
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
// Finally, lets clean up after ourselves.
rs.close();
statement.close();
connection.close();
%>