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
posted
0
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.
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
posted
0
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
posted
0
followed Mike Curwen ,i do it successfully!! many thanx!!
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
posted
0
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"/>