Home > Archive > Java Help > January 2006 > request.setAttribute() fails to set attribute - help
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 |
request.setAttribute() fails to set attribute - help
|
|
| phillip.s.powell@gmail.com 2006-01-28, 7:05 pm |
| code:
cookieArray = request.getCookies();
if (cookieArray != null) {
for (int i = 0; i < cookieArray.length; i++) {
if
(cookieArray[i].getName().trim().toLowerCase().equals(ChatGlobals.CHAT_COOKIE_NAME.trim().toLowerCase())
&&
cookieArray[i].getValue() != null
) {
cookie = cookieArray[i].getValue(); // GET COOKIE
break;
}
}
}
// AT THIS POINT I HAVE cookie VERIFIED VIA out.println(cookie)
MessageProcessor mp = null;
// SEND MESSAGE DIRECTLY TO MessageProcessor AVOIDING ANY ISSUES WITH
URL DATA SCRAPING INVOLVING QUERY STRINGS
if (hasSubmittedMessage && send.length() > 0) {
try { // USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
request.setAttribute("message", (Object)URLEncoder.encode(message,
"UTF-8"));
request.setAttribute("nickname", (Object)URLEncoder.encode(cookie,
"UTF-8"));
out.println("message = " + request.getParameter("message") + " and
nickname = " + request.getParameter("nickname"));
mp = new MessageProcessor(request, response);
mp.process();
} catch (UnsupportedEncodingException uee) {
errorMsg = "Error involving message submittal: " + uee.toString();
} catch (Exception e) {
errorMsg += "Unknown error: " + e.toString();
}
} else if (hasSubmittedMessage && quit.length() > 0) {
try { // USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
// HTMLRetriever WORKS HERE INSTEAD OF USING request.setAttribute()
I HAVE NO IDEA WHY
//retriever = new HTMLRetriever(ChatGlobals.SERVLET_SELF +
"/ppowell.ChatServlet?message=" +
// URLEncoder.encode("/q", "UTF-8") +
"&nickname=" +
// URLEncoder.encode(cookie, "UTF-8")
// );
// String junk = retriever.getHTML();
request.setAttribute("message", (Object)URLEncoder.encode("testing
1 2 3", "UTF-8"));
request.setAttribute("nickname", (Object)URLEncoder.encode(cookie,
"UTF-8"));
out.println("message = " + request.getParameter("message") + " and
nickname = " + request.getParameter("nickname"));
mp = new MessageProcessor(request, response);
mp.process();
} catch (UnsupportedEncodingException uee) {
errorMsg += "Error involving message submittal: " + uee.toString();
} catch (Exception e) {
errorMsg += "Unknown error: " + e.toString();
}
}
When viewing the contents via out and you click the "Send" button, you
get this:
quote: message = blah blah blah and nickname = null
When viewing the contents via out and you click the "Quit" button, you
get this:
quote: message = and nickname = null
How is this possible when I'm setting request.setAttribute()
beforehand? Is there something else I need to do to alter or add values
to HttpServletRequest request before submitting it as parameter to
MessageProcessor?
Please advise, thanx
Phil
| |
| Lothar Kimmeringer 2006-01-28, 7:05 pm |
| phillip.s.powell@gmail.com wrote:
> request.setAttribute("message", (Object)URLEncoder.encode(message,
> "UTF-8"));
> request.setAttribute("nickname", (Object)URLEncoder.encode(cookie,
> "UTF-8"));
> out.println("message = " + request.getParameter("message") + " and
> nickname = " + request.getParameter("nickname"));
[...]
> When viewing the contents via out and you click the "Send" button, you
> get this:
>
> quote: message = blah blah blah and nickname = null
>
> When viewing the contents via out and you click the "Quit" button, you
> get this:
>
> quote: message = and nickname = null
>
> How is this possible when I'm setting request.setAttribute()
> beforehand? Is there something else I need to do to alter or add values
> to HttpServletRequest request before submitting it as parameter to
> MessageProcessor?
I'm not sure if I understand your problem, but if you set something
with setAttribute, shouldn't you use getAttribute for getting
the attribute again? getParameter returns the value for a given
querystring-entry, e.g.
http://somehost.invalid/path/servlet?key=value
key=value is the querystring and request.getParameter("key")
should return "value". Maybe there is a setParameter-method
(I dont' have the Servlet-API at hand right now) you can use
and solve your problem.
Regards, Lothar
--
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!
| |
| phillip.s.powell@gmail.com 2006-01-28, 7:05 pm |
| If there is a request.setParameter() method, the API doesn't indicate
that, else I would have used it for sure :(
I revised my code a bit and got the "nickname" portion to work with an
extremely simple solution: make it an HTML form element instead!!
code:
cookieArray = request.getCookies();
if (cookieArray != null) {
for (int i = 0; i < cookieArray.length; i++) {
if
(cookieArray[i].getName().trim().toLowerCase().equals(ChatGlobals.CHAT_COOKIE_NAME.trim().toLowerCase())
&&
cookieArray[i].getValue() != null
) {
cookie = cookieArray[i].getValue(); // GET COOKIE
break;
}
}
} else {
cookie = request.getParameter("nickname");
}
MessageProcessor mp = null;
// SEND MESSAGE DIRECTLY TO MessageProcessor AVOIDING ANY ISSUES WITH
URL DATA SCRAPING INVOLVING QUERY STRINGS
if (hasSubmittedMessage && send.length() > 0) {
out.println("message = " + request.getParameter("message") + " and
nickname = " + request.getParameter("nickname"));
mp = new MessageProcessor(request, response);
mp.process();
} else if (hasSubmittedMessage && quit.length() > 0) {
try { // USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
// HTMLRetriever WORKS HERE INSTEAD OF USING request.setAttribute()
I HAVE NO IDEA WHY
//retriever = new HTMLRetriever(ChatGlobals.SERVLET_SELF +
"/ppowell.ChatServlet?message=" +
// URLEncoder.encode("/q", "UTF-8") +
"&nickname=" +
// URLEncoder.encode(cookie, "UTF-8")
// );
// String junk = retriever.getHTML();
request.setAttribute("message", (Object)URLEncoder.encode("/q",
"UTF-8"));
out.println("message = " + request.getParameter("message") + " and
nickname = " + request.getParameter("nickname"));
mp = new MessageProcessor(request, response);
mp.process();
} catch (UnsupportedEncodingException uee) {
errorMsg += "Error involving message submittal: " + uee.toString();
} catch (Exception e) {
errorMsg += "Unknown error: " + e.toString();
}
}
At this point "nickname" always exists, but
request.setAttribute("message"..) fails once again.
Phil
Lothar Kimmeringer wrote:
> phillip.s.powell@gmail.com wrote:
>
> [...]
>
> I'm not sure if I understand your problem, but if you set something
> with setAttribute, shouldn't you use getAttribute for getting
> the attribute again? getParameter returns the value for a given
> querystring-entry, e.g.
>
> http://somehost.invalid/path/servlet?key=value
>
> key=value is the querystring and request.getParameter("key")
> should return "value". Maybe there is a setParameter-method
> (I dont' have the Servlet-API at hand right now) you can use
> and solve your problem.
>
>
> Regards, Lothar
> --
> Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
> PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
>
> Always remember: The answer is forty-two, there can only be wrong
> questions!
| |
| James Westby 2006-01-28, 9:58 pm |
| phillip.s.powell@gmail.com wrote:
> code:
> cookieArray = request.getCookies();
> if (cookieArray != null) {
> for (int i = 0; i < cookieArray.length; i++) {
> if
> (cookieArray[i].getName().trim().toLowerCase().equals(ChatGlobals.CHAT_COOKIE_NAME.trim().toLowerCase())
> &&
> cookieArray[i].getValue() != null
> ) {
> cookie = cookieArray[i].getValue(); // GET COOKIE
> break;
> }
> }
> }
>
> // AT THIS POINT I HAVE cookie VERIFIED VIA out.println(cookie)
>
> MessageProcessor mp = null;
>
> // SEND MESSAGE DIRECTLY TO MessageProcessor AVOIDING ANY ISSUES WITH
> URL DATA SCRAPING INVOLVING QUERY STRINGS
> if (hasSubmittedMessage && send.length() > 0) {
> try { // USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
> try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
> request.setAttribute("message", (Object)URLEncoder.encode(message,
> "UTF-8"));
> request.setAttribute("nickname", (Object)URLEncoder.encode(cookie,
> "UTF-8"));
> out.println("message = " + request.getParameter("message") + " and
> nickname = " + request.getParameter("nickname"));
> mp = new MessageProcessor(request, response);
> mp.process();
> } catch (UnsupportedEncodingException uee) {
> errorMsg = "Error involving message submittal: " + uee.toString();
> } catch (Exception e) {
> errorMsg += "Unknown error: " + e.toString();
> }
> } else if (hasSubmittedMessage && quit.length() > 0) {
> try { // USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
> try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
>
> // HTMLRetriever WORKS HERE INSTEAD OF USING request.setAttribute()
> I HAVE NO IDEA WHY
> //retriever = new HTMLRetriever(ChatGlobals.SERVLET_SELF +
> "/ppowell.ChatServlet?message=" +
> // URLEncoder.encode("/q", "UTF-8") +
> "&nickname=" +
> // URLEncoder.encode(cookie, "UTF-8")
> // );
> // String junk = retriever.getHTML();
> request.setAttribute("message", (Object)URLEncoder.encode("testing
> 1 2 3", "UTF-8"));
> request.setAttribute("nickname", (Object)URLEncoder.encode(cookie,
> "UTF-8"));
> out.println("message = " + request.getParameter("message") + " and
> nickname = " + request.getParameter("nickname"));
> mp = new MessageProcessor(request, response);
> mp.process();
> } catch (UnsupportedEncodingException uee) {
> errorMsg += "Error involving message submittal: " + uee.toString();
> } catch (Exception e) {
> errorMsg += "Unknown error: " + e.toString();
> }
> }
>
>
>
> When viewing the contents via out and you click the "Send" button, you
> get this:
>
> quote: message = blah blah blah and nickname = null
>
> When viewing the contents via out and you click the "Quit" button, you
> get this:
>
> quote: message = and nickname = null
>
> How is this possible when I'm setting request.setAttribute()
> beforehand? Is there something else I need to do to alter or add values
> to HttpServletRequest request before submitting it as parameter to
> MessageProcessor?
>
[snip]
It looks like your problem is that the
out.println("message = " + request.getParameter("message") + " and
nickname = " + request.getParameter("nickname"));
lines are not printing what you expect. Is this correct. What are you
expecting the request.getParameter invocations to return? Where have you
set those parameters?
Are you trying to get back what you put in the request with setAttribute
when you call getParameter ?
When you click the "Send" button does this make send.length() > 0? When
you click "Quit" button does this make quit.length() > 0? Is it possible
that both could be true at the same time?
When you say "when viewing the contents via out" do you mean the output
on System.out, or the values printed on out (the text written to the page)?
It looks like you are trying to combine logic for handling people
submitting messages and quitting in one place, is that correct?
James
| |
| James Westby 2006-01-28, 9:58 pm |
| phillip.s.powell@gmail.com wrote:
> If there is a request.setParameter() method, the API doesn't indicate
> that, else I would have used it for sure :(
[snip]
No, he is suggesting that you use getAttribute() methods to access
things that you have set using setAttribute() methods.
As you say there are no setParameter() methods. If you want to use those
then I guess you have to create a new request containing the original
parameters, and those that you want to add.
James
| |
| phillip.s.powell@gmail.com 2006-01-29, 7:57 am |
| See below, thanx (want to be my Java tutor?)
James Westby wrote:
> phillip.s.powell@gmail.com wrote:
> [snip]
>
> It looks like your problem is that the
>
> out.println("message = " + request.getParameter("message") + " and
> nickname = " + request.getParameter("nickname"));
>
> lines are not printing what you expect. Is this correct. What are you
> expecting the request.getParameter invocations to return? Where have you
> set those parameters?
>
request.getParameter("message") is set when the user enters something
into the text field called "message" and submits it by clicking the
"Send" button. The nickname was not originally set by anything and
needed to be added later, but I found a super-simple way of handling
that: a form hidden element called "nickname" - duh! *sigh*
> Are you trying to get back what you put in the request with setAttribute
> when you call getParameter ?
>
Yes, because I need to have the value of "message" changed only when
the user clicks the "Quit" button, because the value of "message" is
"Quit", but it needs to be "/q", however, I can't display "/q" as a
button.
> When you click the "Send" button does this make send.length() > 0? When
> you click "Quit" button does this make quit.length() > 0? Is it possible
> that both could be true at the same time?
>
No, when you click the "Send" button then send.length > 0, same with
"Quit". Because both will exist upon form submission.
> When you say "when viewing the contents via out" do you mean the output
> on System.out, or the values printed on out (the text written to the page)?
>
I'm just using debugging techniques via out to see what message is and
what nickname is, to see what's going on. Out = the implicit Servlet
property "out".
> It looks like you are trying to combine logic for handling people
> submitting messages and quitting in one place, is that correct?
>
Yes. Like I said, I got the nickname part figured out, and the message
part figured out, but the quitting portion has to be handled
differently and it works now:
code:
if (hasSubmittedMessage && send.length() > 0) {
// SEND MESSAGE DIRECTLY TO MessageProcessor AVOIDING ANY ISSUES
WITH URL DATA SCRAPING INVOLVING QUERY STRINGS
mp = new MessageProcessor(request, response);
mp.process();
} else if (hasSubmittedMessage && quit.length() > 0) {
try { // USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
// HTMLRetriever WORKS HERE INSTEAD OF USING request.setAttribute()
I HAVE NO IDEA WHY
retriever = new HTMLRetriever(ChatGlobals.SERVLET_SELF +
"/ppowell.ChatServlet?message=" +
URLEncoder.encode("/q", "UTF-8") +
"&nickname=" +
URLEncoder.encode(cookie, "UTF-8")
);
String junk = retriever.getHTML();
} catch (UnsupportedEncodingException uee) {
errorMsg += "Error involving message submittal: " + uee.toString();
} catch (Exception e) {
errorMsg += "Unknown error: " + e.toString();
}
}
I simply can't alter request from request.getParameter("message") = ""
to request.getParameter("message") = "/q" (when you click the "Quit"
button and there is no message of course submitted) because I simply
don't know how.
Phil
>
> James
| |
| James Westby 2006-01-29, 7:57 am |
| phillip.s.powell@gmail.com wrote:
[snip]
>
>
> request.getParameter("message") is set when the user enters something
> into the text field called "message" and submits it by clicking the
> "Send" button. The nickname was not originally set by anything and
> needed to be added later, but I found a super-simple way of handling
> that: a form hidden element called "nickname" - duh! *sigh*
>
Good solution. Doesn't this allow the userto change their nickname to be
anything that they want with a little work? Are you OK with this?
[snip]
>
>
>
> Yes. Like I said, I got the nickname part figured out, and the message
> part figured out, but the quitting portion has to be handled
> differently and it works now:
>
> code:
> if (hasSubmittedMessage && send.length() > 0) {
> // SEND MESSAGE DIRECTLY TO MessageProcessor AVOIDING ANY ISSUES
> WITH URL DATA SCRAPING INVOLVING QUERY STRINGS
> mp = new MessageProcessor(request, response);
> mp.process();
> } else if (hasSubmittedMessage && quit.length() > 0) {
> try { // USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
> try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
>
> // HTMLRetriever WORKS HERE INSTEAD OF USING request.setAttribute()
> I HAVE NO IDEA WHY
> retriever = new HTMLRetriever(ChatGlobals.SERVLET_SELF +
> "/ppowell.ChatServlet?message=" +
> URLEncoder.encode("/q", "UTF-8") +
> "&nickname=" +
> URLEncoder.encode(cookie, "UTF-8")
> );
> String junk = retriever.getHTML();
You don't need this unused varible. If you just have
retriever.getHtml() there is an implicit cast to void. It's a good idea
to avoid unused variables, as you can make mistakes later on if you have
them lying around.
> } catch (UnsupportedEncodingException uee) {
> errorMsg += "Error involving message submittal: " + uee.toString();
> } catch (Exception e) {
> errorMsg += "Unknown error: " + e.toString();
> }
> }
>
>
> I simply can't alter request from request.getParameter("message") = ""
> to request.getParameter("message") = "/q" (when you click the "Quit"
> button and there is no message of course submitted) because I simply
> don't know how.
>
[snip]
Yeah, you can't alter it. So in the above code you're creating a new
request with the parameters you want. A slightly better way might have
been to have two methods on your servlet, or two servlets, one for
"Submit", and one for "Quit", that way they can be handled independently.
James
|
|
|
|
|