| Author |
Interview Question
|
Kumar Navin
Ranch Hand
Joined: May 27, 2004
Posts: 51
|
|
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
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 %> Dan
|
 |
satishkumar janakiraman
Ranch Hand
Joined: May 03, 2004
Posts: 334
|
|
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
Joined: Jan 10, 2002
Posts: 56227
|
|
Which is a wonderful way to introduce thread safety problems into your JSPs. What good family fun! Translation: don't do it!
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About 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.
|
42
|
 |
Ben Souther
Sheriff
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 ]
|
 |
 |
|
|
subject: Interview Question
|
|
|