SunQuest
           Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old June 17th, 2002, 07:52 AM
itsource itsource is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 181 itsource User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 31 sec
Reputation Power: 7
Declare variable in Scriplet or Declaration?

When I declare variable in Scriptlet
Code:
<%
	java.util.Date a = new java.util.Date();
%>
current time is <%=a%>


When Refresh browser I get different outout

current time is Mon Jun 17 19:44:14 ICT 2002
current time is Mon Jun 17 19:44:26 ICT 2002
current time is Mon Jun 17 19:44:34 ICT 2002
current time is Mon Jun 17 19:44:42 ICT 2002

but when I declare variable in Declaration
Code:
<%
	java.util.Date a = new java.util.Date();
%>
current time is <%=a%>


When Refresh browser I get same out.put

current time is Mon Jun 17 19:45:19 ICT 2002
current time is Mon Jun 17 19:45:19 ICT 2002
current time is Mon Jun 17 19:45:19 ICT 2002
current time is Mon Jun 17 19:45:19 ICT 2002

I see many code, some code declare in Declaration, some code declare in Scriptlet

Which application or Which situation should declare in Declaration, and declare in Scriptlet?

Reply With Quote
  #2  
Old June 21st, 2002, 01:39 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
Correct me if I'm wrong, but your code samples are identical. What I think you are saying is creating a variable in a scriptlet tag (<% %>) vs a Declaration tag (<%! %>).

The difference in the two tags is where the variable is created. The scope of the variable when it is converted into a servlet.

Everything put in scriptlet tags are put directly into the jsp_service method. This is the mthod called each time the JSP is accessed by the server. Everything in the Declaration tag is made into a class variable. These are made only the first time the jsp is accessed.

Using your examples (if they were listed as you intended), this is where the variables would end up in the source:
Code:
<%!
	java.util.Date a1 = new java.util.Date();
%>
current time is <%=a1%>

----------------
vs
----------------

<%
	java.util.Date a2 = new java.util.Date();
%>
current time is <%=a2%>

(Note, code is not complete)
Code:
public class jsp_name extends HttpJspBase {
  public java.util.Date a1 = new java.util.Date();
  ..more code...
  public _jspService(..vars..) {
    java.util.Date a2 = new java.util.Date();
    ...more code....
  }
}

As you can see, scriptlets tags are local to the service method and thus are redeclared every access. Declarations tags are local to the servlet itself, and thus are declared only the first time the servlet is accessed. Hope that helps.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Declare variable in Scriplet or Declaration?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway