如何将http请求发送到另一个servlet [英] How can I send http request to another servlet

查看:514
本文介绍了如何将http请求发送到另一个servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目文件夹中,我们在 ContextPath / WEB-INF / Classes / *下有2个java文件.class 名称是 App1.class App2.class

In my project folder,We have 2 java files under ContextPath/WEB-INF/Classes/*.class names are App1.class and App2.class

如果我想运行 App1 .class ,我需要在浏览器中触发URL。

If I want to Run App1.class,Just i need to trigger URL in browser.

  http://localhost:8080/Mapping/App1

以同样的方式,如果你想触发 App2 .class ,使用以下链接

in the same way,if you want to trigger App2.class,use the following link

 http://localhost:8080/Mapping/App2

我想从<$ c触发 App2 $ c> App1 ,表示如果在浏览器中触发 App1 并带有相应的URL,则会触发 App2

I want to trigger App2 from App1,means If you trigger App1 with corresponding URL in browser,It will be trigger App2.

我也不想做任何回复。

我该怎么做。

任何人都可以帮助我。

谢谢。

推荐答案


我想从App1触发App2,意味着如果触发App1在浏览器中使用相应的URL,它将触发App2。

I want to trigger App2 from App1,means If you trigger App1 with corresponding URL in browser,It will be trigger App2.

考虑 App1 App2 Mapping web-app中配置为servlet;您可以使用 RequestDispatcher forward()请求 App2 。这将发生在服务器端,即浏览器将收到响应,好像它来自 App1

Considering App1 and App2 are configured as servlets in your Mapping web-app; you can make use of a RequestDispatcher to forward() the request to App2. This would happen server-side i.e. the browser would receive the response as if its coming from App1.

if (isForwardReqd()) {
    RequestDispatcher rd = request.getRequestDispatcher("App2");
    rd.forward(request, response);
}

请注意 App1 在执行 forward()之前一定不能提交响应,否则你会得到 IllegalStateException

Please, note App1 must not have committed a response before doing the forward(), otherwise you'd get an IllegalStateException.

参考

http://docs.oracle.com/javaee/7/api/javax/servlet/RequestDispatcher.html


定义一个对象,该对象接收来自客户端的请求并将它们发送到任何资源(例如servlet,HTML文件或JSP文件)服务器。

Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server.

这篇关于如何将http请求发送到另一个servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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