Custom request Headers using java servlet

As part of my assignment I need to create the custom headers using java servlet.Below is the scenario.
I have a JSP page(Registration.jsp) which consists of some fields. once the user submitted the registration form, the request would be submitted to a servlet(ProcessNewRegistration.java) in which I am getting the form data submitted by the user. In this servlet I need to put the data in request header.I am trying to do this using response.setHeader("RACFID", "givenName "). After this I am trying to access the request headers on other servlet. Below is the code snippet.

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {            
            String givenName = (String)request.getParameter("userName");
            String sn = (String)request.getParameter("fName");
            String ln = (String)request.getParameter("lName");      
            response.setHeader("RACFID", "givenName ");      
                                           PrintWriter out = response.getWriter();
                                           out.println("<form action=./MyApp/getHeaders.java' method='post'>");
            out.println("<BR><input type='submit' name='postData' value='postData'");
            }
//web.xml
<?xml version="1.0" encoding="UTF-8"?>  
<web-app>
<servlet>
    <servlet-name>ProcessNewRegistration</servlet-name>
    <servlet-class>com.nu.insurance.web.ProcessNewRegistration</servlet-class>
  </servlet>             
  <servlet>
    <servlet-name>Getheaders</servlet-name>
    <servlet-class>com.nu.insurance.web.Getheaders</servlet-class>
  </servlet>        
         
 <servlet-mapping>
    <servlet-name>ProcessNewRegistration</servlet-name>
    <url-pattern>/processRequest/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Getheaders</servlet-name>
    <url-pattern>/MyApp/Getheaders</url-pattern>
  </servlet-mapping>        
 </web-app>

Please guide me how to put the custom headers using servlets.

Thanks,
Murali
muraliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tomas Helgi JohannssonCommented:
    Hi!
The line
  out.println("<form action=./MyApp/getHeaders.java' method='post'>");
should be
    out.println("<form action=./MyApp/getHeaders' method='post'>");


Regards,
   Tomas Helgi
0
muraliAuthor Commented:
Hi TomasHelqi,  Thanks for the reply.
After submitting the form to "./MyApp/getHeader", the rquest is entering to "getHeaders.java", which is configured in web.xml.
On the getHeaders.java I am retrieving the request headers,but I am not getting the header that added in the ProcessNewRegistration.java servlet.

I hope this would clear my requirement.

Thanks,
Murali


0
Tomas Helgi JohannssonCommented:
      Hi!

It's better to put it into the Session rather than the request itself as the Session lives while the users browser is open and connected or
lives in a sertain amount of time.
Requests are one time event and the data is not retained between requests. To do so you will need to put it into the Session.

http://www.java2s.com/Code/Java/Servlets/Session.htm
http://www.java2s.com/Code/Java/JSP/Session.htm

Regards,
    Tomas Helgi
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

muraliAuthor Commented:
Thanks for reply.

Even for one request, I am not able to get the custom headers in servlet "getHeaders.java".

The exact requirement for me is: I need to send the data (which is collected from customer ) through the requestheaders from JSP to ITDI application(IBM tivoli product).  My JSP's are on TOMCAT server. I should send the user data to another box where ITDI is installed. Please advise me how can I achieve this.

Before doing this I am testing the functionality of custom headers using servlet, JSP. Correct me If I am doing some thing wrong.

Thanks & Regards,
Murali
untitled.bmp
0
Tomas Helgi JohannssonCommented:

Here is an example on how to get the header
http://www.java2s.com/Code/Java/JSP/GettingHeaderDataJsp.htm
Examples on how to pass parameters between pages are here
http://www.java2s.com/Code/Java/JSP/JSPPassingParameters.htm
http://www.java2s.com/Code/Java/JSP/Passingparameters.htm

Hope this helps.

Regards,
   Tomas Helgi
0
smarthawkCommented:
Hi Tomas,

Please don't take it otherwise, but I also have been looking for answer to the same question. I believe murali has quite clearly specified his question. You are simply trying to tell workarounds to the issue but not the solution to the question.

The question is quite simple: How to add a custom header to HTTP request so that at recieving end we are able to get the custom header value from the request object. Please reply if you know how to do it, we already know what the workarounds are!!.
0
Tomas Helgi JohannssonCommented:
    Hi!

You could also create an Filter and HttpRequest wrapper and handle it that way
The following pages will give you the idea on how you could solve this
http://www.theserverside.com/discussions/thread.tss?thread_id=41118
http://www.java2s.com/Code/Java/Servlets/RequestFilter.htm

More on filters
http://www.java2s.com/Code/Java/Servlets/Filter.htm

Regards,
   Tomas Helgi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.