开发者

How do you pass cookies to a resource when using c:import?

开发者 https://www.devze.com 2023-03-12 18:54 出处:网络
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}\" />

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消