For Programmers: Free Programming Magazines  


Home > Archive > Java Help > January 2006 > How could this possibly happen (HttpURLConnection + simple JSP page)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How could this possibly happen (HttpURLConnection + simple JSP page)
phillip.s.powell@gmail.com

2006-01-23, 3:59 am

I have a simple Java class, MessageBinHashBundler, that will need to
retrieve a nickname stored as a cookie value and place it into a
display. This class does not include "javax.servlet.http.Cookie"
because I have no ability to compile it otherwise as my PC can only
take the most simplistic JVM installation on earth. Thus, instead of
using the Cookie object, I have to use a class I wrote called
CookieRetriever that does a URL scrape to obtain a cookie - it does
this by scraping a URL that contains simple HTML tags and the cookie
value, nothing more.

Here is the JSP script, "cookie_grabber.jsp":

code:
<%@ page import="ppowell.*, javax.servlet.http.Cookie" %> <% // COMMENT TITLE GOES HERE String cookie = "", cookieName = ""; cookieName = request.getParameter("name") != null ? request.getParameter("name") : ChatGlobals.CHAT_COOKIE_NAME; Cookie[] cookieArray = request.getCookies(); for (int i = 0; i < cookieArray.length; i++) { if (cookieArray[i].getName().trim().toLowerCase().equals(cookieName.trim().toLowerCase()) && cookieArray[i].getValue() != null ) { cookie = cookieArray[i].getValue(); break; } } %> <html> <head> </head> <body> <%= cookie %> </body> </html>


I verified that this work as I see my cookie value plainly each time I
pull up the URL in my browsers (IE 5.1, Netscape 7.0 and Firefox 1.0).

Here is the code to CookieRetriever.getCookieVal():

code:
/** * To use this method you will have to be sure to have your URL pass the name * of the cookie and not necessarily the cookie object itself * * @access public * @return String name of cookie */ public String getCookieVal() { // STRING METHOD String stuff = null, cookieText = ""; try { URL url = new URL(this.url + "?name=" + this.cookieName); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setDoInput(true); conn.setDoOutput(false); conn.setUseCaches(false); conn.setDefaultUseCaches(false); conn.setRequestProperty("Content-type", "text/html"); conn.setRequestProperty("Connection", "close"); conn.connect(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((stuff = in.readLine()) != null) cookieText += stuff; in.close(); conn.disconnect(); } catch (Exception e) { System.out.println("Could not connect to " + this.url + "?name=" + this.cookieName); e.printStackTrace(); } finally { cookieText = cookieText.replaceAll("[\\n\\r]+", "").replaceAll("<[^>]+>", ""); } return cookieText.trim(); }


This time I bypassed within cookie_grabber.jsp the "query string
problem" that plagued me before, and with or without query string, if I
pull up "cookie_grabber.jsp" in my local browsers (all three of them),
I see my cookie value. However, if I use
CookieRetriever.getCookieVal(), every time I see NOTHING.

Could someone look at the code and tell me what's going wrong, I don't
get it from here.

Thanx
Phil

NullBock

2006-01-23, 3:59 am

So let me get this straight--you're rebuilding the cookie structures
from scratch? You want to include your cookies in the URL? Why can't
you simply use request.getHeader("cookie"), and parse them out by hand?
It's still reinventing the wheel, but less than what you're proposing.

Walter Gildersleeve
Freiburg, Germany

________________________________________
______________
http://linkfrog.net
URL Shortening
Free and easy, small and green.

phillip.s.powell@gmail.com

2006-01-23, 3:59 am

I'm afraid I can't follow anything you said. I know of
request.getHeader("cookie") but don't understand its application in
light of my problem, sorry.

Phil

NullBock wrote:
> So let me get this straight--you're rebuilding the cookie structures
> from scratch? You want to include your cookies in the URL? Why can't
> you simply use request.getHeader("cookie"), and parse them out by hand?
> It's still reinventing the wheel, but less than what you're proposing.
>
> Walter Gildersleeve
> Freiburg, Germany
>
> ________________________________________
______________
> http://linkfrog.net
> URL Shortening
> Free and easy, small and green.


NullBock

2006-01-23, 3:59 am

Oh, I'm sorry. The fact that you talked about "cookies" and mentioned
the javax.servlet.http.Cookie class made me think you wanted to use the
HTTP cookie structure.

My mistake. Won't happen again.

Walter Gildersleeve
Freiburg, Germany

________________________________________
______________
http://linkfrog.net
URL Shortening
Free and easy, small and green.

Oliver Wong

2006-01-23, 7:05 pm


<phillip.s.powell@gmail.com> wrote in message
news:1138006231.342651.158910@z14g2000cwz.googlegroups.com...
>I have a simple Java class, MessageBinHashBundler, that will need to
> retrieve a nickname stored as a cookie value and place it into a
> display. This class does not include "javax.servlet.http.Cookie"
> because I have no ability to compile it otherwise as my PC can only
> take the most simplistic JVM installation on earth. Thus, instead of
> using the Cookie object, I have to use a class I wrote called
> CookieRetriever that does a URL scrape to obtain a cookie - it does
> this by scraping a URL that contains simple HTML tags and the cookie
> value, nothing more.
>
> Here is the JSP script, "cookie_grabber.jsp":
>
>
code:
> <%@ page import="ppowell.*, javax.servlet.http.Cookie" %> > > <% > // COMMENT TITLE GOES HERE > > String cookie = "", cookieName = ""; > cookieName = request.getParameter("name") != null ? > request.getParameter("name") : ChatGlobals.CHAT_COOKIE_NAME; > Cookie[] cookieArray = request.getCookies(); > for (int i = 0; i < cookieArray.length; i++) { > if > (cookieArray[i].getName().trim().toLowerCase().equals(cookieName.trim().toLowerCase()) > && > cookieArray[i].getValue() != null > ) { > cookie = cookieArray[i].getValue(); > break; > } > } > > %> > <html> > <head> > </head> > <body> > <%= cookie %> > </body> > </html> >

>
> I verified that this work as I see my cookie value plainly each time I
> pull up the URL in my browsers (IE 5.1, Netscape 7.0 and Firefox 1.0).
>
> Here is the code to CookieRetriever.getCookieVal():
>
>
code:
> /** > * To use this method you will have to be sure to have your URL pass > the name > * of the cookie and not necessarily the cookie object itself > * > * @access public > * @return String name of cookie > */ > public String getCookieVal() { // STRING METHOD > String stuff = null, cookieText = ""; > try { > URL url = new URL(this.url + "?name=" + this.cookieName); > HttpURLConnection conn = (HttpURLConnection)url.openConnection(); > conn.setDoInput(true); > conn.setDoOutput(false); > conn.setUseCaches(false); > conn.setDefaultUseCaches(false); > conn.setRequestProperty("Content-type", "text/html"); > conn.setRequestProperty("Connection", "close"); > conn.connect(); > BufferedReader in = new BufferedReader(new > InputStreamReader(conn.getInputStream())); > while ((stuff = in.readLine()) != null) cookieText += stuff; > in.close(); > conn.disconnect(); > } catch (Exception e) { > System.out.println("Could not connect to " + this.url + "?name=" + > this.cookieName); > e.printStackTrace(); > } finally { > cookieText = cookieText.replaceAll("[\\n\\r]+", > "").replaceAll("<[^>]+>", ""); > } > return cookieText.trim(); > } > >

>
> This time I bypassed within cookie_grabber.jsp the "query string
> problem" that plagued me before, and with or without query string, if I
> pull up "cookie_grabber.jsp" in my local browsers (all three of them),
> I see my cookie value. However, if I use
> CookieRetriever.getCookieVal(), every time I see NOTHING.
>
> Could someone look at the code and tell me what's going wrong, I don't
> get it from here.


You have some sort of relationship between GET urls, and cookies which I
don't understand (e.g. you shouldn't normally be able to "URL scrape" a
value stored in a cookie, but it looks like you have a special JSP page so
that this becomes possible).

Have you tried removing the call to replaceAll() and trim() and seeing
what raw data you are getting from your HTTP connection?

- Oliver


phillip.s.powell@gmail.com

2006-01-24, 7:05 pm

"Server creates a new request to page B and (optionally specifies a
cookie name to display the value of in the page). Server issues
request.

Server gets the resulting page and attempts to parse it to get the
value
it wants.

...The problem you have is that the two requests are unrelated, except
the fact that the second is fired in response to the first. As they are
unrelated the cookie itself is not sent to the server in the request
for
B, and so it's value cannot be used. "

Let me give you my background, not in Java, but in PHP:

I go to page A
I set a cookie using setcookie('cookieName", "cookie value");

I go to page B
I can now retrieve cookie via $_COOKIE['cookieName']

I go to page whatever else
I can STILL retrieve cookie via $_COOKIE['cookieName']

I can go anywhere I like on that same domain as long as I have not
deleted cookie manually prior to going to that page or not close the
browser if session cookie
...and I can STILL retrieve cookie via $_COOKIE['cookieName']

So I guess your scenarios are Gr to me. It's not a process that is
normal to C-derivative language web development using web scripting
languages like PHP. So what you're asking me to do, along with
describing what I apparently have done, make absolutely no sense to me.

What I do know is that my class, MessageBinHashBundler, needs to get
the cookie value, and it can't because it doesn't import
javax.servlet.http.* and I cannot allow for it to import
javax.servlet.http.* because then I will have no ability to compile
that class on my PC at home. Thus, I have to retrieve the cookie the
only way I know how: scrape a page on that domain that can retrieve it
and display it via HTML so that MessageBinHashBundler's CookieRetriever
class object (which also cannot import javax.servlet.http.* for the
very same reasons) can scrape it, extract the cookie value and pass it
onto the rest of MessageBinHashBundler's convert() method as required
for message display.

Phil

phillip.s.powell@gmail.com

2006-01-24, 7:05 pm

It's so obvious I of course missed it. I'll try that tonight and let
you know, thanx!

Phil

Oliver Wong wrote:
> <phillip.s.powell@gmail.com> wrote in message
> news:1138006231.342651.158910@z14g2000cwz.googlegroups.com...
>
> You have some sort of relationship between GET urls, and cookies which I
> don't understand (e.g. you shouldn't normally be able to "URL scrape" a
> value stored in a cookie, but it looks like you have a special JSP page so
> that this becomes possible).
>
> Have you tried removing the call to replaceAll() and trim() and seeing
> what raw data you are getting from your HTTP connection?
>
> - Oliver


phillip.s.powell@gmail.com

2006-01-24, 7:06 pm

Looks a lot simpler than what I came up with, but I'm notorious for
coming up with the most complex solution to things and having to have
others make it simpler for me as I never think of it myself *sigh*

However, I do have one question: Why do you have response.setHeader()
embedded within the HTML code? In PHP that would throw some nasty
HTTP-related warnings and potentially break the page as you are
changing the headers after HTML display?

Phil

