• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing servlet's method from JSP file

 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai dudes,
i have a diferent problem here. i am having a servlet with a method say public int methAdd(int x, int y){} which will add the two int's.
Now, how can i call this method from JSP page.
thanks & regrds,
Mahesh
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as we know, jsp pages are translated into servlets. it means you need to call a method of servlet2 from servlet1. why not going for include or forward??
 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai ansari,
i didn't get u. will u plz be clear. if possible give me an example.
thanks u
Mahesh
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mahesh Pinnamaneni:
i didn't get u. will u plz be clear. if possible give me an example.


Pardon, but where you are not clear.

- on jsp to servlet translation, or
- include or forward methods
??
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think it is better to avoid some business logic on servlet class.
And according to my knowledge it is not proper to call servlet's method from jsp. It is because all methods in servlet is supposed to be callback methods which are called by container.

Example that I can imagine is..
We have a jsp page (say a.jsp), a servlet class (it is controller) and a plain java class as service.
Our method doing calculate resides on a plain java class.
On our a.jsp, it send a HTTP request to container (it can be POST or GET).
Our request is received by container and container will redirect its request to servlet class (method doPost or method doGet).
Inside method doPost or method doGet, we put a code to call calculate method on that plain java class.

Hope this helps...
Correct me if I am wrong...
thanks
daniel
 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai daniel,
as u said
----------------------------------------------------------------------
Our request is received by container and container will redirect its request to servlet class (method doPost or method doGet).
----------------------------------------------------------------------

here the doPost or doGet methods are called accordingly. what my question is ...
in the same way can u call any other method (other than these doGet etc) in a servlet directly from JSP page.

hope u got me.
Bye
Mahesh
 
ernest fakudze
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahesh,

RE: can u call any other method (other than these doGet etc) in a servlet directly from JSP page.

Just like Daniel said, you should never place any business logic methods inside your servlet - instead move those away into a seperate Java class.

However, I find your question interesting so let me dwell on it a bit. Remember that you never call doPost(), doGet(), doPut() etc methods directly when accessing a servlet through a JSP or another resource. The entry point to a servlet that extends HttpServlet is the service() method and the service method calls the appropriate method for you based on whether the request is is POST (from a form whose mthod has been specified as POST) or if you have accessed the servlet using GET

Also, remember that you can call doGet from the body of the doPost method and vice versa. So to summarise, you can call a method in a servlet straight from a JSP but the way you do it is not that direct. Here is how you would do it:

If you were accessing the servlet using GET by typing its URI in your browser address bar, you can simply implement the doGet() method and call the calculate() method in its body. I hope that answers your question and can someone please correct me if I'm misleading in my facts.
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh,

If you are writing code for a 'real' application, you should not be calling ANY methods from your jsp, and usually wont have any public methods in servlet , other than the ones already implemented in HttpServlet.

Keep you business logic i.e. all public methods in an ordinary Java class, call those methods from the servlet to do any processing, and use JSP's to display the result.

If you are just playing around, remember that a serlvet is simply another java class, if you have a reference to a servlet, or create a new object of a servlet class, you should be able to call any public methods defined in it.

cheers.
Sonny
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!