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

Discussion in 'Java' started by Steven Hines, Feb 4, 2004.

  1. Steven Hines

    Steven Hines Guest

    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" %>
     
    Steven Hines, Feb 4, 2004
    #1
    1. Advertising

  2. Steven Hines wrote:

    > 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
     
    John C. Bollinger, Feb 4, 2004
    #2
    1. Advertising

  3. Steven Hines

    Steven Hines Guest

    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
     
    Steven Hines, Feb 5, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pat Carden
    Replies:
    7
    Views:
    6,785
    Juan T. Llibre
    Nov 20, 2005
  2. Aleksi Kallio
    Replies:
    1
    Views:
    1,034
    John C. Bollinger
    Aug 4, 2003
  3. Anders Skar
    Replies:
    1
    Views:
    2,172
    Anders Skar
    Jul 22, 2004
  4. Geoff Winsor
    Replies:
    6
    Views:
    43,656
    Andrew Thompson
    Jan 13, 2005
  5. jitender

    http status 404 /login.jsp

    jitender, Jan 25, 2005, in forum: Java
    Replies:
    1
    Views:
    6,289
    Heiner K├╝cker
    Jan 25, 2005
Loading...

Share This Page