开发者

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

I am using Ecli开发者_JAVA百科pse 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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