%@ page contentType="application/rss+xml; charset=UTF-8" 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" %><%
/* JSP code written by David Hayes.
* --------------------------------
* This code takes a connection to a database, selects from the table RSS the top
* 15 records. From this it builds up an RSS feed from the results. It deals with
* management of Last-Modified and Conditional-GET's from the client, and will
* only deliver content if they haven't got a most recent version.
*/
// Database initialisation
Connection connection = (Connection) request.getAttribute("connection");
Statement statement = connection.createStatement();
// OK, firstly, lets get the top most news feed. This will give us our Last-Modified date
ResultSet 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);
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;
%>
Smarter Things Feed
http://www.smarterthings.co.uk
Things, Smarter!en-gbCopyright 2004-2005 David Hayes<%
// If we have data, use the latest date, otherwise use the current date
if(dtHeader != null)
out.write(df.format(dtHeader));
else
out.write(df.format(new Date()));
%><%
// If we have data, use the latest date, otherwise use the current date
if(dtHeader != null)
out.write(df.format(dtHeader));
else
out.write(df.format(new Date()));
%>http://backend.userland.com/rssDavyBoy's excellent JSP Scriptdavid@smarterthings.co.ukdavid@smarterthings.co.uk
<%
// OK now we need to go through all of the records
while(rs.next()){
rssid = rs.getInt("rssid");
strTitle = rs.getString("title");
strDescription = rs.getString("description");
strLink = "http://www.smarterthings.co.uk/news/id/"+Integer.toString(rssid);
dateRecord = rs.getTimestamp("DateStamp");
%> <% out.print(strTitle); %><% out.print(strDescription); %>
<% out.print(strLink); %>
<% out.print(df.format(dateRecord)); %>
<%
}
%>
<%
}
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();
%>