James Westby wrote:
> phillip.s.powell@gmail.com wrote:
> [snip]
>
> How's this code. One import, no second request, no "scraping", no
> javax.servlet.http.Cookie, no messing.
>
> <HTML><HEAD><TITLE>TEST</TITLE></HEAD><BODY>
>
> <%@ page import="java.util.Enumeration" %>
>
> Cookies: <% Enumeration e = request.getHeaders("Cookie");
> while (e.hasMoreElements()) {
> String cookie = (String)e.nextElement();
> int index = cookie.indexOf("=");
> if (index > -1) {
> out.println("Name:
> "+cookie.substring(0,index)+"<br>");
> out.println("Value:
> "+cookie.substring(index+1)+"<br><br
> } else {
> out.println("Error<br>");
> }
> }
> %>
>
> <% response.setHeader("Set-Cookie", "name=Bob"); %>
>
> </BODY></HTML>
>
> The first load of the page will show just Cookie:
>
> The second should show the name value pairs of the cookie that was set
> the first time.
>
> Considerably less code than you were trying to write, and no headaches.
>
>
> James


Oliver Wong

2006-01-24, 7:06 pm


"James Westby" <jw2328@bris.ac.uk> wrote in message
news:q4tBf.179198$D47.137267@fe3.news.blueyonder.co.uk...
>
> When you create a response for the user it does not necessarily get sent
> back as soon as you start, and more importantly the headers do not
> necessarily finish as soon as you start writing to the body. This allows
> you to manipulate the headers whenever you want, which can be convinient.
>
> My guess is that this is different in PHP then, do you have to do all
> header stuff at the top of a page or something? If so then it will finish
> the headers in the reponse the moment you manipulate the body. Obviously
> this works fine in some cases, but could make some things difficult. I
> have seen PHP pages where one page is chopped up in to multiple fragments,
> how would you maipulate the headers in a fragment further down? I don't
> like this pattern at all, for one it makes the code almost impossible to
> read. I've seen mention of a Java thing called sitemesh somewhere that
> allows you to do something similar, but all the chunks are valid bits of
> HTML, and so it is much less confusing, but it may still have problems, I
> haven't used it so I don't know.


In PHP, the default is as you've guessed: As soon as some body content
is sent, it is an error to then try to emit header content. However, it is
possible to set up your PHP environment so that output is buffered, to make
it behave like JSPs as described above in the first paragraph of yours that
I've quoted.

In PHP, there's a lot of "you don't know how the envrionment is set up"
(unlike Java), so a common practice is to build up the page in a string, and
then, at the very end, emit all the headers, and then the entire body at
once, essentially implementing buffered output manually, so that you don't
need to rely on the environment being set up to buffer output automatically.

- Oliver


phillip.s.powell@gmail.com

2006-01-25, 4:13 am

Thank you for the clarification! I could not have explained it better
myself. And yes, common practice in PHP development is to put the HTML
content into a string variable, manipulate the headers if need be, and
then "echo" the string variable contents at the end. I am sure I could
do the same configuration in JSP as well.

Phil

Oliver Wong wrote:
> "James Westby" <jw2328@bris.ac.uk> wrote in message
> news:q4tBf.179198$D47.137267@fe3.news.blueyonder.co.uk...
>
> In PHP, the default is as you've guessed: As soon as some body content
> is sent, it is an error to then try to emit header content. However, it is
> possible to set up your PHP environment so that output is buffered, to make
> it behave like JSPs as described above in the first paragraph of yours that
> I've quoted.
>
> In PHP, there's a lot of "you don't know how the envrionment is set up"
> (unlike Java), so a common practice is to build up the page in a string, and
> then, at the very end, emit all the headers, and then the entire body at
> once, essentially implementing buffered output manually, so that you don't
> need to rely on the environment being set up to buffer output automatically.
>
> - Oliver


phillip.s.powell@gmail.com

2006-01-25, 4:14 am

I did just that tonight, and got "" back. Not null, of course, as the
trim() method would have thrown NullPointerException exception, instead
just got an empty string back, no HTML content of any kind.

Phil

Oliver Wong wrote:
> <phillip.s.powell@gmail.com> wrote in message
> news:1138006231.342651.158910@z14g2000cwz.googlegroups.com...
>
> You have some sort of relationship between GET urls, and cookies which I
> don't understand (e.g. you shouldn't normally be able to "URL scrape" a
> value stored in a cookie, but it looks like you have a special JSP page so
> that this becomes possible).
>
> Have you tried removing the call to replaceAll() and trim() and seeing
> what raw data you are getting from your HTTP connection?
>
> - Oliver


phillip.s.powell@gmail.com

2006-01-25, 4:14 am

I'll try that next, although you must understand why I simply can't
fathom how you can do that..

Meanwhile you had given me an idea about passing an Enumeration object
from MessageProcessor class to the MessageBinHashBundler class to the
CookieRetriever class so that its getCookieVal() method would return,
in the end, the cookie name.

Where did I go wrong?

Here is the code snippet to MessageProcessor.process() method:

code:
// NEW 1/24/2006: LOOK FOR Enumeration OBJECT FIRST FOR MORE EFFICIENT MEANS OF HANDLING COOKIE via MessageBinHashBundler Enumeration e = this.request.getHeaders("Cookie"); // WILL RETURN AN Enumeration BASED UPON COOKIE INFO if (e != null) { bundler = new MessageBinHashBundler(this.nickname, (Object)e); // CREATE CLASS INSTANCE TO BUNDLE MESSAGE } else { bundler = new MessageBinHashBundler(this.nickname); }


At this point "e" is a valid Enumeration and all is well. I figured
that it be best to pass it as a serializable Object to recreate it
later as another Enumeration, so that's why I did that.

So now we're in MessageBinHashBundler:

code:
/** * Constructor * * New 1/24/2006: Will override constructor by passing Object parameter (that was Enumeration) * * @access public * @param String nickname * @param Object enumm */ public MessageBinHashBundler(String nickname, Object enumm) { this.nickname = nickname; this.enumm = enumm; if (this.enumm != null) this.willUseEnumeration = true; } /** * Convert String message to Hashtable * * @access public * @param String message * @return Hashtable messageHash * @uses CookieRetriever * @uses ChatGlobals */ public Hashtable convert(String message) { Hashtable messageHash = new Hashtable(1); // NEW 1/21/2006: PREVENT THROWING NullPointerException BY RESETTING PARAMETER message TO "" IN CASE IT IS Null if (message == null) message = ""; if (!this.isFromChatOp && this.nickname != null && !this.nickname.equals("")) { messageHash.put(this.nickname, message); } else if (!this.isFromChatOp && this.willUseEnumeration && this.enumm != null) { // NEW 1/24/2006: USE Enumeration OBJECT WITH EMBEDDED COOKIE INFORMATION IF REQUESTED TO DO SO CookieRetriever cr = new CookieRetriever(ChatGlobals.CHAT_COOKIE_NAME, this.enumm); messageHash.put(cr.getCookieVal(this.willUseEnumeration), message); } else if (!this.isFromChatOp) { // HASHTABLE KEY WILL BE NICKNAME STORED IN COOKIE REF BY ChatGlobals.CHAT_COOKIE_NAME CookieRetriever cr = new CookieRetriever("http://" + ChatGlobals.SERVER_NAME + ChatGlobals.SCRIPT_PATH + "/" + ChatGlobals.COOKIE_GRABBER_FILENAME, ChatGlobals.CHAT_COOKIE_NAME); messageHash.put(cr.getCookieVal(), message); } else { // HASHTABLE KEY WILL BE STATIC VALUE OF ChatGlobals.CHAT_BRAND_NAME messageHash.put(ChatGlobals.CHAT_BRAND_NAME, message); } return messageHash; }


Ok, so at this point we went like this:

1) MessageProcessor passes an Enumeration to MessageBinHashBundler cast
as an Object
2) MessageBinHashBundler pass an Object to CookieRetriever

