File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes How do I call method of jsp from servlets? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do I call method of jsp from servlets?" Watch "How do I call method of jsp from servlets?" New topic

How do I call method of jsp from servlets?

michel dughlas

Joined: Oct 31, 2007
Posts: 6
i want to use a method of jsp in servlets. Please help how i can use the method of jsp.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
You can't, and you shouldn't. If you need the same code in servlets and JSPs move it to a Java class, maybe as a static method. Or even better, keep all code out of the JSP pages; they're not meant for doing computations. You can use backing beans, JSTL tags, or your own custom tags for that.
[ November 13, 2007: Message edited by: Ulf Dittmer ]
praveen sharma
Ranch Hand

Joined: May 27, 2006
Posts: 44

yes you are right you cannot access functions defined in jsp in servlet.if you want to then have to create separate class. since you cannot create object of servlet directly it is created by container and if you can't access function of 1 servlet into another servlet then how can you access from jsp page after all jsp pages are servlet.
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Your requirement clearly indicates that you need a helper class.
Create a helper class and let the servlet and jsp use this helper.

[Servlet tutorial] [Servlet 3.0 Cook Book]
Ryan Muster

Joined: Nov 11, 2007
Posts: 18
Hello michel dughlas,

The idea of combining JSP and Servlets is a good one, but its actually the exact reverse of what you thinking.
I suggest you google the web for as much info as you can find on Servlets and the MVC model... it will get you going.
Basically the servlet gets the request, it sticks stuff into a request or session bean, and then DISPATCHES the request off to the JSP page.

So servlets do the work, JSP's just diplay info... very powerful way of using a servlet container. Basicly you hava a worker bean or class that is moving between servlet and jsp page... so thats the connection between the two.

Have fun...
wang jiajian

Joined: Sep 20, 2007
Posts: 9
You should understand MVC .

please use jsp as the view only,why do you wan`t insert java code in you jsp?

why not use jstl?why not use tag?

you`d better don`t type method in your jsp.
I agree. Here's the link:
subject: How do I call method of jsp from servlets?
It's not a secret anymore!