从一个servlet跳转到另一个webapp的某个地址处理请求

本文介绍如何在Java Servlet环境中,利用ServletContext对象实现从一个Web应用到另一个Web应用中Servlet的跳转,通过forward方法传递请求和响应。

例如:用户下单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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值