aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes q on jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "q on jsp " Watch "q on jsp " New topic
Author

q on jsp

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
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.


SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,
The code doesn't compile. The method def is incomplete.
Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
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 ]

Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
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

Joined: Mar 04, 2005
Posts: 91
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: q on jsp
 
Similar Threads
declarations and scriptlets
a question about bean
URL rewriting
org.apache.jasper.JasperException: java.lang.ArrayIndexOutOfBoundsException: 8
Database Table lookup and populate drop down in JSP