Dynamic include file?
In my application we are using an include. I want to choose which include file to use automatically. For example, I know a variable is pagelang = "english". So I want to display:
<%@ include file="includes/english.jsp"%>
Sometimes the variable is chinese, japanese, etc. I was wondering if I can do something like:
<%@ include file="includes/" & lang & ".jsp"%>
This dies every time, but is there a way to do this properly?
Thanks in advance.
<%
String includePage = new String();
//control flow statements go over here.
//assign appropriate values to the variable
RequestDispatcher dispatcher = request.getRequestDispatcher(inlcudePage) ;
dispatcher.include(request, response);
%>
Similar Messages
-
I need to use dynamically generated file names in my <%@ include file="" %> directive.
I am using objects (such as database connection) in the files included which I created in my base JSP file. For this reason, I cannot use the jsp:include tag (since these objects are not defined in these files, the servlet doesn't compile).
Are there any ways of using these dynamic names in this directive?If you are dymically generating the file, then store it in a string and then include it?
String dynamicallyGeneratedFileName = (however you are generating the file name);
<%@ include file="<%=dynamicallyGeneratedFileName%>" %> -
Dynamic include file in JSP page
<span class="value">Hi
i have index.jsp , this page can include jsp
pages dynamically by passing the name of the page to be included to the
index page with out .jsp , so if i want to include page "code.jsp" i
put: .../index.jsp?page=code , this have to include code.jsp page
i made the following code , but it did not find the file
so can any one help plz ?
String pg=request.getParameter("page");
if( pg!= null && ! "".equals(pg)){
File f=new File(request.getParameter("page")+".jsp");
if(f.exists()){
out.print("file exist");
%>
<jsp:include page="<%=request.getParameter("page")+".jsp" %>" />
<%
}else{
out.print("file not exist");
What does the file existing or not really have to do with it?
You can try it like this:
String pg=request.getParameter("page");
if( pg!= null && ! "".equals(pg)){
String webPath = pg + " .jsp";
String realPath = request.getRealPath(webPath);
File f=new File(realPath);
if(f.exists()){
out.print("file exist");
%>
<jsp:include page="<%= webPath %>" />
<%
}else{
out.print("file not exist");
}Alternatively you could try using a request dispatcher:
RequestDispatcher rd = request.getRequestDispatcher(webPath);
if (rd == null){
// doesn't exist
} -
Include the file dynamicly by file name
in my jsp,I want include another jsp dynamicly
<%@ include file="thefile.jsp" %>
thefile.jsp should be dynamic like:
<%=String filename=="thefile.jsp" %>
<%@ include file=filename%>
I know above is not working since file only take static name.
is there any way to include the file dynamicly by file name?String path = "..."; // to the ressource to include
javax.servlet.RequestDispatcher dispatcher
= getServletContext().getRequestDispatcher(path);
dispatcher.include(request,response); -
Include directive with dynamic attribute file
Hello, currently I'm wrestling with the following problem. I want to include a certain file in a JSP and I'm receiving the filename to be included from the request as a parameter.
This is my code so far.
<%-- Include dynamical path settings --%>
<% java.lang.String envJsp=request.getParameter("envJsp");%>
<%@ include file="<%=envJsp%>" %>
<%-- End Include dynamical path settings --%>
This results in the following error:
org.apache.jasper.JasperException: Bad file argument to include
java.lang.Throwable(java.lang.String)
java.lang.Exception(java.lang.String)
javax.servlet.ServletException(java.lang.String)
org.apache.jasper.JasperException(java.lang.String)
void org.apache.jasper.compiler.JspParseEventListener.handleDirective(java.lang.String, org.apache.jasper.compiler.Mark, org.apache.jasper.compiler.Mark, java.util.Hashtable)
void org.apache.jasper.compiler.DelegatingListener.handleDirective(java.lang.String, org.apache.jasper.compiler.Mark, org.apache.jasper.compiler.Mark, java.util.Hashtable)
boolean org.apache.jasper.compiler.Parser$Directive.accept(org.apache.jasper.compiler.ParseEventListener, org.apache.jasper.compiler.JspReader, org.apache.jasper.compiler.Parser)
void org.apache.jasper.compiler.Parser.parse(java.lang.String, java.lang.Class [])
void org.apache.jasper.compiler.Parser.parse(java.lang.String)
void org.apache.jasper.compiler.Parser.parse()
boolean org.apache.jasper.compiler.Compiler.compile()
Has anyone experience with this, or isn't it possible at all perhaps???
All suggestions are more than welcome.
Greetings, Johantry with requestDispatcher, e.g.
RequestDispatcher requestDispatcher = request().getRequestDispatcher( request.getParameter("envJsp" );
requestDispatcher.include(request,response); -
How to dynamically include jsp files using jsf el in include tag ?
Hi,
I'm trying to get the jsp file name from my backing bean to use it in an include tag but I can't make it work
When using this piece of code I can get the generated path
<h:outputText value="/_partial/#{myBean.jspName}.jsp"/>whereas in this code the jsf el is not replaced by this value.
<%@ include file="/_partial/#{myBean.jspName}.jsp" %> Have you any idea of how to solve this ? I'm currently developing for J2EE 1.4 platform.
Thanks for your help,
Regards.If you're using JSF 1.2 and JSTL 1.2 at a JSP 2.1 environment, then you can just use <jsp:include> with unified EL.
If you're using JSF 1.1, then you may find this article useful [http://balusc.blogspot.com/2007/01/dynamic-jsf-subviews.html] -
How do I find dynamically related files where Wordpress is not involved
There are lots of posts about "Dynamically-related files could not be resolved because the site definition is not correct for this server" but they all seem to relate to Wordpress or other blogging software.
I have recently had to upgrade from DW8 to DW CS5.5 running on Windows 7 64bit. and get this error on lots of pages but there is no help about fixing it. The local testing server is localhost set up to run PHP with iis7 on virtual folders.
What do I have to do to get the dynamically related files to be found?
ThanksI think I've cracked it - nothing to do with databases, that was a red
herring.
I had a hundred or so sites on an XP machine running Dreamweaver 8 and used
the export facility to save their settings when I moved to a Windows 7
system. Win7's directory structure is different but the testing server
location stored in the export file doesn't take account of this and part of
the path may now include directories that are viewed as shortcuts. Once the
locations are reset in Win7 terms, the errors go away.
Regards
Steve
Steve and Diana Kimpton
The Word Pool
http://www.wordpooldesign.co.uk/
http://www.wordpool.co.uk/
http://www.ukchildrensbooks.co.uk/
http://www.contactanauthor.co.uk <http://www.contactanauthor.co.uk/ -
How can I do a dynamic include of a page fragment?
I have a technical support website with a lot of simple html pages. What I want to do is hyperlink from the index page to another page, which would display these html pages as a page fragment, dynamically based on a session bean set by the hyperlink.
I basically want to do this, if it was possible:
<jsp:directive.include file="#{SessionBean1.pageToDisplay}"/>
Now the FAQ's has a topic "How can I do a dynamic include of a page fragment?", which would seem to answer my question.
But this is all it says, and it makes no sense to me. Could someone please translate? :)
"Using a page fragment file (but using instead of the usual Creator approach) will accomplish a dynamic include."Here is 1 solution:
First add this to the jsp:root tag:
xmlns:c="http://java.sun.com/jsp/jstl/core"
Then surround the page fragment directive with something like this:
<div style="position: absolute; left: 24px; top: 408px">
<c:if test="${SessionBean1.count > 0}">
<jsp:directive.include file="testPF.jspf"/>
</c:if>
</div> -
How to access a variable from a dynamically included JSP page
I have a jsp (say main.jsp) using the following in its code :
<jsp:include page="menu.jsp" flush="true" />
I have a variable in menu.jsp which I would like to use in my main.jsp.
How do I get the value of the variable defined in menu.jsp ?
I called this variable in menu.jsp, Public, but this didnot help me use the value of that variable in main.jsp
Any help is highly appreciated.. thnks a lotI do notice the variable var1 has the value populated - in menu.jsp - (from View -->
Source in the browser) but for some reason not able to use it in main.jspI'm not quite sure of the way your jsps are structured from your code snippet but I think there is something to be aware of here that may be part of your problem, namely the difference between the include directive and the include action.
You are currently using the include action, this treats the included resource as a dynamic object - i.e. a request is sent to that resource and the resulting response is included in your page. So when you think of what there is no java variable actually included - you'll just get the resulting HTML that is generated by menu.jsp
The include directive, as in <%@include file=�menu.jsp � %>, on the other hand treats the resource as a static object. This means the actual bytes in the included resource are inserted into the including jsp and then the result of that is compiled and processed, effectively you are cutting and pasting the menu.jsp into your calling jsp. In this case the variable would be available to the calling jsp.
However this approach does bring other difficulties that may break your current code, you'll just have to try it and see what happens.
Hope that's relevant to your problem,
Matt -
Failure in Precompilation of JSPs due to include file in WL6.1 SP2
Hi
We are getting the following error when we try to do precompilation of JSPs using
precompile param to true in weblogic.xml
We are deploying our application as war files.
eagerly waiting for your help
sincerely
Ramesh
Issue description and StackTrace:
Issue:
Precompilation of JSPs fail while using precompile param to true in weblogic.xml and deploy it as War file. It is througing "failure pre-compiling JSP's
weblogic.utils.ParsingException: nested TokenStreamException: antlr.TokenStreamE
xception: Could not include index.jsp" exception.
OS: Windows 2000
Jdk: Sun Jdk1.3.1
Weblogic: 6.1 SP2
***Stacktrace*************
<Dec 17, 2002 1:05:51 PM CST> <Error> <HTTP> <[WebAppServletContext(3961036,AdvW
eb,/AdvWeb)] failure pre-compiling JSP's
weblogic.utils.ParsingException: nested TokenStreamException: antlr.TokenStreamE
xception: Could not include index.jsp
at weblogic.servlet.jsp.JspLexer.parse(JspLexer.java:1025)
at weblogic.servlet.jsp.JspParser.doit(JspParser.java:80)
at weblogic.servlet.jsp.JspParser.parse(JspParser.java:183)
at weblogic.servlet.jsp.Jsp2Java.outputs(Jsp2Java.java:119)
at weblogic.utils.compiler.CodeGenerator.generate(CodeGenerator.java:255
Ramesh,
We experienced this sympton as well. In our JSPs, we converted
<%@ include file="xyz.jsp"%> (static include, source time) to
<jsp:include page="licensedQuote.jsp"/> (dynamic include, run time)
and the problem went away.
We still use static includes at the very start of the JSPs. It seems the problem only occurs when the static include is inside of a tag whose body does JSP evaluation.
-Charlie
"Ramesh Danala" <[email protected]> wrote in message news:[email protected]...
> Hi
> We are getting the following error when we try to do precompilation of JSPs using
> precompile param to true in weblogic.xml
> We are deploying our application as war files.
> eagerly waiting for your help
> sincerely
> Ramesh
>
> Issue description and StackTrace:
> Issue:
>
> Precompilation of JSPs fail while using precompile param to true in weblogic.xml and deploy it as War file. It is througing "failure pre-compiling JSP's
> weblogic.utils.ParsingException: nested TokenStreamException: antlr.TokenStreamE
> xception: Could not include index.jsp" exception.
>
> OS: Windows 2000
> Jdk: Sun Jdk1.3.1
> Weblogic: 6.1 SP2
>
> ***Stacktrace*************
>
> <Dec 17, 2002 1:05:51 PM CST> <Error> <HTTP> <[WebAppServletContext(3961036,AdvW
> eb,/AdvWeb)] failure pre-compiling JSP's
> weblogic.utils.ParsingException: nested TokenStreamException: antlr.TokenStreamE
> xception: Could not include index.jsp
> at weblogic.servlet.jsp.JspLexer.parse(JspLexer.java:1025)
> at weblogic.servlet.jsp.JspParser.doit(JspParser.java:80)
> at weblogic.servlet.jsp.JspParser.parse(JspParser.java:183)
> at weblogic.servlet.jsp.Jsp2Java.outputs(Jsp2Java.java:119)
> at weblogic.utils.compiler.CodeGenerator.generate(CodeGenerator.java:255
> )
-
Trying to dynamic include HTML into a JSP
All,
I am trying to include an HMTL file into a JSP file. I wish to do this dymanically. So, the following is not an option:
<%@ include file="relativeURL" %>
So, I am forced to resort to such measures as the following:
<jsp:include page="<%=myPath%>"/>
However, I get the following error:
java.lang.IllegalStateException: Response has already been committed
at java.lang.Throwable.fillInStackTrace(Native Method)
at java.lang.Throwable.fillInStackTrace(Compiled Code)....
I tried it like this:
<jsp:include page="<%=myPath%>" flush="true"/>
I encoutered the same result. I have JSP 1.2 running Tomcat 3.1 with Apache 1.3.14, all on SunOS. Thanks in advance for any help.
NicholasOne of the worst error messages you can get is the IllegalStateException. It simply means that an attempt has been made to forward or include but only a portion of the include was sent. In a situation like this the best way to handle the dynamic page content is to include it from a Servlet by sending the information you need through beans. Including the Servlet rather than the dynamic path would clear those errors right up. One thing to keep in consideration is that you can't just get the page and display it, you have to buffer it in so the state can be kept alive without timing out like it does with the dynamic include.
Hope that helps. Been there, done that, I know how frustrating the error was. -
Dynamic include in a custom tag, called from a jsp
Hello, I would like to be able to dynamically include other jsp's from within a custom tag that I create. is this possible?
in a jsp page i would just write <%@ include file="file.jsp" %> and all would be ok... but if i do an out.println( "<%@ include file=\"file.jsp\" %>" ); inside a custom tag bean then it doesn't work. it just prints out the command to the page as plain text.
I do understand why this is happening, but can anyone offer me a way around this problem?
Thanks in advance,
RandyThat's actually the same result as you get with a normal include - The method I suggested is a dynamic include similar to the <jsp:include> tag, not the <%@ include %> tag.
When you use a dynamic include, the included page is compiled and 'invoked' as serarately, and the result is what gets included, not the actual source. To get the behaviour you want would require an include directive (the <%@ include %> tag) which I don't think has an equivalence you can use inside a custom tag.
What you can do is pass attributes from the tag class like this:
pageContext.setAttribute(<name>, <object>, pageContext.REQUEST_SCOPE);
and then remove them after the include using pageContext.removeAttribute.
I'm not certain, but this implies that if you declare your variables with a <jsp:usebean> tag with request scope, instead of normal Java variable declarations, you should be able to use them in the page included by the custom tag.
*** in the jsp ***
<jsp:useBean id="myVar" scope="request" class="java.lang.String" />
<% myVar = "Something"; %>
// Now call the tag which uses the pageContext.include() mehtod.
*** end ***
*** in the include ***
<jsp:useBean id="myVar" scope="request" class="java.lang.String" />
<%= myVar + " something else" %>
*** end ***
Let me know if it works. -
Hi,
I have say 4 jsp pages. One page (A.jsp) is sort of a controller. It defines different kinds of variables. The other 3 jsp pages (say B, C, D) are included in between the code in A.jsp (using @include file..) depending upon some kind of condition. Not all the 3 files are included - they are in a if..then condition. All the 3 files make use of some variables defined in A.jsp.
The problem is:
In JDev9i, When I try to run the application, I get an error in the compilation stage saying that variable x etc are not found in file B, C or D - wherever its used (The variable was defined in A.jsp). and the application fails to execute. What do I have to o in order to get around this problem 'cause there are many such files in my project.Hi,
Instead of using static include using @include, depending on the condition, use dynamic jsp include <jsp:include> and pass the variables used in A.jsp using <jsp:param> to B,C,D jsps. Access the value of the variable from the request.parameter object.
Hope that helps.
OTN Group@IDC -
How to create a dynamic include
I would like to construct a dynamic include depending on URL searchstring data. In my mind it looks like this:
<%@ include file = request.getParameter("subnav")+".jsp" %>
but for my Server it�s more like this :-))
index.jsp(110,25) Attribute value should be quoted
It might be a problem on the execution time of the include command but I don�t know how to get around it.
ElmarThe JSP's include directive (<%@ include) can be used to include files at the translate stage. So, you can't parameterize the file name. But the JSP's include tag (<jsp:include...) includes the file at runtime and hence you can parameterize it as I have shown in my earlier reply.
Hope this helps. And this is the solution I believe.
Sudha -
Generating a random include file
I'm trying to generate a random "include" file in an HTML web page. I have a group of 20 files, each with one sentence, and I'd like to randomly call one of those files into my web page, using a statement like
<!--include virtual = "includes/file1.inc" -->
Is there a way to use Javascript to generate that line of code, but make the number just before ".inc" be randomly entered?
Thanks in advance.
BobWell, dynamically formed file name for include is not possible in javasript. Includes are processed before any javascript runs on the page.
But
You can do this if you keep the lines in the HTML file:
<SCRIPT LANGUAGE="JavaScript">
lines = new Array;
lines[1] = "Well, some things in Javascript are not cool";
lines[2] = "Sometimes Javascript is cool";
lines[3] = "Ok, I am getting tired of typing now";
lines[4] = "But then again, who is worried";
lines[5] = "A random line can be shown";
var total = 5;
var randomNumber = Math.random();
var idx = Math.round( (total-1) * randomNumber) + 1;
document.write(lines[idx]);
</SCRIPT>
Maybe you are looking for
-
I use google as my homepage. Very often( every day at some point) when I search a subject and click on the address, a google search page come up with list of choices. When I click on address it simply returns to this page and will not let me access a
-
Adapter Engine Error Message on RWB
Hello We are getting an Red color error message on Adapter engine on RWB monitoring ---> Component Monitoring. Here is the error message: SLD access SLD host:port = sap03:50000 Error getting JPR configuration from SLD. Exception: No entity of class S
-
Adding music to slide shows??
does anyone know if there is a limit tot he number of songs you can add to a slide show? i'm not having any luck getting all the music i want into a lengthy slide show.... it keeps looping back to the 1st song and starting over after about 15 songs..
-
Hi All,problem in synchronizing toughbook
Hi All, Pls provide the solution for the problem when synchronizing toughbook .Its Urgent .
-
HT204249 Xsan 4: Confirm network being used for metadata
Regarding this article: How to select an Xsan metadata network in OS X Server (Yosemite) What is the best way to confirm the IP/network that is currently in use for metadata OS X 10.10?