This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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
Author

How do I call method of jsp from servlets?

michel dughlas
Greenhorn

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
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
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 ]

Ping & DNS - my free Android networking tools app
praveen sharma
Ranch Hand

Joined: May 27, 2006
Posts: 44
hi,

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
Greenhorn

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
Greenhorn

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.
 
Consider Paul's rocket mass heater.
 
subject: How do I call method of jsp from servlets?