1

I am using Eclipse Helios to program JSP and POJOs running on Tomcat 5.5. I am trying to use the org.apache.catalina.connector.Response class as follows:

import org.apache.catalina.connector.*; ... Response resp = (Response) r;

where r is an instance of HttpServletResponse. Eclipse gives no warnings, and the project compiles fine. However, when I browse to a page that contains this code, I get the following error:

java.lang.ClassNotFoundException: org.apache.catalina.connector.Response

any idea of what is going wrong?

I have also tried it this way, with the same result.

org.apache.catalina.connector.Response resp = (org.apache.catalina.connector.Response) r;

Thanks,

Ean

ean
  • 41
  • 1
  • 4
  • 1
    *Why* do you want to cast it back to a servletcontainer specific class which would make your app unable to run on other servletcontainers? Chances are *big* you don't need to do it that way at all. – BalusC Mar 07 '11 at 03:28
  • I suspect that you didn't understand how to implement Bozho's answer on [your previous question](http://stackoverflow.com/questions/5214066/view-response-headers-before-they-are-sent-to-the-client). Is this true? Bozho's answer is the only and the right way to achieve this. If you have trouble implementing it accordingly, you should ask about it rather than asking how to achieve the wrong solution. – BalusC Mar 07 '11 at 03:45
  • You're right. I'm trying another way around Bozho's approach. Perhaps you can explain what he wrote to me? I didn't understand his suggestion, and he didn't write me back. The reason I want to turn it to a org.apache.catalina.connector.Response is to read what's in the HTTP response headers before they are sent to the client. Any suggestions or clarifications are most appreciated. – ean Mar 07 '11 at 04:08

3 Answers3

1

A ClassNotFoundException means that a class was available at compile time, but not at run time. So it makes no difference how you import it in your source code.

I haven't checked, but it's quite possible that Tomcat doesn't let web apps access its internal classes. And the reason I haven't checked is that I know it'd be a bad idea even if I could do it. You should code to the API (in this case, the stuff under javax.servlet.*) not to the implementation.

Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71
1

Clearly, the Tomcat designers don't think that it is a good idea for webapps get at its internal classes. Here's a couple of possible reasons:

  • It makes your code dependent on Tomcat.
  • It makes your code fragile in the face of changes to internal details; i.e. it depends on the Tomcat version.
  • The Tomcat stack or other things inside your webapp context could do things that mean that your Response object is a wrapper rather than the class you are expecting.

To change make your code "work" you would probably need to tinker with the Tomcat JAR files ... which will make it even less portable. For more information that might help you to slit your own throat, refer here. Note that this is Tomcat 5.5 specific ... and the Tomcat 6.0 version of the page is different.


The reason I want to turn it to a org.apache.catalina.connector.Response is to read what's in the HTTP response headers before they are sent to the client.

I see. Well perhaps you should be implementing a custom Valve. (This is a Tomcat-specific thing.) Or better still, see if one of the existing Valve implementations will do the job for you.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I don't' think you can use that class. I am not sure what you are trying to do, but use HttpServletResponse and you should be good to go.

Arash
  • 11,697
  • 14
  • 54
  • 81
  • It's definitely possible in other way, but it's just the wrong approach :) Sorry, but this answer is useless. – BalusC Mar 07 '11 at 03:35