How to forward request from web1/servlet to web2/servlet?

南楼画角 提交于 2019-12-30 02:32:07

问题


I've two web applications say web1 and web2. I want to forward a request from web1/servlet1 to web2/servlet2. Is it possible? Please help!


回答1:


This is a two-step process:

  1. Get hold of the ServletContext representing web2
  2. Get the RequestDispatcher from that ServletContext corresponding to servlet2

So, something like this, from inside servlet1:

ServletContext web1 = getServletContext();
ServletContext web2 = web1.getContext("/web2");
RequestDispatcher dispatcher = web2.getRequestDispatcher("/servlet2");
dispatcher.forward(request, response);

There's a big caveat to all of this - the container may not be configured to permit cross-context forwarding, since it's a potential security risk. If this is the case, getContext("web2") will return null.



来源:https://stackoverflow.com/questions/4889113/how-to-forward-request-from-web1-servlet-to-web2-servlet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!