MJ
asked on
Checking for validity of an include
If ihave the below code and want to check if the include is valid and not null, can this be done?
for example, if I pass an invalid include name that is not the correct name, can I catch this and not throw an error?
////////////////////////// ////
Code
////////////////////////// ///
<% String incName = request.getParameter("incN ame"); %>
<%
if( incName != null )
{
String varincludefile ="includes/" + incName; %>
<jsp:include page="<%=varincludefile%>" flush="true" />
<%
}
%>
////////////////////////// ////////// ////////// /
I want incName to be a valid include name. How do I check this?
for example, if I pass an invalid include name that is not the correct name, can I catch this and not throw an error?
//////////////////////////
Code
//////////////////////////
<% String incName = request.getParameter("incN
<%
if( incName != null )
{
String varincludefile ="includes/" + incName; %>
<jsp:include page="<%=varincludefile%>"
<%
}
%>
//////////////////////////
I want incName to be a valid include name. How do I check this?
something like:
<%
if( incName != null )
{
String varincludefile ="includes/" + incName; %>
File inc = application.getRealPath(va rincludefi le);
if (inc.exists())
{
%>
<jsp:include page="<%=varincludefile%>" flush="true" />
<%
}
}
%>
<%
if( incName != null )
{
String varincludefile ="includes/" + incName; %>
File inc = application.getRealPath(va
if (inc.exists())
{
%>
<jsp:include page="<%=varincludefile%>"
<%
}
}
%>
>File inc = application.getRealPath(va rincludefi le);
more like
File inc = new File(application.getRealPa th(varincl udefile));
more like
File inc = new File(application.getRealPa
woops :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My point is there is no need to check anything.
result:
Tomcat 4, Weblogic 8, WebSphere 5 work as to spec.
Tomcat 3: exception (different exception in subversions)
WebSphere 4: 404 Not Found error message on page.
Tomcat 4, Weblogic 8, WebSphere 5 work as to spec.
Tomcat 3: exception (different exception in subversions)
WebSphere 4: 404 Not Found error message on page.
What you want to do is check for the existence of the file, perhaps using getRealPath() to get the file path of the include and check it exists.