Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Checking for validity of an include

Posted on 2003-12-01
7
Medium Priority
?
348 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 28

Expert Comment

by:rrz
ID: 9854951
>File inc = application.getRealPath(varincludefile);
more like  
File inc = new File(application.getRealPath(varincludefile));
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

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

Accepted Solution

by:
kennethxu earned 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa‚Ķ
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.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

876 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