Chin
Chin

Reputation: 613

Get request header in JSP Struts2

I set the header in the action class. The code are as follow:

public String doMyAction() {

    response.setHeader("abc","cba");
    response.addHeader("abcdefg","1234567890");

    return "target_page";

}

At my target_page.jsp, I tried to display the header, but the value is null.

<%= request.getHeader("abc") %>           <-- Null
<%= request.getHeader("abcdefg") %>       <-- Null
<%= response.containsHeader("abc") %>     <-- True
<s:property value="%{#request.abc}" />    <-- Empty
<s:text name="%{request.abc}" />          <-- Empty
<s:text name="%{#header.abc}" />          <-- Empty
<s:text name="%{#header['abc']}" />       <-- Empty

And in the developer tools, there are my headers.

enter image description here

In my struts-config.xml,

<action name="target_name" class="com.my.Class">
    <result name="target_page">/my/jsp/target_page.jsp</result>
</action>

Why I can't get the request header that set in my action class?

EDIT:

How can I get the request header that set in the action class?

Upvotes: 0

Views: 1965

Answers (1)

Thilo
Thilo

Reputation: 262474

The response headers and the request headers are different sets of headers.

You can only set response headers in your server-side code. Request headers are set by the browser.

If you want to pass data from action class to JSP, you could use request attributes.

Upvotes: 2

Related Questions