简体   繁体   English

向JSP发送请求并接收响应

[英]Send request to JSP and receive response

I have the following scenario of integrating a third party application into my java application. 我有以下方案将第三方应用程序集成到我的Java应用程序中。 The third party application is running in a different context than my java application. 第三方应用程序在与我的java应用程序不同的上下文中运行。 It provides a JSP which needs to be called with certain parameters such as authentication information based on which it generates a cookie value and sets it in the header. 它提供了一个JSP,需要使用某些参数调用,例如基于它生成cookie值的身份验证信息,并在头中设置它。 I need to call this JSP from my java application and then retrieve the headers from the response with the cookie value and set it to the new cookie that will be created in my application. 我需要从我的java应用程序调用此JSP,然后从响应中检索带有cookie值的标头,并将其设置为将在我的应用程序中创建的新cookie。

I was able to call the JSP using 我能够使用调用JSP

response.sendRedirect("http://<host>:<port>/<context>/authn.jsp").

The authn.jsp was able to retrieve all values sent authenticate and generate the cookie value. authn.jsp能够检索发送身份验证的所有值并生成cookie值。 It then does the 它然后做了

response.setHeader(attr,val).

However, I am not sure how to read this response header back in my servlet. 但是,我不知道如何在我的servlet中读回这个响应头。 Is request.sendRedirect the correct way to do this? request.sendRedirect是否有正确的方法来执行此操作? Do I need to use the HTTPURLConnection class to achieve this? 我是否需要使用HTTPURLConnection类来实现此目的?

You need to use the HTTPURLConnection to read the headers. 您需要使用HTTPURLConnection来读取标头。 You cannot use response.sendRedirect(..) . 您不能使用response.sendRedirect(..) Once you have received the headers, you can set response.setHeader(attr,val) in your code. 收到标题后,可以在代码中设置response.setHeader(attr,val)

URL url = new URL("JSPURL");
URLConnection conn = url.openConnection();
for (int i = 0;; i++) {
  String headerName = conn.getHeaderFieldKey(i);
  String headerValue = conn.getHeaderField(i);
  System.out.println(headerName + "===");
  System.out.println(headerValue);
  if (headerName == null && headerValue == null) {
    break;
  }
}

If your are using Tomcat and both applications are deployed into the same server instance then you could configure both servlet applications to accept incoming requests from each other. 如果您正在使用Tomcat并且两个应用程序都部署到同一服务器实例中,那么您可以配置两个servlet应用程序以接受来自彼此的传入请求。 That's done by means of the crossContext attribute in the context configuration . 这是通过上下文配置中的crossContext属性完成的。

Then, from your servlet you could use a RequestDispatcher against the other web application: 然后,从您的servlet中,您可以对另一个Web应用程序使用RequestDispatcher

RequestDispather dispatcher = request.getRequestDispatcher("/<context>/authn.jsp");
dispatcher.forward(request, response);
// Process the headers
Enumeration<String> headerNames = response.getHeaderNames();
while(headerNames.hasMoreElements()) {
   Collection<String> values = response.getHeaders(headerNames.nextElement());
   // do whatever with the header values.
}

NOTE : Other servlet containers have similar ways of configuring the cross context feature, you should check your container's configuration documentation. 注意 :其他servlet容器具有配置交叉上下文功能的类似方法,您应该检查容器的配置文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM