• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interview Question

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Friends,
Recently I was asked the following question. How can we write a user defined method in a JSP page ???
Thanks,
-Navin.
[ January 17, 2005: Message edited by: Bear Bibeault ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you asking us for the answer or just sharing the experience with us?
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use jsp declaration in jsp code as follows

<%!

put you method code here

%>

Dan
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Any variable or method u can define inside the JSP declaration

for eg

<%!
int i=10;
public String str()
{
return "test";
}
%>
These variables and methods are instance variables and methods
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is a wonderful way to introduce thread safety problems into your JSPs. What good family fun!

Translation: don't do it!
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by satishkumar janakiraman:

These variables and methods are instance variables and methods



And that's the problem. There's only ever one (1) instance of a compiled JSP in memory so your method and variables are shared by all requests.

Better make darn sure you're not relying on the data to remain stable even during a request as it's bound to not happen.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was actually an excellent interview question. It's also one that shouldn't be answered, at least directly, in forum like this.
Doing so contributes to that class of people who know just enough to get themselves in trouble.

JSP, on the surface, looks very similar to ASP or PHP. Under the surface, there are some very important differences. Thinking that your knowledge of one of those scripting language is enough preperation for you to take a job as a JSP developer is an easy trap to fall into.

It was an excellent interview question for this reason: If an interviewee can't answer it off the top of her/his head then they don't understand the the most important difference -- the one that leads to the type of threading issues seen above. Asking "how would you create an instance variable" might have tipped the interviewee off.

If you plan on working with JSP, and don't fully understand what parts of your code will be translated into service code and which parts will be translated into instance code, then someone, handing the answer to you, is not doing you any favors.

Sit down and read a book, cover to cover, build a few apps that use the examples from the books and play with them. Better yet, learn servlets. The concepts that you'll need to understand to write proper JSP code are laid out in plain sight in a servlet. Some JSP/Servlet engines, by default, keep the code generated from your JSPs (Tomcat). If you've already learned servlets then looking at the generated code will show you exactly what becomes of your JSP scriptlets.

My first impluse when an easy question is posted is to show off that I know the answer. What's better, and harder to do, is to turn the question into a longer discussion in which the reader actually learns something.

In this case, Bear did it right by waiting until someone gave a "less than complete (although perfectly correct) answer" and then stepped in.
[ January 20, 2005: Message edited by: Ben Souther ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic