Link to home
Start Free TrialLog in
Avatar of murali
murali

asked on

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
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

    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
Avatar of murali
murali

ASKER

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


      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
Avatar of murali

ASKER

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

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
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!!.
ASKER CERTIFIED SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial