Java 如何将 http 请求发送到另一个 servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18148932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I send http request to another servlet
提问by Hanumath
In my project folder,We have 2 java files under ContextPath/WEB-INF/Classes/*.class
names are App1.class
and App2.class
在我的项目文件夹中,ContextPath/WEB-INF/Classes/*.class
名称下有 2 个 java 文件App1.class
和App2.class
If I want to Run App1.class
,Just i need to trigger URL in browser.
如果我想运行App1.class
,只需要在浏览器中触发 URL。
http://localhost:8080/Mapping/App1
in the same way,if you want to trigger App2.class
,use the following link
同样的,如果你想触发App2.class
,使用以下链接
http://localhost:8080/Mapping/App2
I want to trigger App2
from App1
,means If you trigger App1
with corresponding URL in browser,It will be trigger App2
.
我想触发App2
从App1
如果触发,方式App1
与在浏览器对应的URL,这将是触发App2
。
I don't want to any response also.
我也不想要任何回应。
How can I do this.
我怎样才能做到这一点。
can anyone help me.
谁能帮我。
Thanks.
谢谢。
回答by Robadob
You could send a Get request using Java;
您可以使用 Java 发送 Get 请求;
URL url = new URL("http://localhost:8080/Mapping/App2");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
Alternatively you should probably configure App2 so that it's action is handled by a separate class or a method accessible to both servlets.
或者,您可能应该配置 App2,以便它的操作由单独的类或两个 servlet 均可访问的方法处理。
回答by Ravi Thapliyal
I want to trigger App2 from App1,means If you trigger App1 with corresponding URL in browser,It will be trigger App2.
我想从App1触发App2,意思是如果你在浏览器中用对应的URL触发App1,它会触发App2。
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
.
考虑App1
并App2
在您的Mapping
web 应用程序中配置为 servlet ;您可以使用 aRequestDispatcher
来forward()
请求App2
。这将发生在服务器端,即浏览器会收到响应,就好像它来自App1
.
if (isForwardReqd()) {
RequestDispatcher rd = request.getRequestDispatcher("App2");
rd.forward(request, response);
}
Please, note App1
must not have committed a response before doing the forward()
, otherwise you'd get an IllegalStateException
.
请注意,App1
在执行 之前一定不要提交响应forward()
,否则您会得到IllegalStateException
.
Reference:
http://docs.oracle.com/javaee/7/api/javax/servlet/RequestDispatcher.html
参考:http :
//docs.oracle.com/javaee/7/api/javax/servlet/RequestDispatcher.html
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.
定义一个对象,该对象接收来自客户端的请求并将它们发送到服务器上的任何资源(例如 servlet、HTML 文件或 JSP 文件)。
回答by Yash
Possible Ways
可能的方法
HTTP GETrequest with (optionally) query parameters
带有(可选)查询参数的HTTP GET请求
String query = String.format("param1=%s¶m2=%s",
URLEncoder.encode("param1Value", "UTF-8"),
URLEncoder.encode("param1Value", "UTF-8"));
URL url = new URL(servletURL + "?" + query);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
Map<String, List<String>> header = conn.getHeaderFields();
int responseCode = conn.getResponseCode();
System.out.println("Headers : "+header);
System.out.println("Response Code "+responseCode);
RequestDispatcher- Dispatch the Request from one resource to other-resource. If they are available in same project & server.
RequestDispatcher- 将请求从一个资源分派到另一个资源。如果它们在同一项目和服务器中可用。
This interface allows you to do a server side forward/include, executes service(...)/doGet(...) method of requested Servlet.
RequestDispatcher rd = req.getRequestDispatcher("/servlet2"); rd.forward(req, resp); // rd.include(req, resp);
Same Server, Different-Project
RequestDispatcher rd = req.getServletContext().getContext("/Project2").getRequestDispatcher("/ips"); rd.forward(req, resp);
该接口允许您执行服务器端转发/包含,执行请求的 Servlet 的 service(...)/doGet(...) 方法。
RequestDispatcher rd = req.getRequestDispatcher("/servlet2"); rd.forward(req, resp); // rd.include(req, resp);
相同的服务器,不同的项目
RequestDispatcher rd = req.getServletContext().getContext("/Project2").getRequestDispatcher("/ips"); rd.forward(req, resp);
.sendRedirect()
.sendRedirect()
- Redirects the request from client side[URL get changed in client's browser].
- When server encounters sendRedirect method it Sends a temporary redirect response to the client with 3XX status code, then requests new URL.
www.sun.com redirects to www.oracle.com/sun/index.html
response.sendRedirect(servletURL); // Different Server.
- 重定向来自客户端的请求 [URL 在客户端浏览器中更改]。
- 当服务器遇到 sendRedirect 方法时,它会向客户端发送一个带有 3XX 状态码的临时重定向响应,然后请求新的 URL。
www.sun.com 重定向到 www.oracle.com/sun/index.html
response.sendRedirect(servletURL); // Different Server.
Invoking Other Servlet
using different ways
Invoking Other Servlet
使用不同的方式