wood burning stoves 2.0*
The moose likes Servlets and the fly likes Running servlets from with jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Running servlets from with jsp" Watch "Running servlets from with jsp" New topic
Author

Running servlets from with jsp

Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
Hi

I have a query which I am unable to find the answer too. I have a small web app set up using the struts framework. Within this I have a number of jsp files for content.

I also have a number of servlets used to access the database.

My question is this:
Is there a way to include a servlet in a jsp page i.e. have a jsp with all the html tags and stuff and make a call to a servlet which will then go access the database, retrieve it and then display it within the page

Or using struts and the tiles capability, can the servlet be embedded.

Hope someone can help as I've only been looking at struts and jsp/servlets for a week now so I still have a lot to learn
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Sure, you could include the servlet with a JSP include action.

But... it's not a very standard or accepted thing to do.

If you're already working in Struts, then this is the type of thing that would normally take place in a Struts action prior to reaching the JSP page in the first place. Why are you waiting until you have forwarded to a presentation resource (the JSP) before trying to access the database?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This is generally done the other way around.
The call is made to the servlet (controller).
The Servlet instanciates a bean or some beans and passes the request parameters to them.
The beans do whatever work in needed (talk to database, etc...).
The servlet then bind the beans (or results from the beans) to a particular scope (request, session, context) and forwards to the JSP (view) for handling.

What you've described can be accomplished with the <jsp:include ... /> action.


[KLUNK]
[ July 28, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
jQuery in Action, 2nd edition
 
subject: Running servlets from with jsp
 
Similar Threads
JSP/Servlets!
designing a serch engine
Some Brainbench JSP Q's.....Opinions required
Connecting JSP to MySQL
Struts book for a newbie