Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a cookie using HttpServletRequest?

I've created a RenderingPlugin, for use in WebSphere Portal, which is invoked serverside before sending markup to client. The plugin loops through all cookies and if 'test' is not found, I'd like to set that cookie.

I know this is possible with a HttpServletResponse but the RenderingPlugin doesn't have access to that object. It only has a HttpServletRequest.

Is there another way to do this?

public class Request implements com.ibm.workplace.wcm.api.plugin.RenderingPlugin {

    @Override
    public boolean render(RenderingPluginModel rpm) throws RenderingPluginException {

        boolean found = false;

        HttpServletRequest servletRequest = (HttpServletRequest) rpm.getRequest();
        Cookie[] cookie = servletRequest.getCookies();

        // loop through cookies
        for (int i = 0; i < cookie.length; i++) {

            // if test found
            if (cookie[i].getName().equals("test")) {

                found = true;
            }
        }

        if (!found){

            // set cookie here
        }
    }
}
like image 925
bobbyrne01 Avatar asked Nov 10 '22 14:11

bobbyrne01


1 Answers

Did you try using javascript code to set the cookie ?

<script>
document.cookie = "test=1;path=/";
</script>

you send this as part of the content you give to the Writer rpm.getWriter() and it will be executed by the browser.

like image 121
Ahmed M Avatar answered Nov 15 '22 05:11

Ahmed M