How do I set the HTTP status of my error page to 404 in Tomcat?

S

Steven Hines

Hi all - am having trouble finding an answer to this question.

I'm running Tomcat 4.1.27 on Windows 2000 Server, and I would like to
know how to manually set the HTTP status code of a web page to 404.
Allow me to explain...

I have recently rebuilt my old website but the search engines (in
particular YahooSeeker from inktomisearch.com) are still trying to
index pages under the old URLs they have stored in their databases. I
have a default user-friendly error page that is served whenever a use
encounters a 404. The trouble is that, when the search engines request
an old page, they get the error page. The logs tell me that this error
page is being served with an HTTP status of 200 which means the search
engines think that it's a valid page. This means that they continually
try to index old pages that aren't there anymore.

How can I manually set the HTTP status code of my error page to always
be 404, yet still show the user-friendly page to normal browsers?

All ideas gratefully received!

Thanks in advance,

Steve

PS can anyone tell me why I get so many & characters in web log
entries such as this:

yj1035.inktomisearch.com - - [04/Feb/2004:11:25:33 +0000] "GET
/scripts/display.cgi?type=hotels&city=238&&&&&&&&&&&&&first=1&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&sort=alpha
HTTP/1.0" 200 8678 "-" "YahooSeeker/1.0 (compatible; Mozilla 4.0; MSIE
5.5; http://help.yahoo.com/help/us/shop/merchant/)"


---------
TECH INFO
---------

Here are some of the relevant entries from my web.xml file:

<servlet>
<servlet-name>errorDispatcher</servlet-name>
<servlet-class>com.ora.jsp.servlets.ErrorDispatcherServlet</servlet-class>
<init-param>
<param-name>errorPage</param-name>
<param-value>/error.jsp?debug=log</param-value>
</init-param>
</servlet>

<!-- The mapping for the servlet to handle run-time errors -->
<servlet-mapping>
<servlet-name>errorDispatcher</servlet-name>
<url-pattern>/errorDispatcher</url-pattern>
</servlet-mapping>

<!-- Error pages -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorDispatcher</location>
</error-page>

<error-page>
<error-code>400</error-code>
<location>/errorDispatcher</location>
</error-page>

<error-page>
<error-code>401</error-code>
<location>/errorDispatcher</location>
</error-page>

<error-page>
<error-code>403</error-code>
<location>/errorDispatcher</location>
</error-page>

<error-page>
<error-code>404</error-code>
<location>/errorDispatcher</location>
</error-page>

<error-page>
<error-code>405</error-code>
<location>/errorDispatcher</location>
</error-page>

<error-page>
<error-code>408</error-code>
<location>/errorDispatcher</location>
</error-page>

<error-page>
<error-code>500</error-code>
<location>/errorDispatcher</location>
</error-page>


Also, my error page error.jsp contains the following:

<%@ page isErrorPage="true" %>
 
J

John C. Bollinger

Steven said:
How can I manually set the HTTP status code of my error page to always
be 404, yet still show the user-friendly page to normal browsers?

You may be looking for HttpServletResponse.setStatus(int).


John Bollinger
(e-mail address removed)
 
S

Steven Hines

Thanks for the suggestion John.

After posting my question, I did some more research and, (wouldn't you
know it?!) I found a thread that helped me out. the subject of the
thread is "Tomcat and custom error pages" and you can find it at

http://groups.google.com/groups?hl=...my+error+page+to+404+in+Tomcat%3F&sa=N&tab=wg

My particular solution was to put the following line at the top of my
error page:

<% response.setStatus(response.SC_NOT_FOUND); %>

Cheers,

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top