Satyendra Singh
Satyendra Singh

Reputation: 46

Which class has the implementation of the getSession() method?

I am a little confused about the various methods' implementations. Actually, I was Learning Servlet and JSP and I found many methods like

request.getSession(); 
response.getWriter();

so please, can any one tell me where (In which class) the implementation of the methods (getSession(), getWriter(), etc) are present?

Upvotes: 1

Views: 3105

Answers (2)

BalusC
BalusC

Reputation: 1108712

They are present in the servletcontainer itself. Even more, the servletcontainer is at its own actually the whole concrete implementation of the Servlet API.

In case of Tomcat (which is open source), the implementation of request.getSession() is provided by the org.apache.catalina.connector.Request class. In the current Tomcat 7.0.25 release it look like this:

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    }

Equivalently, the response.getWriter() implementation is provided by org.apache.catalina.connector.Response class which look like this in 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    }

So has each other servletcontainer/appserver (Glassfish, JBoss AS, Jetty, WebLogic, WebSphere AS, etc) its own implementation (although they mostly use a fork of Tomcat).

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160191

HttpServletRequest has the implementations of getSession() you'll be interested in, implemented by HttpServletRequestWrapper and its parent SerlvetRequestWrapper.

Unsurprisingly, HttpServletResponse is the starting point for getWriter(), but the implementation lives in HttpServletResponseWrapper (actually in its superclass, ServletResponseWrapper).

This should all be searchable, however: you call those from servlets. If you call them, you've imported them. If you've imported them, you have the fully-qualified class name. If you have the FQN you can look up the class's Javadocs. If you have the Javadocs, you have links to superclasses/interfaces.

Upvotes: 0

Related Questions