Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

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("incName"); %>

<%
  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?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

jsp:include's are processed at request time so you shopuld be able to do that.
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.
something like:

<%
  if( incName != null )
  {
      String varincludefile ="includes/" + incName; %>
      File inc = application.getRealPath(varincludefile);
      if (inc.exists())
      {
%>
          <jsp:include page="<%=varincludefile%>" flush="true" />
<%
      }
   }
%>
>File inc = application.getRealPath(varincludefile);
more like  
File inc = new File(application.getRealPath(varincludefile));
woops :)
ASKER CERTIFIED SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kennethxu
kennethxu

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.