This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Passing header in all subsequent HTTP Request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing header in all subsequent HTTP Request" Watch "Passing header in all subsequent HTTP Request" New topic
Author

Passing header in all subsequent HTTP Request

Aaki Jain
Greenhorn

Joined: Feb 29, 2012
Posts: 3
Hi All,

I have basic question regarding Java web application

In my application i created a servlet (which is hit directly from browser to start the application) . In this servlet , I set header called "MyName"

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

URL url = new URL("http://localhost:7001/JavaWebApplicaionTest/jsp/TestSession.jsp");


HttpURLConnection postUrlConnection = (HttpURLConnection)url.openConnection();

postUrlConnection.setDoOutput(true);
postUrlConnection.setUseCaches(false);
postUrlConnection.addRequestProperty("MyName",
"Tester");

postUrlConnection.getResponseCode();


Writer w= response.getWriter();


InputStream postInputStream=postUrlConnection.getInputStream();

BufferedReader postBufferedReader = new BufferedReader(new
InputStreamReader(postInputStream));
String postline = null;
while((postline = postBufferedReader.readLine()) != null) {
w.write(postline);
}

response.flushBuffer();
}


As it takes us to TestSession.jsp , which contains


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>
</head>
<body>
<form action="/JavaWebApplicaionTest/SessionTestServlet">
<input type="submit" value='<%=request.getHeader("MyName")%>' ></input>
</form>

</body>
</html>

After I click on submit I forward it to a servlet called SessionTestServlet

In SessionTestServlet I have

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getSession(false));
System.out.println(request.getHeaderNames());
}


Now when I log in to my initial servlet where I have been adding my "MyName" header , it sets header fine and my jsp which uses this header uses it correct ( as the value to the button and prints "Tester") .

But when I click on the button and the control goes to SessionTestServlet ( as it is submit button) , this header is lost.

Is there a way , in which i can set up a header in a initial servlet and it can be passed in all the subsequent request to the server ? As in my case "MyName" header is passed from my jsp to the next servlet .

Any leads would be very helpful.

Thanks
Aaki


Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Headers are set for individual requests or responses; there is no way to keep them between requests (unless you have a special client that does so - web browser don't).
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
Cookies are provided for the purpose of having the client browser add data to each request.

Bill
Aaki Jain
Greenhorn

Joined: Feb 29, 2012
Posts: 3
Thanks Tim and Bill,

So isn't there any way in JSP ( i mean using some tags or other approach ) in which I can ask browser to send some header to servlet .

I mean as we know putting <META HTTP-EQUIV tag adds the following name to response header to be read by client . Is there any such tag which i can put so that client know that it has to send it to the servlet as header.

Thanks
Aaki
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing header in all subsequent HTTP Request
 
Similar Threads
what is request.getInputStream() useful for?
Wierd problem
retrieve value at servlet from javascript.
unable to pass data in same package
Wierd problem