File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Can't include other page content with RequestDispatcher.include() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Can Watch "Can New topic
Author

Can't include other page content with RequestDispatcher.include()

Mark McKay
Greenhorn

Joined: Mar 20, 2011
Posts: 24
I'm having a heck of a time trying to get a .jsp page in one context to display a fragment of a .jspf that's stored in a different context. After looking at a lot of help forums, it seems the best way to do this is to get the context's application object, create a RequestDispatcher object from it and then call include(). However, this is failing for me even if I stay in my original context:


This results in the error "getOutputStream() has already been called for this response".

I can't use a <jsp:include> tag because I ultimately want this to be a cross context call. I tried using <c:import>, but that won't let you submit expressions to the url parameter and doesn't appear to evaluate the .jspf dynamically.

I'm using Tomcat 6. I've already got the crossContext="true" thing configured. Any guidance would be much appreciated.





Games, art, code:
http://www.kitfox.com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Mark McKay wrote:I tried using <c:import>, but that won't let you submit expressions to the url parameter ...

The documentation seems to say otherwise.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Goutam Chowdhury
Ranch Hand

Joined: Jan 20, 2009
Posts: 44

At the end of your JSP page Add these two lines and try

out.clear();
out = pageContext.pushBody();

For Information
http://www.jguru.com/faq/view.jsp?EID=501393


Thanks And Regards
Goutam Chowdhury (Scjp1.4 86%,SCWCD5 94%)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

There is no need for Java code on the JSP.

<c:import> should work just fine.
Mark McKay
Greenhorn

Joined: Mar 20, 2011
Posts: 24
Goutam Chowdhury wrote:
At the end of your JSP page Add these two lines and try

out.clear();
out = pageContext.pushBody();

For Information
http://www.jguru.com/faq/view.jsp?EID=501393


I updated my web page to the below. Unfortunately, it's not including the .jspf withing the body of the page - it's replacing the entire page with the .jspf. It also does not seem to evaluate the .jspf but just slurp it in as raw text. How do I make this work like and include and not a forward to a text file?

Mark McKay
Greenhorn

Joined: Mar 20, 2011
Posts: 24
Bear Bibeault wrote:There is no need for Java code on the JSP.

<c:import> should work just fine.


I created a separate test case for <c:import>. I'm having the same problem I'm having with RequestDispatcher.include() - the test.jspf file is being treated as static text. Below is the jsp page and response I get:

Source:




Reply:

Mark McKay
Greenhorn

Joined: Mar 20, 2011
Posts: 24
Bear Bibeault wrote:
Mark McKay wrote:I tried using <c:import>, but that won't let you submit expressions to the url parameter ...

The documentation seems to say otherwise.


The docs may say otherwise but Netbeans/Tomcat refuses to let me put an expression there. Below are my test case and error:



Error:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Mark McKay wrote:The docs may say otherwise but Netbeans/Tomcat refuses to let me put an expression there. Below are my test case and error:

That's because you are mixing scriptlets expressions with the JSTL.

Drop the scriptlets. All of them. Now. Scriptlets have been discredited for almost 10 years now and their use in JSP pages is irresponsible at this point.

Dynamic expressions should be expressed with the EL.

Aside from mixing technologies that won;t work well together, the file fragment should be named .jsp, not .jspf. The latter are not processed as JSP pages.
Mark McKay
Greenhorn

Joined: Mar 20, 2011
Posts: 24
Bear Bibeault wrote:
Mark McKay wrote:The docs may say otherwise but Netbeans/Tomcat refuses to let me put an expression there. Below are my test case and error:

That's because you are mixing scriptlets expressions with the JSTL.

Drop the scriptlets. All of them. Now. Scriptlets have been discredited for almost 10 years now and their use in JSP pages is irresponsible at this point.

Dynamic expressions should be expressed with the EL.

Aside from mixing technologies that won;t work well together, the file fragment should be named .jsp, not .jspf. The latter are not processed as JSP pages.


What's an EL? (I have a lot of experience with J2SE, but am very new to JSP. I'm learning mostly by reading web tutorials).

If I don't use scriptlets, what do I do? Write my own tag library that does the equivalent?

