Servlets 哪个类实现了getSession()方法?
我对各种方法的实现有点困惑。 实际上,我在学习Servlet和JSP,我发现了很多方法,比如Servlets 哪个类实现了getSession()方法?,servlets,Servlets,我对各种方法的实现有点困惑。 实际上,我在学习Servlet和JSP,我发现了很多方法,比如 request.getSession(); response.getWriter(); 那么,有谁能告诉我方法(getSession(),getWriter(),等等)的实现在哪里(在哪个类中)?有您感兴趣的实现,由及其父级实现 毫不奇怪,它是getWriter()的起点,但实现存在于(实际上在它的超类中) 然而,这一切都应该是可搜索的:您可以从servlet调用它们。如果你打电话给他们,你就已经导
request.getSession();
response.getWriter();
那么,有谁能告诉我方法(getSession()
,getWriter()
,等等)的实现在哪里(在哪个类中)?有您感兴趣的实现,由及其父级实现
毫不奇怪,它是getWriter()
的起点,但实现存在于(实际上在它的超类中)
然而,这一切都应该是可搜索的:您可以从servlet调用它们。如果你打电话给他们,你就已经导入了他们。如果已导入它们,则具有完全限定的类名。如果您有FQN,您可以查找该类的Javadocs。如果您有Javadocs,那么您就有指向超类/接口的链接。它们存在于servletcontainer本身中。更重要的是,servletcontainer本身实际上就是ServletAPI的整个具体实现 对于(即),类提供了
request.getSession()
的实现。在当前的Tomcat 7.0.25版本中,它如下所示:
2288 @Override
2289 public HttpSession getSession() {
2290 Session session = doGetSession(true);
2291 if (session == null) {
2292 return null;
2293 }
2294
2295 return session.getSession();
2296 }
等价地,response.getWriter()
实现由类提供,在Tomcat 7.0.25中如下所示:
628 @Override
629 public PrintWriter getWriter()
630 throws IOException {
631
632 if (usingOutputStream) {
633 throw new IllegalStateException
634 (sm.getString("coyoteResponse.getWriter.ise"));
635 }
636
637 if (ENFORCE_ENCODING_IN_GET_WRITER) {
638 /*
639 * If the response's character encoding has not been specified as
640 * described in <code>getCharacterEncoding</code> (i.e., the method
641 * just returns the default value <code>ISO-8859-1</code>),
642 * <code>getWriter</code> updates it to <code>ISO-8859-1</code>
643 * (with the effect that a subsequent call to getContentType() will
644 * include a charset=ISO-8859-1 component which will also be
645 * reflected in the Content-Type response header, thereby satisfying
646 * the Servlet spec requirement that containers must communicate the
647 * character encoding used for the servlet response's writer to the
648 * client).
649 */
650 setCharacterEncoding(getCharacterEncoding());
651 }
652
653 usingWriter = true;
654 outputBuffer.checkConverter();
655 if (writer == null) {
656 writer = new CoyoteWriter(outputBuffer);
657 }
658 return writer;
659
660 }
因此,每个servletcontainer/appserver(、、等)都有自己的实现(尽管它们大多使用Tomcat的分支)。包装器不是实现。。。默认情况下,它们只是将所有方法委托给在构建过程中传递的包装实例(按照decorator模式;这样开发人员就不需要重复开发人员不需要重写的无数方法)。