Take the 2-minute tour ×

I'm using IBM WebSphere as my servlet container. My application has several servlets and Java classes. My intent is to call one of those servlets directly from a Java class. Doing some research I figured out that is possible to use the RequestDispatcher interface to achieve this. But it is necessary to pass the objects ServletRequest and ServletResponse as arguments to the method forward(). There is some way to bypass this safely and "nicely"? By "nicely" I meant to say preserving good programming and design patterns.

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

It would help to get some more background as to why you are trying to do this. I am assuming you want to invoke some piece of business logic in the servlet. This is a sign that the application is poorly designed.

Are you familiar with MVC architecture? If your "model" code was loosely coupled, you would be able to call it directly.

share|improve this answer
That's the case, the application is really poorly designed, thus the cause of my struggling. But I can't refactor the business logic behind this servlet, at least not now. The servlet to be called was provided by another company to offer a interface to a proprietary legacy system. –  Renan Vinícius Mozone Jun 10 '10 at 14:17
A bit decent IDE (Eclipse, Netbeans, IntelliJ, etc) can refactor code in a few clicks. You would be ready sooner than the time needed to post this question, waiting for answers and inventing a hack/workaround which ain't going to work. –  BalusC Jun 10 '10 at 14:18
I fully understand and agree with you. But, unfortunately, there is some political issues that prevents me of doing any refactoring right now. I just don't want to call the servlet using HTTP inside the same application. –  Renan Vinícius Mozone Jun 10 '10 at 14:28
Then I would give ewernli's idea a shot. –  dbyrne Jun 10 '10 at 14:46
Honestly said, ewernli's idea is not feasible. –  BalusC Jun 10 '10 at 14:58
show 1 more comment

The only way to do this nicely is to decouple the desired logic from the servlets. This requirement is a sign that the servlets are too tight coupled with business/domain code logic which apparently needs to be used as well outside the webapplication context.

Refactor the original servlet code into reuseable Java class(es) and method(s) (which in turn does not use anything from the javax.servlet package) so that you can finally import and invoke it from both the servlet class and the "plain vanilla" Java class.

share|improve this answer
add comment

You could write a filter that stores the current request and response in static ThreadLocal, so that you can use them later from within the same request. You can then implement your own static method forward that uses them and dispatch to another page.

This is somehow the approach taken in JSF where FacesContext.getCurrentInstance can be accessed anytime.

But I wouldn't qualify that as an elegant design. Rather try to follow @BalusC advice and refactor your logic.

share|improve this answer
re: `FacesContext.getCurrentInstance` can be accessed anytime. - this is because the FacesServlet (or similar controller) initializes it as a ThreadLocal variable before invoking the lifecycle. You will not get the same Faces context in another thread. –  McDowell Jun 10 '10 at 14:51
@McDowell Sure. That's exactly what I describe in the first § with the filter and ThreadLocal. Is my answer unclear? –  ewernli Jun 10 '10 at 15:43
no, my comment was foolish. I must have glanced over your answer and missed the blatantly obvious. I think I've tried to explain the ThreadLocal/FacesContext thing so many times I have an automatic response to the topic. Apologies! –  McDowell Jun 10 '10 at 21:39
add comment

There is no way to call servlet from a java class but from a servlet you can call a java class ..

So if you need to pass something from java class to servlet you can create a method with that return type like string,int most list to contain lot of data and so..

share|improve this answer
add comment

Your Answer


