This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing header in all subsequent HTTP Request

 
Aaki Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2684
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cookies are provided for the purpose of having the client browser add data to each request.

Bill
 
Aaki Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic