0

How to set header in sendRedirect()?

asked 2013-05-01 07:42:08 +0800

ankur gravatar image ankur
6

updated 2013-05-01 08:42:16 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

I am trying to send some headers when redirecting. Sample code is:

*Executions.getCurrent().setResponseHeader("auth", "abcd");
Executions.getCurrent().sendRedirect(Url,"_");*

But on the redirected URL (servlet) I am not able to retrieve "auth". Have also tried:

*Executions.getCurrent().setAttribute("auth", "abcd");
Executions.getCurrent().addResponseHeader("auth", "abcd");*

But still not able to get hold of auth. Could you please suggest some mechanism for the same?

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-06-20 07:35:43 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

You can use cookie or put it in session,

e.g.,

test.zul

<zk>
    <button label="test">
        <attribute name="onClick"><![CDATA[
            import javax.servlet.http.Cookie;
            import javax.servlet.http.HttpServletResponse;
            // use cookie
            Cookie userCookie = new Cookie("auth", "abcd");
            ((HttpServletResponse)Executions.getCurrent().getNativeResponse()).addCookie(userCookie);
            // put it in session
            Sessions.getCurrent().setAttribute("auth", "abcd");
            Executions.sendRedirect("test2.zul");
        ]]></attribute>
    </button>
</zk>

test2.zul

<zk>
    <textbox rows="5" width="350px">
        <attribute name="onCreate"><![CDATA[
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.Cookie;

            String value = "In Cookies - \n";
            Cookie[] cookies = ((HttpServletRequest)Executions.getCurrent().getNativeRequest()).getCookies();
            for (Cookie c : cookies) {
                value += c.getName() + ": " + c.getValue() + "\n";
            }
            self.setValue(value);
        ]]></attribute>
    </textbox>
    <div />
    <textbox rows="5" width="350px">
        <attribute name="onCreate"><![CDATA[
            self.setValue("From Session - auth: " + Sessions.getCurrent().getAttribute("auth"));
        ]]></attribute>
    </textbox>
</zk>

By the way, sendRedirect in ZK is not a real redirect with response code 302 (i.e., you will not see a response with status 302 in browser console) since ZK is an ajax framework and there are lots of limitation of 302-redirect within an ajax call.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-05-01 07:42:08 +0800

Seen: 38 times

Last updated: Jun 20 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More