Solved

Checking for validity of an include

Posted on 2003-12-01
7
344 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

856 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