I have a servlet that calles other Java classes which perform various functions. Now can I have one of these classes access the servlet directly, without setting up a socket based connection. What I mean is can I just simply call its methods and have access to the HttpServletResponse/HttpServletRequest objects?
Usually the servlet gets accessed via an HTTP client where then servlet will call classes it needs to access, where here I have a class accessing the servlet.
I do not have the capability of running servlets at the moment so I cannot just do a quick and dirty test to see how it would be done.
Any input would be appreciated.
Originally posted by Jonathan Wilson:
Can you give some more information as to what your class is actually doing? I'm having problems imagining why a class would need to access a servlet. I suspect there may be a better way to accomplish what you are doing.
I am sure there is a better way of doing what I want to do.
But I would like to share the CGIVariables that the servlet provides witht the class that I am using, that's the bottom line.
I have before setup an httpclient to access a servlet but I don't want to use this method for this scenario. If there is an alternative route please do share I would appreciate it.
So, the flow would go something like this:
1) A request is made to the servlet
2) The servlet creates an instance of a command bean. It calls setter methods on the bean corresponding to specific request headers
3) The servlet executes the command bean
4) The servlet forwards the request (and perhaps the bean) to a JSP for display
I don't know if that is at all what you are trying to do, but I hope that helps.
If your design is such that you have methods in your Servlet that other Java classes need to call, then your design is bad.
you should move these methods to a seperate class this way all Java classes and Servlets can call them.
You can create an instance of this new class in your servlet and pass the reference to the other Java class that needs to also clal this method.
Or you can make the class have static methods so you don't even have to instantiate the class.