aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes question from java black belt? 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 "question from java black belt?" Watch "question from java black belt?" New topic
Author

question from java black belt?

Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

The intent of the following JSP is to view the current Server date and time individually for each client request.



Which of the following code fragments could be used to implement this behavior?




Answer given are
<% java.util.Date now = new java.util.Date(); %>
<jsp:useBean id="now" class="java.util.Date" scope="request" />

I think if we set the bean using the jsp bean tag, we can only access it via
request.getAttribute("now").
so this should not be the answer.can some one verify the same.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Answer given are correct only!

<% java.util.Date now = new java.util.Date(); %>
<jsp:useBean id="now" class="java.util.Date" scope="request" />


what i feel is they are creating object for the java.util.Date. so we can get the value through <%= now %>.

note: i tested this .working fine

Hope This Helps
Nishal Dsouza
Greenhorn

Joined: Dec 02, 2007
Posts: 18
Hi Sunny

I dont agree with you. Because
<jsp:useBean id="now" class="java.util.Date" scope="request" /> declares and initializes the "now" variable as a local variable inside the jspService method.


Nishal Allan Dsouza<br /> <br />SCJP 5,SCWCD 5
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Ok, I got it now...
"now" will be available in the JSP in which it is declared and
it is also stuffed in the request scope as an attribute.

It was just skipped from my mind. all we need to do is, just look at the
generated java code of jsp:useBean tag.
siyabonga khanyile
Greenhorn

Joined: Aug 22, 2008
Posts: 12
Can anyone tell me why is the first one wrong cos I think we should be able to access the servlet instance from the _jspService method.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
The value of date would not change from request to request. It would stay the value it was when it is created and would be created only once.
Jan Sterk
Ranch Hand

Joined: Jun 06, 2008
Posts: 139
Originally posted by siyabonga skippy:
Can anyone tell me why is the first one wrong cos I think we should be able to access the servlet instance from the _jspService method.


<%! java.util.Date now = new java.util.Date(); %>
does compile, but the variable will be assigned only once in the lifetime of the page. So the date will not change between requests.


SCJP 1.4 (81%)<br />SCWCD 5 (95%)
Jan Sterk
Ranch Hand

Joined: Jun 06, 2008
Posts: 139
@Michael: you beat me to it!

Gotta learn to type faster
siyabonga khanyile
Greenhorn

Joined: Aug 22, 2008
Posts: 12
Ooh yah...I missed that.

Thnx
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question from java black belt?