This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

q on jsp

 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wht will be the result of accessing the following jsp page, if there is no attribute by name "str" in session?

<%! String str;
public void jspInit()
{ str = (String) session.getAttribute("str");%>
The strins is <%=str%>

1) null is returned.
2) nullpointerexception is thrown
3) code doesnt compile
4) none of the above.

I am confused between 1 & 2. plz help.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The code doesn't compile. The method def is incomplete.
Thanks
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use JSP implicit objects in declarations and directives. Thats the first mistake in your code. Then there is no ending braces for the method. If these two are correct then


If you didn't set an attribute(in any of the four scopes in your servlet code) and try to access that attribute in a JSP Page using JSP implicit objects(in a scriptlet(or) expression) then you will get "null" as output in JSP Page. There will be NO compile time errors (or) runtime exceptions

NOTE:

Whereas in EL if you try to access that attribute using EL implicit objects then nothing will be printed in the JSP page. It will be blank output.

Hope it is useful
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the 2 errors pointed by Vishnu are corrected, I believe it will throw a Null Pointer Exception if the "str" attribute is not in the session. Because we are trying to cast something that's null to string.
[ October 24, 2005: Message edited by: Bosun Bello ]
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bosun,

It'll not throw null pointer exception..coz "null" can very well be type casted to String type.

Regards,
Priya.
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried this one out. For this exact code:

it comes with a compile time error due to the unmatched brackets.

for this code, when i put the brackets:



it still comes up with compile time error saying "Undefined variable or class name: session", which was my first assumption, as session is not available in jspInit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic