例如:用户下单servlet跳转至另一个应用仓库管理的servlet中核对商品信息,再确认后返回用户请求。
<span style="font-size:14px;">public class test extends HttpServlet{
private static final long serialVersionUID = -37232222805167898L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
//++++++需要把请求转移到另外一个Web App中的某个地址,可以按以下做法+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 获得另外一个Web App的ServletConext对象(currentServletContext.getContext(uripath))
ServletContext context =getServletContext().getContext("http://localhost:8080/apache-tomcat-7.0.62/webapps/examples");
// 调用ServletContext.getRequestDispatcher(String url)方法,forward写在一起可以
// 相对于webapp根目录的绝对路径
context.getRequestDispatcher("/otherwebapp.jsp").forward(req,resp);
// 相对于context,也就是webapp根目录的相对路径
context.getRequestDispatcher("otherwebapp.jsp").forward(req,resp);
} </span>
本文介绍如何在Java Servlet环境中,利用ServletContext对象实现从一个Web应用到另一个Web应用中Servlet的跳转,通过forward方法传递请求和响应。
200

被折叠的 条评论
为什么被折叠?



