Hi All, How to forward a servlet on a different context? Context - > test1 -> ServletTest1 Context - > test2 -> ServletTest2 On ServletTest1 i want forward a ServletTest2. getServletContext().getRequestDispatcher("/test2/servlet/ServletTest2").forward(req, res); Don't work. Best Regards,
Depending on how well it is implemented on the server you are using (it isn't always useful), this is possible but not a good idea. The reason it's a bad idea is that it creates dependencies between your applications. A context should be a stand-alone application. If you need the servlet, it may be better to import it into the context and give it its own mapping. That said, this may work: getServletContext().getContext( "test2" ).getRequestDispatcher("/servlet/ServletTest2").forward(req, res); The call to 'getRequetDispatcher' is always specific to a context, which is why your call didn't work. This reaches from one context to the next and makes method calls from that context. Depending on the sever you are using, you might need "/test2" rather than "test2". Dave
Sent by chandm I quoted your reply to a previous post and I wanted to ask you what you meant. I have been creating Web application which send control over to a servlet, the servlet does some work and uses RequestDispatcher.forward to send a JavaBean and the request to a JSP page. The thing is it fails a lot after using a stress tool to test it out. Is this attributed to what your stating below and the better way you offer is that using RequestDispatcher.include. Does that make it stay in the same context? Thanks.
If you aren't explicitly calling a different context then you always remain in the same one. If you are crossing contexts then you are likely to have a range of problems depending on the server you are using. One possible problem I forgot to mention is that of thread safety. When you cross between contexts, it may be that you get assigned a different thread in the other context. If this is the case then code may not execute in the order you expect, and Objects that you though were safe may be altered by different threads at the same time. This could certainly cause problems during load testing. I can't really say more without specific information of whats going on. Dave