• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from java black belt?

 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Michael: you beat me to it!

Gotta learn to type faster
 
siyabonga khanyile
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooh yah...I missed that.

Thnx
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic