Solved

Checking for validity of an include

Posted on 2003-12-01
7
342 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

914 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

21 Experts available now in Live!

Get 1:1 Help Now