Reputation: 69
I have the following scenario of integrating a third party application into my java application. The third party application is running in a different context than my java application. 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. 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.
I was able to call the JSP using
response.sendRedirect("http://<host>:<port>/<context>/authn.jsp").
The authn.jsp was able to retrieve all values sent authenticate and generate the cookie value. It then does the
response.setHeader(attr,val).
However, I am not sure how to read this response header back in my servlet. Is request.sendRedirect the correct way to do this? Do I need to use the HTTPURLConnection class to achieve this?
Upvotes: 1
Views: 4866
Reputation: 7858
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. That's done by means of the crossContext
attribute in the context configuration.
Then, from your servlet you could use a RequestDispatcher
against the other web application:
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.
Upvotes: 1
Reputation: 4829
You need to use the HTTPURLConnection
to read the headers. You cannot use response.sendRedirect(..)
. Once you have received the headers, you can set response.setHeader(attr,val)
in your code.
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;
}
}
Upvotes: 2