GeeCON Prague 2014*
The moose likes JSP and the fly likes Do I need to use ! for declarations? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Do I need to use ! for declarations?" Watch "Do I need to use ! for declarations?" New topic
Author

Do I need to use ! for declarations?

k Oyedeji
Ranch Hand

Joined: Jul 07, 2002
Posts: 96
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

Joined: Mar 22, 2000
Posts: 12792
    
    5
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

Joined: Jul 07, 2002
Posts: 96
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

Joined: Oct 09, 2002
Posts: 34
Instance variable:
<%! String _var = "instance"; %>
Local variable (within _jspService():
<% String localVar = "local"; %>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
k Oyedeji
Ranch Hand

Joined: Jul 07, 2002
Posts: 96
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

Joined: Aug 01, 2002
Posts: 54
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

Joined: Jul 18, 2001
Posts: 2712
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

Joined: Jul 07, 2002
Posts: 96
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

Joined: Mar 22, 2000
Posts: 12792
    
    5
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

Joined: Jul 07, 2002
Posts: 96
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

Joined: Mar 22, 2000
Posts: 12792
    
    5
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

Joined: Jul 07, 2002
Posts: 96
I think i'll do that, thanks for all your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do I need to use ! for declarations?