Solved

Checking for validity of an include

Posted on 2003-12-01
7
341 Views
Last Modified: 2013-11-18
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?
0
Comment
Question by:894359
  • 3
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9854342
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 9854352
something like:

<%
  if( incName != null )
  {
      String varincludefile ="includes/" + incName; %>
      File inc = application.getRealPath(varincludefile);
      if (inc.exists())
      {
%>
          <jsp:include page="<%=varincludefile%>" flush="true" />
<%
      }
   }
%>
0
 
LVL 27

Expert Comment

by:rrz
ID: 9854951
>File inc = application.getRealPath(varincludefile);
more like  
File inc = new File(application.getRealPath(varincludefile));
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Expert Comment

by:objects
ID: 9854969
woops :)
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 500 total points
ID: 9855200
According to servlet/jsp specification, there is nothing going to happen if the including resource not exists.
I had verified this in tomcat. will check against weblogic and websphere and get back.

application.getRealPath will always return null when the webapp is deployed as web application archive file (war), so use with caution.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9855222
My point is there is no need to check anything.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9860443
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.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now