Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

could servlet include the jsp,html files???

 
Tony Ge
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like the jsp's include:
<%@ page include="somefile.jsp"%>
could servlet performs as above!!
if can, how to do??
thanx!!!
 
Meng Tan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
From my understanding, the answer is NO.
Because <%@ page include="someFile.jsp" %> is a JSP translation time operation.
It cannot work for a Servlet because the servlet would have already been compiled in order to run.
Thanks.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it can work.

Use
 
Tony Ge
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank meng tan,mike curwen
but i use:
request.getRequestDispatcher("/test.htm").include (request,response);

it doesn't occurs any error or exception,but it has not effect,why??
thanx!!
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh sheesh.. did I actually give the wrong code? Sigh... I must have been distracted, yes, that's it... I was VERY distracted when I posted that.

By "no effect", do you mean the page you see in your browser doesn't include the test.htm ? Did you view the source to see what it did include? Where is the included html residing?
My structure is like so:
included.html looks like:And the output is:
 
Christopher Dixon
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like the jsp's include:
<%@ page include="somefile.jsp"%>

Do you mean <%@ include file="somefile.jsp" %> ?
If so, that is indeed a translation time operation.
The short answer is no.
But...
I don't believe you could easily implement this in a servlet. I can think of a few hacks that could get you part of the functionality, but no full way to do it. The include directive will take the contents of whatever file is included and write to the JSP source before parsing the source into a .java file. To implement this in a servlet, I guess you could write a preparser that took the included page, ran it through the JSP translation engine, stripped away the surrounding servlet, and wrote a new .java source file for the servlet. Lemme know when you get it done
Chris
 
Tony Ge
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
followed Mike Curwen ,i do it successfully!!
many thanx!!
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should clarify that both Christopher and Meng are correct to point out that the RequestDispatcher.include(request, response) is a different operation than a page directive include ( <%@ include page="/somepage.jsp"> )

But they indeed have the same net effect.

If you were writing a servlet and you wanted to do this static kind of include, then you would simply cut and paste the contents of the jsp-as-compiled-to-servlet into your 'including' servlet.
 
Steven Kors
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this Method ...
private void gotoPage (String dispatchType,String address, HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
if (dispatchType.equalsIgnoreCase("include")) {
dispatcher.include(request, response);
} else if (dispatchType.equalsIgnoreCase("forward")) {
dispatcher.forward(request, response);
}
}

String dispatchType
dispatchType can be either "forward" or "include".
forward= When you forward, the forwarding servlet relinquishes control tothe page that its forwarding to.
include=When you include, you are only TEMPORARILY relinquishing control to the page being included. Typically this is used if you want to include the ouput of a page into the calling page.
String address
address, refers to the destination page.
IE of an include.
gotoPage("include","/footer.jsp",request,response);
IE of a Forward.
gotoPage("forward","/iDontWantToComeBack.jsp",request,response);
This will FORWARD to the target JSP page (or servlet as long as you use the correct context)
Oh, If you want to include a page at request time in JSP, use the following;
<jsp:include page="lookIamIncludedAtRequestTime.jsp" flush="true"/>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic