0

Is there any way to modify or set-header of request inside action class? I want to modify it or you can say i want to put flag inside request Header just like we put values in 'attribute' and parameters.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
muneebShabbir
  • 2,500
  • 4
  • 29
  • 46

7 Answers7

3

You can do this using HttpServletRequestWrapper. But it's quite dirty solution. Are there really no other ways to solve your problem?

Artem
  • 4,347
  • 2
  • 22
  • 22
1

You cannot. Request parameters returned from the servlet are unmodifiable Map. You cannot add/delete content returned from request (via servlet).

In order to set a flag, my suggestion is to store it in a session, and on another action, retrieve the flag & delete it from session.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • it means i can only get data from request. one way data movement – muneebShabbir Jan 03 '12 at 07:31
  • Yes, since a request is data sent from client to server. Think of it as client sending you genuine info. If we had opportunity to manipulate header requests, then anybody can spoof it through code. Wait, it is possible, but for Servlet, the container shouldn't allow this. – Buhake Sindi Jan 03 '12 at 07:44
  • what you mean by container? i m working in struts-1 and attempting to modify the request in action class – muneebShabbir Jan 03 '12 at 07:48
  • You are running on Struts action in a servlet container (Tomcat), that's what I meant. – Buhake Sindi Jan 03 '12 at 07:52
1

I think you'd need to wrap the original request into a request class containing the change you wish to have.

It might be better design to have the request parameters parsed earlier in processing to such objects that make more sense to your application logic, and then set the state of those objects in the place where you now would like to modify the original header.

Juha Laiho
  • 596
  • 2
  • 8
1

If you are trying to open a URL connection using Java, you can something like this What is the proper way of setting headers in a URLConnection?

If you can make requests with a browser, You can use this Firefox plugin to add/modify any number of request headers.

https://addons.mozilla.org/en-US/firefox/addon/modify-headers/

Good Luck

Community
  • 1
  • 1
Dipin
  • 1,085
  • 6
  • 19
1

The answer to this depends on what problem you're trying to solve.

One of your comments suggests you're trying to test; if this is the case you have two basic options:

  1. Use a mock request (unit-style testing).
  2. Change the header from the client (integration-style testing).

For testing from real clients, set headers on the client side.

For mocking client interactions, you should be using some form of mock. StrutsTestCase, for example, provides MockStrutsTestCase (outside container) and CactusStrutsTestCase (inside container) classes allowing easy manipulation of request properties and characteristics.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

You need to give more details. It sounds like you want to manipulate the request header once the server has received the request. I'm not sure I understand why you would want to do that. Modifying the response headers make sense. But not the request.

drekka
  • 20,957
  • 14
  • 79
  • 135
  • actually i want to simulate the flow. the header is set by third party module. but on local i want to test the reflection of that that is why i was trying to set Header of request – muneebShabbir Jan 03 '12 at 07:34
0

I think they only clean way you can do this is via an HttpServletRequestWrapper

Just override getHeader, getHeaders, getHeaderNames and you are good to go.

MahdeTo
  • 11,034
  • 2
  • 27
  • 28