Why are scriplets no longer considered good practice?
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Anything you want to do with scriptlets on a JSP page, you can do the equivalent using JSTL and EL.
If you can't do it with JSTL and EL, then it probably shouldn't be on your JSP page, but rather in a servlet/bean.

It makes the page easier to follow if you don't have two types of code in a page, and an html/xml like syntax is neater.



.jspf files are intended to be included with <%@ include %> directive.
If you are using the request dispatcher, you should point at a complete jsp page.

Note the distinction between the include directive and request dispatcher include. The directive effectively pastes the text into the JSP, and then evaluates it. The Request dispatcher includes the result of executing the JSP referred to.

Doing a cross context reference is normally undesirable. Is there any particular reason you need that?
Mark McKay
Greenhorn

Joined: Mar 20, 2011
Posts: 24
I'm thinking of creating a set of choose-your-own-adventure type games and using JSP to link it all together. Since I'm pretty good with Java, I'd like to include some extra features beyond what just HTML can provide.

Part of this is creating a login/registration system. At the moment, I can create a user account and have them log in. All of the business logic is handled by regular Java beans. I'm just using scriptlets to format the HTML and read the request data.

Anyhow, since each game will independent, I'd like to give each one its own context. This way I don't have to upload the entire website each time I modify one game. But since they all need to use the same login system, I need to have the login be able to pass data to the different game contexts. Part of the login system will be a listing of all the games available - for this I want my directory page to be able to call a .jsp in each of the game contexts to get HTML containing a banner and some extra information that is user specific. Right now I'm stuck at being able to create the listing. (And I suppose the next challenge would be seeing if the other contexts will accept the UserIdentity bean I've put in the session).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

As Stefan pointed out as well, the JSTL and EL are the modern equivalents of scriptlets.

If you are just learning JSP, then do yourself a favor and forget scriptlets ever existed and learn the correct way to code JSPs.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Interesting issue.

First thing you should know is that your session is NOT shared between all your web applications.
So putting the UserIdentityBean into your login application's session won't make it available for other applications directly.
It makes sense if you think about it. You don't want some random web application in another context futzing with your session data.

However your architecture doesn't seem too horrible . I can see your reasoning behind wanting one web app context for each separate "adventure" and have them all talking to a central controller of sorts.

You may want to take a look at Tomcat's SSO facility to see if it will help you out.

Some suggestions:
Have a "central controller" for the server. Not necessarily a web app. Probably a singleton pattern would be appropriate? Or store it in JNDI environment.
Have each "adventure" register with the central controller when it starts up. Probably pass in its context object, and some extra parameters.
Your "index" page can then get a canonical list of all the registered adventures from the controller (and their contexts) and can either render the information itself, or invoke a "well known url" on each web context to display the details of each adventure.

I guess the important bit here is the concept of a controller independant of running in a servlet container.
Or I might just be spouting a whole lot of horse manure.


Mark McKay
Greenhorn

Joined: Mar 20, 2011
Posts: 24
Interesting idea. A singleton would make it easy for the apps to let each other know they exist. I suppose they're running out of the same VM, so that should work.

My directory app would also provide useful account features to the user, such as changing their email address or resetting their password. Since this is generic stuff that all the games would have in common, it makes sense to have it in a single separate web app. If I can't pass a UserIdentityBean in the session, one problem I see is that the user will have to login anew if they ever change games or go back to the directory app. A user might find this inconvenient.

Another thing - I thought Tomcat didn't compile/run any context code until a user requested a page from it. If I redeploy or restart my server, is there a way to have my web app immediately register with that singleton?
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
If I redeploy or restart my server, is there a way to have my web app immediately register with that singleton?


Certainly there is.
In your web.xml file you can configure a servlet to be loaded at at startup.



You can put the relevant code in the init() method of the servlet.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Stefan Evans wrote:Certainly there is.
In your web.xml file you can configure a servlet to be loaded at at startup.

Sigh. 2003 called an it wants its code back.

A context listener is the correct way to perform startup activity. Using a servlet's init() for startup activity is an old-fashioned hack that is no longer needed.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
2003 called?

I guess a context listener would be a better approach now that I consider it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Hope the humor came across...

Yes, a context listener is one of the first things that I always set up when starting a new web app.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't include other page content with RequestDispatcher.include()