wood burning stoves*
The moose likes I/O and Streams and the fly likes How to recive / return cookies to server? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to recive / return cookies to server?" Watch "How to recive / return cookies to server?" New topic

How to recive / return cookies to server?

Huw Morgan

Joined: Mar 07, 2006
Posts: 23
This is a continuation of the thread [1]. I am trying to get a java app. [3] comunicating with a web developed with Java Studio Creator. The web site is accesable at [2].

If you access this web page, and click the button or select an item from the dropdown box, the text is changed ("not pressed" initially). If I access this site with a java app, I cannot get the text to change. The behaviour of the app. can be mimicked in the browser by deleting the cookies (and removing the JSESSIONID from the url if pressent) after first visiting the link but before doing an action on the form. I thus assume it is the cookies that are causing the problem.

How do I send cookies from a java app? Any other suggestion how I can solve this problem?

[1] http://www.coderanch.com/t/278161/Streams/java/POST-data-form
[3] private String postRequest(String unit) {

try {
// Construct data
String data = URLEncoder.encode("form1 ropdown1", "UTF-8") + "=" + URLEncoder.encode("+447123456789", "UTF-8");
data += "&" + URLEncoder.encode("form1", "UTF-8") + "=" + URLEncoder.encode("form1", "UTF-8");

System.out.println("data = " + data);
// Send data
// Note it runs this tice, one to get the cookie, then to send it
int tryAgain = 0;
while(tryAgain != 2) {
URL url;
if(cookie == null) {
url = new URL("http://localhost:18080/smsserver/faces/test2.jsp");
} else {
url = new URL("http://localhost:18080/smsserver/faces/test2.jsp;" + cookie.replaceFirst("JSESSIONID=", "jsessionid=").replaceFirst("; Path=/smsserver", ""));
System.out.println("url = " + url);

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

conn.setRequestProperty("Content-Length", "" + data.length());
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");


if(cookie != null) {
conn.setRequestProperty("Cookie", cookie);

//OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
BufferedOutputStream wr = new BufferedOutputStream(conn.getOutputStream());
byte[] dataBytes = data.getBytes();
wr.write(dataBytes, 0, dataBytes.length);
for(int i = 0; i < dataBytes.length; ++i) {
System.out.print((char) dataBytes[i]);
System.out.println("data = |" + data + "|");

System.out.println("getHeaderField = " + conn.getHeaderField("Set-Cookie"));
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
cookie = conn.getHeaderField("Set-Cookie");
while ((line = rd.readLine()) != null) {
// Process line...
} catch (Exception e) {

return "";
Joe Ess

Joined: Oct 29, 2001
Posts: 8908

As our FAQ, How To Ask Questions On JavaRanch states, Use One Thread Per Question.
I'll close your previous thread on this subject.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Huw Morgan

Joined: Mar 07, 2006
Posts: 23
I am sorry about breaking the forum rules. I thought this was significantly different to the previous question to warrant a new thread. Somehow I missed your reply (despite checking the site frequently today, I wounder if I have a cache problem of something more biological) , or I would have at least continued the discusion there.

So you are talking about the JSP page you read in response to your POST not having the value "pressed" in it.
What makes the decision to change "not pressed" to "pressed"? I don't see it in any of the code you've given us. Is it in the JSP or a servlet?

To answer your question from the other thread, this is in the server. If it gets a responce like form1:button1=Submit it changes the text to "pressed" and if it gets form1 ropdown1="not+a+unit" it changes the text to "not+a+unit". [EDIT] As I did not explicitly say, this is done by a java function called by the JSP on the server.
[ August 30, 2006: Message edited by: Huw Morgan ]
Joe Ess

Joined: Oct 29, 2001
Posts: 8908

Can we see the server-side code. The JSP and servlet (if there is one).
Huw Morgan

Joined: Mar 07, 2006
Posts: 23
The relavent bits of the servlet are at [1] and the .jsp is at [2]. I could post the whole .java file but the rest is all auto generated by Java Studio Creator.

[2] <?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="1.2" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:jsp="http://java.sun.com/JSP/Page">
<jsp irective.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html lang="en-GB" xml:lang="en-GB">
<meta content="no-cache" http-equiv="Cache-Control"/>
<meta content="no-cache" http-equiv="Pragma"/>
<title>test2 Title</title>
<link href="resources/stylesheet.css" rel="stylesheet" type="text/css"/>
<body style="-rave-layout: grid">
<h:form binding="#{test2.form1}" id="form1">
<h utputText binding="#{test2.outputText1}" id="outputText1" style="left: 168px; top: 72px; position: absolute" value="not pressed"/>
<h:commandButton action="#{test2.button1_action}" binding="#{test2.button1}" id="button1" style="left: 336px; top: 72px; position: absolute" value="Submit"/>
<h:selectOneMenu binding="#{test2.dropdown1}" id="dropdown1" immediate="true" onchange="this.form.submit();"
style="left: 192px; top: 144px; position: absolute" valueChangeListener="#{test2.dropdown1_processValueChange}">
<f:selectItems binding="#{test2.dropdown1SelectItems}" id="dropdown1SelectItems" value="#{test2.unitRowSet.selectItems['number,number']}"/>
[ August 30, 2006: Message edited by: Huw Morgan ]
Joe Ess

Joined: Oct 29, 2001
Posts: 8908

There's your problem:

You don't have a form input named outputText1. So yea, it is probably set in a session somewhere. Here is a how-to on using cookies. If I were you, I'd just use a hidden form field to store the state.
I recommend against people using IDE's to generate code. All it does is produce a lot of stuff you don't understand and make unraveling that mess all the more complex.
Huw Morgan

Joined: Mar 07, 2006
Posts: 23
outputText1 is a variable in the java servlet that is called by the JSP. [EDIT] After rereading your comment, I think I should be more clear. outputText1 is the value of the one bit of plain text on the page. This starts as "not pressed" and is changed by submition of the form.

I would not use an IDE if I was to start from scratch, it is legacy issues.

I shall have a look at the How to, thanks ([EDIT] though www.hccp.org seems to be down ATM, but I am off to the pub anyway).

[ August 30, 2006: Message edited by: Huw Morgan ]
[ August 30, 2006: Message edited by: Huw Morgan ]
It is sorta covered in the JavaRanch Style Guide.
subject: How to recive / return cookies to server?