Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interview Question

 
Kumar Navin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking us for the answer or just sharing the experience with us?
 
danny liu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use jsp declaration in jsp code as follows

<%!

put you method code here

%>

Dan
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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!
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • 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 Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic