• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing a servlet from another Class.

 
El Durango
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, for you to call a servlet's methods directly v/s a container calling them, may cause problems. Just another thought :-)
-Sri
 
El Durango
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic