I'm trying to import an external resource in a jsp and so I'm using:
<c:import url="http://foo.co.uk/articles?开发者_开发问答id=${article.id}" />
Is it possible to pass the cookies received on the current request to the imported resource?
No, you can't.
You will have to do that in a servlet, using new URL(..).openConnection()
for example.
c:import tag is incapable to pass cookies to the external resource. You can do a work around by exactly mocking the c:import tag functionality with additional capability to pass cookies to the imported resource as below.
// handle absolute URLs ourselves, using java.net.URL URL u = new URL(target);
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
Cookie cookies [] = request.getCookies();
Cookie myCookie = null;
String WCSCookie="";
if (cookies != null)
{
System.out.println("Cookies List start------------");
for (int i = 0; i < cookies.length; i++)
{
myCookie = cookies[i];
System.out.println(myCookie.getName()+"="+myCookie.getValue());
WCSCookie = WCSCookie+";"+myCookie.getName()+"="+myCookie.getValue();
}
}
System.out.println("-------------------------");
System.out.println(WCSCookie);
System.out.println("-------------------------");
System.out.println("Cookies List end------------");
URLConnection uc = u.openConnection();
uc.setRequestProperty("Cookie", WCSCookie);
InputStream i = uc.getInputStream();
精彩评论