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.
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
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.
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()
Joined: Nov 27, 2001
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..
Joined: Nov 27, 2001
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???
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.