Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Ques:- How to 'include' other files into them.(ie .html .jsp files)  RSS feed

 
Ryan Yeap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have came across a problem .
My basic page layout (.jsp) has lots of <%@ include file="" %> Some being .jsp files , some .html files.
Now when I am creating a servlet , I want to show the similar layout, but i cant(or so it seems) simply do:
out.println("<%@ include file='some.jsp' %> ");
as the servlet will process it as html and not .jsp .
My ultimate question being ,
- Is there a method that i could use to 'include' these files in a servlet and still process it as .jsp or do i have to manually include all the code that is in the include files.
- I have also thought about another method maybe, I use the servlet as only a processor where the form.jsp passes to servlet , servlet processes request then passes to show.jsp. Is that possible? If so, can anyone show me a quick example?
Thanks. thats about all . Thank you for your time
Ryan

[This message has been edited by Ryan Yeap (edited August 30, 2001).]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't know of any such method you could use directly.
IMO, you should write a (Bean) CustomClass with a method
which does this for you and returns a String maybe.
You could then add this to the out stream.
Just a thought....
- satya
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have to say that (personally) I disagree with the "code outputs html" solution..
It should be possble to achieve what you want (if I understand correctly) by including several pages together. Not something I've ever tried since I only use Sevlets and JSPs as the Model-View-Controller Pattern (no presentation in the Servlet)
So, something like this [i]should[/] work...

I still have to try it myself though...
If you want to do the servlet -> jsp -> servlet thing, it sounds like MVC to me.
Dave.
 
Ryan Yeap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
Have to say that (personally) I disagree with the "code outputs html" solution..

Would jsp(form) -> servlet -> jsp(output) be better?
I did try using
getServletContext().getRequestDispatcher("include/header.html").include(req,res);
but got an Error 500 which points to that line.
I must be doing something wrong
I'm looking into the idea of just using the servlet to process and pass to the jsp for output though... any comment or help would be appreciated. Thanks!
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Would jsp(form) -> servlet -> jsp(output) be better?

If you could do it like this, then you should think of
jsp(form) -> jsp(output) and eliminate the servlet mess
altogether.
the jsp(form) should be made to handle the data by itself
easily. Then inside if this jsp(form) use the standard
includes.
Atleast thats what I would attempt to do instead of bringing
in a servlet. Sure this seems like we are eliminating the
controller part of an MVC model, but if this works, then
I would slowly move into the next step of introducing a
controller.
My two cents worth....
- satya
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounding more and more like the MVC
Controller - the servlet decides on the page that gets shown but plays no part in generating the page.
Model - the business logic or data to be presented. Performs the business rules but does not have any presentation specific data in it. I've seen a bunch of ways of doing this but the way I'm most familiar is having the sevlet build a bunch of business objects then passing them to the jsp
View - the presentation layer. Gets passed a bunch of data and decides how to draw them.
I wasn't sure the original code I gave (chaining using include) would work, as I said, I haven't done it that way and probably wouldn't.
To give a real basic MVC example, heres a user login with most of the complexity removed:

Note that it uses forward rather than include so that it ensures that the srevlet has not set any data that will interfere with the presentation displayed by the servlet.
The reason I called it servlet -> jsp -> servlet was that you call the servlet, it decides to show the login page, then the login page calls a servlet again.
Hope this helps
Dave.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!