Here is CookieRetriever.getCookieVal(boolean willUseEnumeration)

code:
/** * Retrieve cookie value from HttpServletRequest request.getHeaders("Cookie") which spawned the Enumeration obj parameter * * @access public * @param boolean willUseEnumeration * @return String name of cookie */ public String getCookieVal(boolean willUseEnumeration) { // STRING METHOD String cookieText = ""; int index = 0; if (willUseEnumeration && this.enumm != null) { Enumeration e = (Enumeration)this.enumm; cookieText = (String)e.nextElement(); while (e.hasMoreElements()) { cookieText = (String)e.nextElement(); index = cookieText.indexOf("="); if (index > -1 && cookieText != null && cookieText.substring(0, index).trim().equals(this.cookieName)) return cookieText.substring(index + 1); } return "blah"; } else { // GO BACK TO URL-SCRAPING-BASED getCookieVal() METHOD return this.getCookieVal(); } }


And yet, even after:

3) CookieRetriever sets a local Enumeration "e" as the
Enumeration-casted Object value of this.enumm

e.hasMoreElements() is always false! Even when the original boolean
return value all the way back from MessageProcessor was true!

So at this point I'm just plain out of ideas. I'm open, sorry, help!

Phil
James Westby wrote:
> phillip.s.powell@gmail.com wrote:
> [snip]
>
> How's this code. One import, no second request, no "scraping", no
> javax.servlet.http.Cookie, no messing.
>
> <HTML><HEAD><TITLE>TEST</TITLE></HEAD><BODY>
>
> <%@ page import="java.util.Enumeration" %>
>
> Cookies: <% Enumeration e = request.getHeaders("Cookie");
> while (e.hasMoreElements()) {
> String cookie = (String)e.nextElement();
> int index = cookie.indexOf("=");
> if (index > -1) {
> out.println("Name:
> "+cookie.substring(0,index)+"<br>");
> out.println("Value:
> "+cookie.substring(index+1)+"<br><br
> } else {
> out.println("Error<br>");
> }
> }
> %>
>
> <% response.setHeader("Set-Cookie", "name=Bob"); %>
>
> </BODY></HTML>
>
> The first load of the page will show just Cookie:
>
> The second should show the name value pairs of the cookie that was set
> the first time.
>
> Considerably less code than you were trying to write, and no headaches.
>
>
> James


phillip.s.powell@gmail.com

2006-01-25, 7:16 pm

See below, thanx

James Westby wrote:
> phillip.s.powell@gmail.com wrote:
> [snip]
>
> There's no need to worry about passing it as a serializable object when
> you are merely calling functions. Pass it as an enumeration to take
> better advantage of Java's type safety.
>


It didn't work when I did that; the Enumeration within
MessageBinHashBundler always became e.hasMoreElements() = false each
time whereas it would be e.hasMoreElements() = true PRIOR to it being
passed to MessageBinHashBundler.


> [snip]
>
> OK
>
>
> You have pulled out the first element before you start the loop. This is
> a bad idea. For one you will get an exception if e has no elements. For
> another the first element will always be skipped. Lastly if there is
> only one element you will never do anything with the enumeration. have
> another look at my sample code.
>
>
> You are falling back to a method that you know doesn't work? Or is it
> working now? Or is this just here as you are modifying your code?
>


I was modifying to debug my code because I kept constantly getting
e.hasMoreElements() = false..

I gave up on the whole Enumeration idea, sorry, way too hard for me to
work with and will never figure it out quickly (went to several online
tutorials, to no avail). I decided on a more "elementary school
level" approach:

1) Use Cookie[] from this.request.getCookies() within MessageProcessor.
2) Bundle the Cookie array into a Hashtable with key = cookie name and
value = cookie value
3) Pass THAT to MessageBinHashBundler (and ultimately to
CookieRetriever).

I can work with Hashtables!

>
> Because you have removed the only element before you call it.
>
>
> Hope this fixes it.
>
>
> James


It didn't, it still returned "false" even after removing the initial
(String)e.nextElement() line. Like I said, I can't work with
Enumerations so I'm done with them. Hashtables instead!

Phil

Oliver Wong

2006-01-25, 7:16 pm

<phillip.s.powell@gmail.com> wrote in message
news:1138206847.952584.5350@g43g2000cwa.googlegroups.com...
>
> Like I said, I can't work with
> Enumerations so I'm done with them. Hashtables instead!


Enumerations and Hashtables are two distinct concepts. Neither of them
do anything similar to what the other one does.

Enumeration is a way of dealing with a collection of elements, one at a
time.

Hashtable is a way of associating a key with a value.

I'm not sure what you're trying to do, but the following code (written
by you, I think):
[color=darkred]


is correct usage of of Enumerations.

- Oliver


phillip.s.powell@gmail.com

2006-01-25, 7:16 pm

See below, thanx

Oliver Wong wrote:
> <phillip.s.powell@gmail.com> wrote in message
> news:1138206847.952584.5350@g43g2000cwa.googlegroups.com...
>
> Enumerations and Hashtables are two distinct concepts. Neither of them
> do anything similar to what the other one does.
>
> Enumeration is a way of dealing with a collection of elements, one at a
> time.
>
> Hashtable is a way of associating a key with a value.
>


Right, and since I'm a PHP guy, the Hashtable makes more sense as I
understand "associating a key with a value" as an "associative array",
which in PHP is all you have (enumerative arrays are not truly
"enumerative" but take on that role though and can be created with that
identity)

> I'm not sure what you're trying to do, but the following code (written
> by you, I think):
>
>
>
> is correct usage of of Enumerations.


And also doesn't work. (String)e.nextElement() is always "" regardless
of what it was intitially.

Phil

>
> - Oliver


Oliver Wong

2006-01-25, 7:16 pm


