• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explain why request parameter need to be added as attribute before forwarding

 
Arnab Sinha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please clear the below doubt:

Form parameters, when sent to a servlet are retrieved using the getParameter(String) method. However, this same parameter is added as an attribute to the request object before forwarding it to a jsp.

But, a jsp is finally a servlet, so why cant it retrieve this value using getParameter(string) instead of getAttribute(String)?

Maybe I need to start reading the spec, but I was thinking of first completing the book before reading the spec.

Any responses will be appreciated.

[ August 16, 2007: Message edited by: Arnab Sinha ]
[ August 16, 2007: Message edited by: Arnab Sinha ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to read this.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, I can think of no reason that request parameters need to be copied to scoped variables.
 
Frederic Thierry
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arnab keep reading the book you will find out that the JSP can access the form parameters using scriptlets, el expressions and standard actions. Advantage of attributes is that they allow you the programmer to add (or set) programmatic (in your servlet java code) parameters. You do not have a setParameter() method out there since parameters are linked to html forms (and of course init parameters and context parameters are linked to the Deployment descriptor files).

You might even want to adjust the form parameter before dispatching it to another page and hence you can use the setAttribute() method to add your new adjusted parameter value to the request object before dispatching this to another servlet or JSP.
 
Arnab Sinha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the situation is I have designed a simple web app to test security. I am using the form based security, so the user-name is stored in j_username as per container requirement. Once the user logs in successfully I would like to display the user-name.

However when I use request.getParameter("j_username") it displays null. Not sure how I can save the user-name, or is there a way to add an attribute to the request object?
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF security is working, then you should be able to get the name from the Principal object that is part of the request.
 
Frederic Thierry
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually j_security_check implementation is application
server specific. It's up to vendor how to implement the standard. I believe the Tomcat server creates a HttpSession but your request was not added to this session hence I dont think you can access those parameters.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic