File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Interview Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Interview Question" Watch "Interview Question" New topic

Interview Question

Kumar Navin
Ranch Hand

Joined: May 27, 2004
Posts: 52
Hi! Friends,
Recently I was asked the following question. How can we write a user defined method in a JSP page ???
[ January 17, 2005: Message edited by: Bear Bibeault ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Are you asking us for the answer or just sharing the experience with us?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
danny liu
Ranch Hand

Joined: Jan 22, 2004
Posts: 185
Use jsp declaration in jsp code as follows


put you method code here


satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334

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

Joined: Jan 10, 2002
Posts: 63866

Which is a wonderful way to introduce thread safety problems into your JSPs. What good family fun!

Translation: don't do it!

[Asking smart questions] [About Bear] [Books by Bear]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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

Joined: Dec 11, 2004
Posts: 13410

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 ]
I agree. Here's the link:
subject: Interview Question
It's not a secret anymore!