Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

request.setAttribute() fails to set attribute - help

Thread view: 
phillip.s.powell@gmail.com - 28 Jan 2006 21:48 GMT
[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();
  }
 }

[/CODE]

When viewing the contents via out and you click the "Send" button, you
get this:

[quote]message = blah blah blah and nickname = null[/quote]

When viewing the contents via out and you click the "Quit" button, you
get this:

[quote]message = and nickname = null[/quote]

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 - 28 Jan 2006 22:19 GMT
>     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:
[quoted text clipped - 10 lines]
> 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
Signature

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 - 28 Jan 2006 22:28 GMT
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();
  }
 }
[/CODE]

At this point "nickname" always exists, but
request.setAttribute("message"..) fails once again.

Phil

> >     request.setAttribute("message", (Object)URLEncoder.encode(message,
> > "UTF-8"));
[quoted text clipped - 37 lines]
> Always remember: The answer is forty-two, there can only be wrong
>                  questions!
James Westby - 29 Jan 2006 02:03 GMT
> 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
James Westby - 29 Jan 2006 01:59 GMT
> [CODE]
>  cookieArray = request.getCookies();
[quoted text clipped - 77 lines]
> 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
phillip.s.powell@gmail.com - 29 Jan 2006 11:35 GMT
See below, thanx (want to be my Java tutor?)

> > [CODE]
> >  cookieArray = request.getCookies();
[quoted text clipped - 88 lines]
> 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();
  }
 }
[/CODE]

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 - 29 Jan 2006 12:31 GMT
[snip]

>>It looks like your problem is that the
>>
[quoted text clipped - 10 lines]
> 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]

>>It looks like you are trying to combine logic for handling people
>>submitting messages and quitting in one place, is that correct?
[quoted text clipped - 22 lines]
>                                  );
>     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();
[quoted text clipped - 8 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.