This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I call method of jsp from servlets?

 
michel dughlas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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 ]
 
praveen sharma
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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.
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Ryan Muster
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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...
 
wang jiajian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic