Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running servlets from with jsp

 
Nicola Guy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic