Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do I need to use ! for declarations?

 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have read somewhere that when declaring methods in a jsp page in a scriptlet that you need to use <%! to indicate a declaration is this compulsory?
Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All variable and method declarations have to be enclosed in <%! %> tags.
Sun has a lovely short summary of JSP syntax somewhere on the java.sun.com site (they keep moving things around but I bet you can find it with a search.) I keep several copies on my desk.
Bill
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thing is, i'm sure i've seen code where variables are declared without using the <%! syntax. Is this only needed for methods and classes?
Thanks
 
Trish Wu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variable:
<%! String _var = "instance"; %>
Local variable (within _jspService():
<% String localVar = "local"; %>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64699
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An exercise that would be very instructional would be to try variations on these themes and then take a peek at the java file that the container creates for you.
Understanding how the container creates the servlet that represents your JSP page is a big step in understanding how to use JSP wisely.
hth,
bear
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trish and Bear thanks for the explanation and tip. One thing that puzzles me, is a new servlet instance created for every request of a page or are they shared between users?
Thanks
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very important aspect of jsp's to understand, indeed.
By default, a single instance of the serlvet resulting from the jsp translation phase is instantiated. Each request is serviced in a separate thread.
IE: the resulting servlet does not implement SingleThreadModel
This means you have to be thread-aware when dealing with access to state ( instance variables, class variables ) in your jsp. Local variables, including automatics passed as args to methods, are all private to the servicing thread, so no worries w/them.
I don't know if there is a way to influence thejsp translation phase to create a servlet that is single-threaded.
Anyone?
(btw: I'm surely not advocating SingleThreadModel. Personally, I think it's the wrong way to go about ensuring thread-safety.)
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can make a JSP implement SingleThreadModel with the following page directive:

It is not recommended if you can avoid it but it is possible.
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for all the replies. I think i undertand this. To clarify unless specifying the page to be thread safe each time a page is requested it spawns a new thread? Is a new instance of the servlet created for each request of the page or simply a new thread?
Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet engine has one instance of a servlet which stays in memory. Every request gets a Thread assigned to it - this Thread may be new or from a pool that the engine keeps. After constructing the request and response objects, this Thread executes the service method in the servlet. After the request is sent, the servlet engine can re-use the Thread.
Many Threads may be executing in the same servlet instance "at the same time." Furthermore, this instance may stay in memory for days and service thousands of requests, OR the servlet engine may decide to discard it and create a new instance when the next request comes in - this is not under your control.
(Above assumes you are not using SingleThreadModel)
Bill
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys
Hi sorry if i'm asking stupid questions. SO every jsp page is translated into a servlet which the servlet engine keeps in memory. When a client
requests the page if this servlet is still in memory it assigns a thread and calls the service method for that servlet.
Is my understanding correct?
Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly correct. You should download the servlet and JSP APIs from java.sun.com for all the details on what servlet engines do.
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i'll do that, thanks for all your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic