Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Set headers for HttpServletRequest object?

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Set headers for HttpServletRequest object?

    hey, I've written a web application and I have methods which take in
    a HttpServletRequest object and process the request headers.

    How can I test my methods which take in HttpServletRequest object? I need to set the values of the headers, usually this is done by an external party but when I'm testing I need to manually set attributes and corresponding values. So is there a way to set these headers manually for testing purposes? or can you suggest some way I can write my test cases?

    Thanks


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Set headers for HttpServletRequest object?

    Well in your unit testing you could fire up a jetty webserver and then actually fire off a proper request and let it all work on its own. However you could also create a class that implements the HttpServletRequest interface and pass that object in.

    // Json

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Set headers for HttpServletRequest object?

    thanks, I can write a class to implement that interface but what methods do I then call in order to set the values of the request headers? I can see there's a getHeaders() method of HttpServletRequest but there's no setHeader() method. so how do I get around this?

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Set headers for HttpServletRequest object?

    Try response.setHeader()

    You can't set any headers on the request because once you have access to the request the headers will already have been read by the web server.

    // Json

Similar Threads

  1. Javamail html mail sended as text and headers problem
    By johnymj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 29th, 2010, 09:22 AM
  2. Object.clone()
    By bbr201 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 14th, 2010, 10:55 AM
  3. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  4. Help with object
    By Spangle1187 in forum Collections and Generics
    Replies: 3
    Last Post: April 22nd, 2010, 04:34 PM
  5. Object o = new Object(); need help
    By zeeshanmirza in forum Object Oriented Programming
    Replies: 11
    Last Post: January 6th, 2010, 09:01 PM