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(ProcessNewRegistra tion.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.getParamet er("userNa me");
String sn = (String)request.getParamet er("fName" );
String ln = (String)request.getParamet er("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>ProcessNewRe gistration </servlet- name>
<servlet-class>com.nu.insu rance.web. ProcessNew Registrati on</servle t-class>
</servlet>
<servlet>
<servlet-name>Getheaders</ servlet-na me>
<servlet-class>com.nu.insu rance.web. Getheaders </servlet- class>
</servlet>
<servlet-mapping>
<servlet-name>ProcessNewRe gistration </servlet- name>
<url-pattern>/processReque st/*</url- pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Getheaders</ servlet-na me>
<url-pattern>/MyApp/Gethea ders</url- pattern>
</servlet-mapping>
</web-app>
Please guide me how to put the custom headers using servlets.
Thanks,
Murali
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(ProcessNewRegistra
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String givenName = (String)request.getParamet
String sn = (String)request.getParamet
String ln = (String)request.getParamet
response.setHeader("RACFID
PrintWriter out = response.getWriter();
out.println("<form action=./MyApp/getHeaders.
out.println("<BR><input type='submit' name='postData' value='postData'");
}
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>ProcessNewRe
<servlet-class>com.nu.insu
</servlet>
<servlet>
<servlet-name>Getheaders</
<servlet-class>com.nu.insu
</servlet>
<servlet-mapping>
<servlet-name>ProcessNewRe
<url-pattern>/processReque
</servlet-mapping>
<servlet-mapping>
<servlet-name>Getheaders</
<url-pattern>/MyApp/Gethea
</servlet-mapping>
</web-app>
Please guide me how to put the custom headers using servlets.
Thanks,
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.jav a servlet.
I hope this would clear my requirement.
Thanks,
Murali
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.jav
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
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
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
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!!.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The line
out.println("<form action=./MyApp/getHeaders.
should be
out.println("<form action=./MyApp/getHeaders'
Regards,
Tomas Helgi