• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I call method of jsp from servlets?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to use a method of jsp in servlets. Please help how i can use the method of jsp.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your requirement clearly indicates that you need a helper class.
Create a helper class and let the servlet and jsp use this helper.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic