File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes could servlet include the jsp,html files??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "could servlet include the jsp,html files???" Watch "could servlet include the jsp,html files???" New topic
Author

could servlet include the jsp,html files???

Tony Ge
Greenhorn

Joined: Sep 05, 2001
Posts: 20
like the jsp's include:
<%@ page include="somefile.jsp"%>
could servlet performs as above!!
if can, how to do??
thanx!!!
Meng Tan
Ranch Hand

Joined: Jan 20, 2001
Posts: 115
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

Joined: Feb 20, 2001
Posts: 3695

Yes, it can work.

Use
Tony Ge
Greenhorn

Joined: Sep 05, 2001
Posts: 20
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

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Jan 30, 2002
Posts: 22
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

Joined: Sep 05, 2001
Posts: 20
followed Mike Curwen ,i do it successfully!!
many thanx!!
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Jan 30, 2002
Posts: 33
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"/>


Thanks,
Steven
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: could servlet include the jsp,html files???
 
Similar Threads
Handling Error in JSP
jsp include
JSP to HTML on serverside
Difference between jsp:include and include directive.
read permission to a file created by weblogic