• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP

 
Ajnam Bujanany
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a method written in JSP be called from some other class?
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
y limit the qn to jsp ? how about servlets ? that's what jsps finally get converted into, right ?
And in both cases the answer is no. To understand this, think about the lifecycle of Servlets, do we
initialize them explicitly - no. After all servlets are intsantiated and maintained by the container.
Is it possible to get a handle to an existing Servlet object - no. There used to be a getServlets() method
in the context object which is now deprecated and would return null (according to Sun).

Theorotically it may be possible to say new Servlet() (I have never tried it out, so Iam not sure), however you couldnt call the Servlets doGet() or doPost() method, coz you cannot prepare the request and response objects,
for the same reason you cannot call init(). It may be possible, if for some bizzare reason(I cant think of any), you have your own public method inside the Servlet (& then u do a new Servlet() and call this method), but why would anybody do such a thing is quite beyond me. The same goes for static methods :-)

Tx,
Ram.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what javabeans are for.

Move your method to a bean and make it accessible to whatever JSPs need it.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
This is what javabeans are for.

Move your method to a bean and make it accessible to whatever JSPs need it.


cent percent AGREEMENT.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!