<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>radikalFX &#187; Tomcat</title>
	<atom:link href="http://radikalfx.com/tag/tomcat/feed/" rel="self" type="application/rss+xml" />
	<link>http://radikalfx.com</link>
	<description>In full FX</description>
	<lastBuildDate>Tue, 27 Dec 2011 11:44:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Tomcat and if-modified-since header</title>
		<link>http://radikalfx.com/2008/03/23/what-to-do-with-google-seo-and-if-modified-since-header-in-your-servlet-jsp/</link>
		<comments>http://radikalfx.com/2008/03/23/what-to-do-with-google-seo-and-if-modified-since-header-in-your-servlet-jsp/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 10:25:54 +0000</pubDate>
		<dc:creator>cra5h</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://radikalfx.com/2008/03/23/what-to-do-with-google-seo-and-if-modified-since-header-in-your-servlet-jsp/</guid>
		<description><![CDATA[If you ever had to do something with web pages that need to be ranked the highest as possible, then you&#8217;ve probably read through the Google Webmaster Guidelines. But when I did that, I came across the following line: Make sure your web server supports the If-Modified-Since HTTP header. This feature allows your web server [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever had to do something with web pages that need to be ranked the highest as possible, then you&#8217;ve probably read through the <a href="http://www.google.com/webmasters/guidelines.html" title="Google Webmaster Guidelines" target="_blank">Google Webmaster Guidelines</a>. But when I did that, I came across the following line:</p>
<blockquote><p>Make sure your web server supports the If-Modified-Since HTTP header. This feature allows your web server to tell Google whether your content has changed since we last crawled your site. Supporting this feature saves you bandwidth and overhead</p></blockquote>
<p>Because Tomcat&#8217;s default is to let the browser / crawler know that it doesn&#8217;t cache pages, GoogleBot will go and download the page every time it visits that page. In general this doesn&#8217;t have to be bad, but because the website I am working on contains a lot of pages in google (&gt;500.000) it might be wise to sometimes give this if-modified-since header back to Google and thereby spare some bandwidth and server load.</p>
<p>So then we need to take a look if GoogleBot gives back this header and then compare it and see if the information should be re crawled by Google. For this example I will be taking a period of 7 days. Of course, you could also let this depend on the data in your system. But the website I am working on makes it a bit difficult to do this, because looking up if there is new data, will slowdown the entire load process of the website (for each request), which is something I would like to avoid.</p>
<p>So how does the code look, to get this done:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>
<span style="color: #003399;">String</span> ifModifiedSince <span style="color: #339933;">=</span> request.<span style="color: #006633;">getHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;if-modified-since&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ifModifiedSince <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>ifModifiedSince.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Format should be something like this: Thu, 10 Jan 2008 09:20:50 GMT</span>
    <span style="color: #003399;">SimpleDateFormat</span> sdf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EEE, d MMMM yyyy HH:mm:ss z&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Date</span> modifiedDate <span style="color: #339933;">=</span> sdf.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>ifModifiedSince<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">Calendar</span> modifiedCal <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    modifiedCal.<span style="color: #006633;">setTime</span><span style="color: #009900;">&#40;</span>modifiedDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Calendar</span> nowCal <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">long</span> sevenDays <span style="color: #339933;">=</span> <span style="color: #cc66cc;">604800000</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>modifiedCal.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> sevenDays <span style="color: #339933;">&gt;</span> nowCal.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      response.<span style="color: #006633;">sendError</span><span style="color: #009900;">&#40;</span>HttpServletResponse.<span style="color: #006633;">SC_NOT_MODIFIED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
response.<span style="color: #006633;">setHeader</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control&quot;</span>, <span style="color: #0000ff;">&quot;max-age=604800&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 7 days in seconds</span>
response.<span style="color: #006633;">setDateHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires&quot;</span>,
<span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">604800000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 7 days in milliseconds</span>
response.<span style="color: #006633;">setDateHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Last-Modified&quot;</span>, <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Now</span>
<span style="color: #339933;">%&gt;</span></pre></div></div>

<p>The code above should be enough to get the job done. But if you feel that this can be done better or easier, feel free to comment on this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://radikalfx.com/2008/03/23/what-to-do-with-google-seo-and-if-modified-since-header-in-your-servlet-jsp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

