Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JSP function modifer's , static varibles etc...

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writting functions in a jsp page with the <%! %>.
But I have noticed in some sites they give examples where they have
access modifiers and specifiers.

How does these functions work in a jsp page?
I don't understand how modifier's and specifiers work in jsp pages and what their effect is?

Like what is the difference if you start to declare your functions like
below. Where does it come into play and what is vaild?
<%! int add(int num1 , int num2) %>

<%! private int add(int num1 , int num2) %>

<%! protected int add(int num1 , int num2) %>

<%! public int add(int num1 , int num2) %>

<%! public static int add(int num1 , int num2) %>

<%! public int add(int num1 , int num2) %>

Also can you have static varibles in a jsp page and what is it's effect over non-static.

I think anything you declare in a jsp has file scope.
Maybe it has to do with if you do include statment's?

Thanks for any help in resolving my confussion.
 
Marshal
Posts: 67426
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sam Doder:
I am writting functions in a jsp page with the <%! %>.
But I have noticed in some sites they give examples where they have
access modifiers and specifiers.

Anything you declare using the <%! ... %> syntax ends up at class level in the servlet generated for the JSP. So anything you put there has the same meaning as in any other Java class.

At this point, I'd usually strongly try to dissuade you from using this mechanism, but that's a subject that being adequately covered in your other topic.

think anything you declare in a jsp has file scope.

Class scope.

Also can you have static varibles in a jsp page and what is it's effect over non-static.

Non-static member variables are to be avoided in JSPs at all costs. These variables are shared by every JSP invocation and are the number one cause of threading issues in JSP applications. Read-only constants are the only member variables that should ever be declared in a JSP. (With the caveat that, in this day and age, JSPs shouldn't contain any Java code at all -- but again, that's a conversation for your other topic.)

Maybe it has to do with if you do include statment's [sic]?

It has absolutely nothing to do with include statements.
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I total agree with you. But I want to understand if these modifiers work the same way with jsp pages as they do in regular classes.

I just don't get what protected and private is going to do in a jsp page.
We don't have anyway of inheritance so does protected, package,...etc.

Even have meaning.

And if I have a static varible in a jsp (as stupied as it may sound)
Say it incremented every time the jsp page was acessed.
Then if I print the static varible.
Will it print 1 , 2 , 3, 4 ... for the 1,2,3,4 ... person to access the site.

So the jsp page get's compiled to a class but can you have some type of inhertance for a jsp page.
 
Bear Bibeault
Marshal
Posts: 67426
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sam Doder:
I just don't get what protected and private is going to do in a jsp page.

Not much.

And if I have a static varible in a jsp (as stupied as it may sound)
Say it incremented every time the jsp page was acessed.
Then if I print the static varible.
Will it print 1 , 2 , 3, 4 ... for the 1,2,3,4 ... person to access the site.

Pretty much. Remember that multiple simultaneous requests will be contending for the same instance of the variable.

So the jsp page get's compiled to a class but can you have some type of inhertance for a jsp page.

Not really.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the page inheritance issues - you actually can <shiver> have an inheritance structure for JSPs. The page directive allows you to extend a particular class using:
<%@page extends="package.classname" %>
Note that you would have to know the full package and class name of the class you are extending. So you could, for instance, find out the typical base class your server uses, extend it to provide some type of base functionality, then make your JSPs extend your custom JSP.

As I continue to shiver, let me provide more warnings: JSP servers will try to optimize the JSP it compiles by using different parent classes, but if you define the extends it can no longer do that. You would be tying your JSPs to one particular server (and probably server version). You would defeat the reason you wanted to move to an all JSP environment (if you made any changes to the class file you extend, you would have to re-start the application to see those changes in your JSPs). That extends won't work on extending another JSP that is compiled dynamically.
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic