• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

page directive confusion

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
I got the following question in one of the mock test.

Is the following JSP code legal? Select the one correct statement.
<%@page info="test page" session="false"%>
<%@page session="false"%>
(a) Yes. This is legal JSP syntax.
(b) No. This code will generate syntax errors

Answer:b

According to me, the answer is (a). Because in both the page directive the session="false".

Am I correct?

[ April 16, 2007: Message edited by: Tridib Samanta ]
[ April 16, 2007: Message edited by: Tridib Samanta ]
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tridib Samanta,

{
boolean session = false;
boolean session = false;
}

above code gives compiler error bec'ze session already exists in that block.
 
Tridib Samanta
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srinivasan,
Thanks for your reply. Have you picked up the following lines from the servlet code produced by web container?

{
boolean session = false;
boolean session = false;
}



I tried the situation in tomcat 6 and it run well.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No its more of generic code.
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasan thoyyeti:
{
boolean session = false;
boolean session = false;
}



That does not look right. The page directive is for giving the Container intructions when it performs jsp to java translation. It does not generate any variable (at least in the way it was stated before), but it affects the code generated by the Container. In the case of the "session" attribute of the page directive, it tells the 'translator' not to generate the session implicit variable and any code related to it.

If you take a look at the JSP 2.0 specs, you'll find the answer in the JSP 1.10.1 (IIRC) section, where it explains the use of the page directive. I don't have access to the specs right now, but what it says is that, with the exception of two attributes (import and pageEncoding), you might have duplicate attributes providing the values you specify are the same. In the question on the first post, it declared "session" attribute twice but with the same value: "false", so no exception should be raised at translation time.

I think Tridib is right and the mock response is not (unless there is another problem I cannot see right now).
[ April 16, 2007: Message edited by: Sergio Tridente ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergio,

Sergio: In the question on the first post, it declared "session" attribute twice but with the same value: "false", so no exception should be raised at translation time.



Its not problem with value.
Its problem with variable declaration.
Do you mean to say You will pass the compiler with fallowing code

{
int x =1;
int x =1;
}
I think thats a blunder.
[ April 16, 2007: Message edited by: Srinivasan thoyyeti ]
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Srinivasan,

No, of course the java compiler will not allow such kind of code. But there is no variable declaration involved when using a directive: <%@ page session="false" %> does not declare any "session" variable in the resulting java code. It is like a 'pre-compile time instruction' (in fact is a translation time 'instruction').

What would really not work will be something like:

<jsp:useBean id="session" class="com.scwcd.SessionBean" />
<jsp:useBean id="session" class="com.scwcd.AnotherSessionBean" />

Because the useBean standard action does declare a variable.
[ April 16, 2007: Message edited by: Sergio Tridente ]
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also, the session local variable in _jspService method will be of type HttpSession - corresponding to the jsp implicit object.
 
reply
    Bookmark Topic Watch Topic
  • New Topic