RequestDispatcher是否在一个servlet容器中处理多个webapps? [英] Does RequestDispatcher work over multiple webapps in one servlet container?

查看:123
本文介绍了RequestDispatcher是否在一个servlet容器中处理多个webapps?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RequestDispatcher是否适用于多个Web应用程序?



我问,因为我有一个使用RequestDispatcher而不是重定向的webapp工作正常,因此显示错误时状态不会丢失反馈消息。



但是我现在需要在两个webapps之间拆分一些功能,所以初始调用来自webapp1上托管的网页,调用webapp2最终将用户返回给webapp1上托管的页面。



很明显,如果webapps和webapp2在不同的网站上使用RequestDispatcher是不可能的,但如果两个webapps都部署在同一个实例中servlet容器(tomcat 7)



更新



请求调度程序部分到按照回答的说明工作,但无法检索我的webapp2中的数据,这就是我使用它的原因



ie



webapp2调用,进行一些处理,然后调度到webapp1上的jsp

  protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{

HttpSession userSession = request.getSession(true);
String emailAddress = ......
String nextPage = /finish.jsp
userSession.setAttribute(DATA,emailAddress);
ServletContext otherContext = getServletContext()。getContext(/ webapp1);
otherContext.getRequestDispatcher(nextPage).forward(request,response);
}

webapp2 jsp文件包含

  ... 
< p>电子邮件(<%=((String)session.getAttribute(DATA))%>)< / p> ;
...

但始终显示为空



更新2 **



我想知道我是否真的误解了crossContext =true究竟是什么没有。它是否可以在不同的webapps中使用相同的HttpSession,或者只是将ServletContext从一个webap可用到另一个webap,从而允许一个webapp看到另一个webapp的HttpSessions?



<我开始认为我在做什么是一个坏主意,因为我一直热衷于使用vanilla servlet设置,并且从不想将自己绑定到特定的实现。我认为如果我解释为什么我首先需要拆分webapps,这可能会有所帮助。



我有一个webapp(webapp1),这是一个网站关于我开发的产品以及使用Google Checkout(现在是Google电子钱包)购买该产品的代码。



然后我添加了为新产品(webapp2)创建了一个新的webapp。



然后我尝试将新产品的Google Checkout添加到webapp2中,但我意识到我无法轻易做到这一点,因为Google Checkout要求我提供一个可以调用它的网址申请一旦处理完付款,我就可以向
用户发送许可证。网址已经设置为webapp1中的servlet,但是
webapp1处理产品2的付款是没有意义的。



一个选项是将webpp1和webapp2合并到一个webapp中,但这违背了我保持模块化的一般观点,它bb b也意味着我想要为一个
产品制作产品的时间我必须重新部署所有内容。它还意味着对webapp1进行了大量的
修改,我真的不想修改它,因为它是
工作和稳定。



另一种方法是创建webapp3然后谷歌网址可以指向这个,并使用它来处理产品1
和产品2的购买,这是我的意思完成。但问题是当
购买产品1的起始页面是在webapp1中,并且一旦
购买已经发生,我想回到webapp1中的页面,但是
只有webapp3有详细信息我刚想要在webapp1页面上显示
购买的用户。

解决方案

这是出于安全原因,默认情况下不可能。您需要首先配置Tomcat以启用将当前Web应用程序的 ServletContext 公开给其他Web应用程序。这可以通过设置 crossContext context.xml的属性 true

 < Context ... crossContext =true> 

完成后,您可以使用 ServletContext#getContext() 通过其上下文路径获取其他servlet上下文,最后使用 RequestDispatcher javaee / 6 / api / javax / servlet / ServletContext.html#getRequestDispatcher%28java.lang.String%29rel =nofollow> ServletContext#getRequestDispatcher() 通常的方式。



例如

  ServletContext otherContext = getServletContext ().getContext( / otherContext); 
otherContext.getRequestDispatcher(/ WEB-INF / some.jsp)。forward(request,response);


Does RequestDispatcher work over multiple webapps ?

I'm asking because I had a single webapp working fine that uses RequestDispatcher rather than redirects so state isnt lost when displaying error and feedback messages.

However I now need to split some functionality between two webapps, so initial call is made from a webpage hosted on webapp1, calls webapp2 which eventually returns user to a page hosted on webapp1.

Clearly if webapps and webapp2 were on different websites using RequestDispatcher would not be possible but is it if both webapps are deployed within the same instance of a servlet container (tomcat 7)

Update

Got the request dispatcher part to work as explained in answer but am unable to retrieve data put in my webapp2 which iss why Im using it

i.e

webapp2 called , does some processing and then dispatches to a jsp on webapp1

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
{

    HttpSession userSession = request.getSession(true);
    String emailAddress = ......
    String nextPage     = /finish.jsp
    userSession.setAttribute("DATA", emailAddress);
    ServletContext otherContext = getServletContext().getContext("/webapp1");
    otherContext.getRequestDispatcher(nextPage).forward(request, response); 
}

webapp2 jsp file contains

...
<p>Email(<%=((String)session.getAttribute("DATA"))%>)</p>
...

but always displays null

Update 2 **

Im wondering if Im misunderstanding what crossContext="true" actually does . Does it make the same HttpSession availble in different webapps, or does it just make the ServletContext from one webap availble to another and hence allow one webapp to see the HttpSessions of another webapp ?

Im starting to think what Im doing is a bad idea as Ive always been keen to use vanilla servlet setups and never want to tie myself to a particular implementation. I think it might help if I explain why I flet the need to split the webapps in the first place.

I had a one webapp (webapp1), that was a website about a product I develop and code for purchasing that product using Google Checkout (now Google Wallet).

I then added created a new webapp for a new product (webapp2).

I then tried to add Google Checkout for new product into webapp2, but realised I couldnt do this easily because Google Checkout requires me to provide it with a url which it can call by application once it has processed payment so that I can then send user a license. The url was already set to a servlet in webapp1, but it wouldn't make sense for webapp1 to process payment s for product 2.

One option was to merge webpp1 and webapp2 into one webapp, but this goes against my general view of keeping things modular, it would also mean evey time I would want to make chnages for one product Id have to redeploy everything. It also meant big modifications to webapp1 which I really didnt want to modify as it was working and stable.

The alternative was to create webapp3 and then google url can point to this, and use this for processing purchases of product 1 and product 2 which is what Ive done. But the problem is when purchasing product 1 the starting page is in webapp1 , and once purchase has taken place I want to go back to a page in webapp1, but only webapp3 has the details of the user who has just made the purchase which I wanted to display on on the page in webapp1.

解决方案

This is due to security reasons by default not possible. You need to configure Tomcat first to enable exposing the ServletContext of the current webapp to other webapps. This is to be done by setting the crossContext attribute of context.xml to true.

<Context ... crossContext="true">

Once done that, then you can use ServletContext#getContext() to obtain the other servlet context by its context path and finally use the RequestDispatcher as obtained by ServletContext#getRequestDispatcher() the usual way.

E.g.

ServletContext otherContext = getServletContext().getContext("/otherContext");
otherContext.getRequestDispatcher("/WEB-INF/some.jsp").forward(request, response);

这篇关于RequestDispatcher是否在一个servlet容器中处理多个webapps?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