Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the org.apache.catalina.connector.Request object in Tomcat?

I need to write a Servlet or Filter class that can get access to the org.apache.catalina.connector.Request object, which is wrapped in the RequestFacade object given to my servlet. Casting doesn't work, since RequestFacade is not a subclass of Request.

The reason I need this is because I am trying to call the setRequestedSessionId() method of Request, and this is apparently not part of the Http servlet spec. The reason I need to do this is because the session ID is being included in a URL under a different name than JSESSIONID. I can't change the URL or the name of the parameter, so I'm trying to associate the request with the correct session by extracting the session ID and call Request.setRequestedSessionId().

I have actually solved the problem using a Valve subclass, but I don't like using a Valve because as far as I can tell, I need to install my Valve subclass in the Tomcat/server/classes directory instead of packaging it with the rest of my webapp. If there was a portable way to do this across different Servlet containers, that would be great, but at the moment I'm resigned to making this Tomcat-specific.

Here is the working Valve code:

public class SessionSetter extends ValveBase {
public void invoke( Request request, Response response ) throws IOException, ServletException {
    String sessionId = request.getParameter( "whatever" );
    request.setRequestedSessionId( sessionId );
}

}

Is there some way to do the same thing in a Servlet or Filter? Or is there some way to package the Valve class in my application .war file?

like image 938
Jesse Barnum Avatar asked Feb 14 '11 20:02

Jesse Barnum


People also ask

Where is Catalina in Tomcat?

Tomcat is actually composed of a number of components, including a Tomcat JSP engine and a variety of different connectors, but its core component is called Catalina. Catalina provides Tomcat's actual implementation of the servlet specification; when you start up your Tomcat server, you're actually starting Catalina.

What is Catalina file in Tomcat?

Catalina. Catalina is Tomcat's servlet container. Catalina implements Sun Microsystems' specifications for servlet and JavaServer Pages (JSP). In Tomcat, a Realm element represents a "database" of usernames, passwords, and roles (similar to Unix groups) assigned to those users.

What is HTTP connector in Tomcat?

The HTTP Connector element represents a Connector component that supports the HTTP/1.1 protocol. It enables Catalina to function as a stand-alone web server, in addition to its ability to execute servlets and JSP pages.

Which modules in Tomcat are connected with the help of connector?

The mod_jk connector is an Apache HTTPD module that allows HTTPD to communicate with Apache Tomcat instances over the AJP protocol. The module is used in conjunction with Tomcat's AJP Connector component.


1 Answers

(this is my comment upgraded to an answer)

If you're using Tomcat 5.5 or 6 then this might be an alternative you could look at: http://tomcat.apache.org/tomcat-6.0-doc/config/systemprops.html#Sessions. You can change the names used for session id parameter using system properties. It's not available for Tomcat 7 though as far as I can tell (I think the servlet spec that tc7 implements is stricter about changing the name).

As for accessing the Tomcat internal request object from your webapp (servlet or filter), I don't think you'll be able to. I seem to recall reading somewhere that the RequestFacade class exists explicitly to prevent that, so that a webapp can't mess with the the Tomcat internals.

like image 71
matt Avatar answered Oct 12 '22 22:10

matt