• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i do not understand servlet request.setAttribute ~

 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm newbie in struts, i do have difficulty of understanding on how request.setAttribute work , what is the value i get after request.setAttribute?

SignupAction1.java


MessageBean.java



Q1) from SignupAction1.java , how do i get the values/attribute after the setAttribute method ?

Q2) what i guess on retrieved attribute here is "message" , can anyone kindly please guide or show me the way how this attribute been retrieve ?

thank you very much for helping!
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your question is really about the Servlet API, I'm going to move this to Servlets.
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Attributes are retrieved from the request using HttpServletRequest.getAttribute(). A quick example is:



Simple, easy, like God intended.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chris, you means setAttribute not necessary for only retrieve form data right ? in my case, it actually get "message" variable from MessageBean.java ? if so , how to i show the output it ?
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpServletRequest.getAttribute() and HttpServletRequest.setAttribute() go together like peas and carrots. The existance of one is only justified by the existance of the other.

But to your question, setAttribute() is used for putting Objects into the request and getAttribute() is used for getting those previously placed Objects back out. If your application never uses setAttribute than consequently there will never be a reason to call getAttribute().

Now, I get the feeling what you are really trying to do is get at the HTTP request parameters. This is a slightly different beast. HTTP parameters are all passed as Strings (as opposed to Objects) and are available via readonly access. The way to get to these parameters is by using HttpServletRequest.getParameter(). As you seem to be pretty new to the Servlet/JSP world I highly that you run out and pick up a copy of a good Servlet book (see the Bunkhouse for some ideas). Otherwise you are in for a world of pain as you learn the ropes.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks , chris , i have better understanding now ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic