aspose file tools*
The moose likes Servlets and the fly likes Accessing a servlet from another Class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Accessing a servlet from another Class." Watch "Accessing a servlet from another Class." New topic
Author

Accessing a servlet from another Class.

El Durango
Greenhorn

Joined: Oct 09, 2003
Posts: 23
Not to confuse anyone too much, but here's the scenario.
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.
Jonathan Wilson
Greenhorn

Joined: Oct 14, 2003
Posts: 1
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.
Sri Basavanahally
Ranch Hand

Joined: Oct 07, 2003
Posts: 75
Also, for you to call a servlet's methods directly v/s a container calling them, may cause problems. Just another thought :-)
-Sri


UP THE IRONS !
El Durango
Greenhorn

Joined: Oct 09, 2003
Posts: 23
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.
Jon Wilson
Greenhorn

Joined: Oct 15, 2003
Posts: 14
I still don't have a good feel for what it is that you are trying to do (the big picture), but if you want another class to have access to some of the request headers, then you could always have the servlet create a bean containing this data. The bean could then be passed to the class. Or, depending on what it is you are doing, the bean could be a simple command bean that is executed.
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
Example:
bean.setReferrer(request.getHeader("referer"));
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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

I had posted this response in another thread.
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.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessing a servlet from another Class.