<phillip.s.powell@gmail.com> wrote in message
news:1138219040.370041.260690@g44g2000cwa.googlegroups.com...
> See below, thanx
>
> Oliver Wong wrote:
>
> Right, and since I'm a PHP guy, the Hashtable makes more sense as I
> understand "associating a key with a value" as an "associative array",
> which in PHP is all you have (enumerative arrays are not truly
> "enumerative" but take on that role though and can be created with that
> identity)


While I agree that a Hashtable is like an associative array, an
enumeration is NOT like an "enumerative array". It's closer to the foreach()
construct in PHP.

>
>
> And also doesn't work. (String)e.nextElement() is always "" regardless
> of what it was intitially.


That would be a problem with the collection from which the enumeration
is being generated, rather than a problem with the enumeration itself.

I see you're getting the Enumeration from
this.request.getHeaders("Cookie"). I don't know what the "getHeaders" method
does, but its name implies that it will return a collection of headers,
rather than an enumeration over the collection of headers. Is that method
part of some library, or did you write it yourself?

- Oliver


phillip.s.powell@gmail.com

2006-01-26, 7:05 pm

See below, thanx

James Westby wrote:
> phillip.s.powell@gmail.com wrote:
>
> [snip]
> [snip]
>
> What do you mean "what it was originally"?
>
> How are you setting your cookie values?
>


I initially set my cookie values in chat_validation.jsp:

code:
response.addCookie(new Cookie(ChatGlobals.CHAT_COOKIE_NAME, (Object)request.getParameter("nick")));


And then redirect to chat.jsp. If that's what you mean. Otherwise I'm
not sure what you mean.

Phil

>
> James


phillip.s.powell@gmail.com

2006-01-26, 7:05 pm

Maybe it's best to skip this and let you know that last night I got it
to work after a w of trying, but I simply will never understand why
it works.

When you send a message to be added to the messages file, it's supposed
to prepend onto the message "[your cookie value i.e. your nickname]>",
e.g. "James>" or "Phil>" or "Whoever>", and then your message, which
gets displayed.

That is the sole purpose of the class MessageBinHashBundler, which
takes that.

The file "chat_submit_message.jsp" will submit your message to the
messages file. Here is how it originally worked:

1) It would data scrape the servlet "ChatServlet" with query string
"?message=" + URLEncoder.encode(message, "UTF-8") + "&nickname=" +
URLEncoder.encode(cookie, "UTF-8")

2) The servlet would instantiate a MessageProcessor object which would
perform its process() method, call up MessageBinHashBundler, combine
the message into the "Phil> blah blah blah" message format and place it
into the message file.

Problem is, [1] never worked inasmuch as you'd never see "Phil> blah
blah blah" you would insted see "> blah blah blah" because the cookie
"stopped existing" for some unknown reason.

So what I did was something I looked up online that worked. Instead of
data scraping the servlet to call up MessageProcessor object, I called
it directly after performing request.setAttribute():

code:
request.setAttribute("message", (Object)URLEncoder.encode(message, "UTF-8")); request.setAttribute("nickname", (Object)URLEncoder.encode(cookie, "UTF-8")); MessageProcessor mp = new MessageProcessor(request, response); mp.process();


When I use THIS code in chat_submit_message.jsp, sure enough,
ultimately in the message file, I see "Phil> blah blah blah" or
"Whoever> blah blah blah" the way it should be.

In short, it works and I can't possibly explain why.

Phil
James Westby wrote:
> phillip.s.powell@gmail.com wrote:
> [snip]
>
> I'll rephrase my question.
>
> What do you mean "what it was initially"?
>
>
> The constructor of Cookie takes two Strings, not a String and an Object
> (assuming CHAT_COOKIE_NAME is a String). Does this code compile?
>
>
> And are you expecting to see the Cookie first time?
>
> How are you doing the redirection?
>
>
> James


phillip.s.powell@gmail.com

2006-01-26, 7:06 pm

Thanx for all your help on this!

Incidentally, another case of "weirdness" is that while sending a
message (using form post) requires request.setAttribute(), if I click
the "Quit" button (to leave the chatroom), request.setAttribute() fails
to retrieve required messages from an external XML file and I have to
revert back to using the HTMLRetriever data scrape to scrape the
servlet URL and all works there.

Phil

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com