Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about accessing attributes with Expression Language  RSS feed

 
Syed Islam
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things I'm confused about.....

In the servlet class I have:


Why is the attribute name and attribute value the same in all the books I've read. I know one is a string literal and one is an object but must it be the same?

Second thing I'm confused about... let's say I change the servlet code to

When I try to access it using JSP:
${sessionScope.name}

it works fine. So what is the point of the first argument in Session.setAttribute() ?


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Syed Islam wrote:

Why is the attribute name and attribute value the same in all the books I've read. I know one is a string literal and one is an object but must it be the same?

Convention. They do not need to be the same. But naming one "name" and the other "somethingCompletelyDifferent" doesn't make much sense. They both represent the same thing right?

What should be the guiding principle for naming the scoped variable is "What makes the most sense when the variable is used on the JSP?"

For example, if just "name" would be confusing or ambiguous, you would make the variable name less generic: "cityName" or "dogName" or "personName" or some such.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Syed Islam wrote: let's say I change the servlet code to

When I try to access it using JSP:
${sessionScope.name}

it works fine. So what is the point of the first argument in Session.setAttribute() ?

No, it doesn't work fine. You're likely seeing the scoped variable named "name" from another run before you changed the servlet.

The scoped variable will be named "hisname", not "name".

Why are you using session scope instead of request scope anyways? Using the session is frequently not appropriate.
 
Syed Islam
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay that makes sense.

I just found it confusing because in Java, variables normally are without quotes and string value have quotation marks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scoped variables are not Java variables. (But they should still be named using Java naming conventions.)

Think of the scopes as a Map. The string you pass serves as a key into the Map of scoped variables.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!