Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Include dynamic HTML in a JSP

 
russell peter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
russell peter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried <c:import>?
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using the EL, not Java scriptlets and scriptlet expressions that have been discredited for almost 10 years now.
 
russell peter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct Bear, we need to use EL.

But, I am not sure if that is the issue here.
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic