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 Include dynamic HTML in a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Include dynamic HTML in a JSP" Watch "Include dynamic HTML in a JSP" New topic
Author

Include dynamic HTML in a JSP

russell peter
Greenhorn

Joined: Jul 03, 2011
Posts: 5
How do we include a HTML file dynamically - that is - a HTML file whose name gets resolved at run time.

I am trying to include a html page dynamically in a jsp page using the following dynamic jsp include syntax -

<jsp:include page="%=htmlfilename%" />

But, this causes a NullPointerException. I have verified that the path of the html is correct. If I rename the same html file to a jsp file the issue is resolved.

any suggestions are welcome.

Thanks,
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Russell,
Welcome to CodeRanch!

As you noted you can't include an html page in a jsp. Only jsp/jspf files may be included that way.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
russell peter
Greenhorn

Joined: Jul 03, 2011
Posts: 5
So, how do we include a html file in a jsp dynamically, that is include a html file whose file-name is not know till run time?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61220
    
  66

Have you tried <c:import>?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
You seem to be using the xml syntax for JSP.
I would recommend using the standard syntax for a runtime expression <%= htmlfilename %>
or an EL expression to populate this value ${htmlfilename}



It should work regardless of jsp or html, as long as that resource is available within the same container that is serving this JSP
ie it is retrievable via the RequestDispatcher
russell peter
Greenhorn

Joined: Jul 03, 2011
Posts: 5
Thanks for the replies. We have been trying what Stefan & Bear have been suggesting - but, we get a null pointer exception when we try to access the HTML file dynamically. If we provide a relative URL for a static html; for example - String htmlfilename = "/abc/xyz/html1.htm", then it works. When we change the filetype from html to jsp, the server picks the right file dynamically.

But, a dynamic HTML file fails - Tried:
<jsp:include page="<%=htmlfilename%>" />
<c:import url = "<%=htmlfilename%>" />
<%= htmlfilename%>

This is a very simple include. I am not sure, if the server configuration are causing the issue - Using WAS6.1
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61220
    
  66

You should be using the EL, not Java scriptlets and scriptlet expressions that have been discredited for almost 10 years now.
russell peter
Greenhorn

Joined: Jul 03, 2011
Posts: 5
That is correct Bear, we need to use EL.

But, I am not sure if that is the issue here.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
It works for me on Tomcat 7, both with runtime scriptlet expressions and EL expressions.



The only time I see a null pointer exception is when the variable "includePage" is null.

Are you sure that value is being set correctly?

Under the hood, the tag is obtaining a request dispatcher and calling forward on it so
the (almost) equivalent scriptlet code would be



The request dispatcher will return null if the requested resource does not exist under your JSP container.
So if your static html pages are being served by a web server (eg Apache + Tomcat combination) then <jsp:include> won't work, as it purely looks under Tomcat.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61220
    
  66

Stefan Evans wrote:Under the hood, the tag is obtaining a request dispatcher and calling forward on it so

Correction: include() rather than forward().
russell peter
Greenhorn

Joined: Jul 03, 2011
Posts: 5
Thanks for the replies. We are using WAS6 & hence this might be a IBM Websphere issue and not a JSP issue. On one of my machines, this works, but, not on a second machine with a similar configuration. The servlet container just does not recognize the html files. If I rename the same html files as a jsp file (same location, packaging & everything else remains the same), then the servlet container is able to pick up the files.

We actually tried using request.dispatcher object instead of scriptlets & EL, but, still get a Null pointer for html files.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Include dynamic HTML in a JSP