This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with RequestDispatcher

 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again. My directory structure looks like this:
tomcat
|--webapps
|-------openjms
|------------WEB-INF
|-----------------classes
|----------------------CreateConsumer.java
|-------project
|------------WEB-INF
|------------------classes
|-----------------------project
|----------------------------DBServlet.java
If DBServlet.java is running, how do I use the RequestDispatcher to send the request to CreateConsumer.java??
is it
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(../../../..???);
thanks.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer is that no, you shouldn't. Each of your servlets exist in separate application contexts, meaning that they pretend to be completely different web applications.
The advantage that you have is that YOU KNOW that the two applications are running side by side, and there is a way to cros over between them.
If you get the current servlet context (via getServletContext() ), which represents the container for the current servlet, it can get a reference to another (named) context. Then you can use this context to dispatch to an external resource.
To recap:
get current context
get another named context (usually as "\openjms", but it may be "openjms")
then get the dispatcher to the servlet name (ie CreateConsumer, if that is what you have mapped it as)
then include or forward to it.
(please note that the support for this sort of functionality is vendor specific and you may find it doesn't work on you server...)
Dave.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are going cross-context, so you'll have to investigate how to enable this in your servlet container (because for the most part, this feature is disabled by default, for security reasons).

Also, check out the API on getContext()
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. The applications are running side by side.
Thanks, I got it working in this way.

there was another thing after typing this code. I was getting an error 405, saying that the POST method was not implemented, or something like that. So in my CreateConsumer.java I added:

and it worked. Why did I have to do that? I had only implemented the doGet method. (note that DBServlet.java inserts information to a database. The info comes from a form.)
Now, this question:
- Should you avoid a situation like this when you communicate with different webapps? I think is a bit cumbersome....
thanks..
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another quick question:
if DBServlet.java uses a bean (which is stored in the same place as DBServlet), How do I access the properties of this bean when CreateConsumer.java is running?
Do I have to import the bean? How would the import statement be like?
import project.FormBean???
 
Meng Tan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
From my understanding,
If the request object that is passed into the forward or include method of the RequestDispatcher is a POST request then the doPost() method of the Servlet implementing the RequestDispatcher will be called. The same is also true for GET request.
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic