[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1308
  • Last Modified:

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
0
murali
Asked:
murali
  • 4
  • 2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now