Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 749697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:27:51+00:00 2026-05-14T14:27:51+00:00

I have a jsp page which contains the code which prints all files in

  • 0

I have a jsp page which contains the code which prints all files in a given directory and their file paths. The code is

if (dir.isDirectory())
        {
            File[] dirs = dir.listFiles();
            for (File f : dirs)
            {
                if (f.isDirectory() && !f.isHidden())
                {
                    File files[] = f.listFiles();
                    for (File d : files)
                    {
                        if (d.isFile() && !d.isHidden())
                        {
                            System.out.println(d.getName()+ 
                            d.getParent() + (d.length()/1024));
                        }
                    }
                }
                if (f.isFile() && !f.isHidden())
                {
                    System.out.println(f.getName()+ 
                    f.getParent() + (f.length()/1024));
                }
            }
        }

The problem is that it prints the complete file path, which when accessed from tomcat is invalid. For example, the code spits out the following path:

/usr/local/tomcat/sites/web_tech/images/scores/blah.jpg

and I want it to only print the path up to /images ie

/images/scores/blah.jpg

I know I could just mess around with an actual string, ie splitting it or string matching, but is there an easier way to do it?

Thanks

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:27:52+00:00Added an answer on May 14, 2026 at 2:27 pm

    You’ll need to substring the root path away.

    File root = new File(getServletContext().getRealPath("/"));
    for (File file : root.listFiles()) {
        // ...
        String path = file.getAbsolutePath().substring(root.getAbsolutePath().length());
    

    By the way, those System.out.println() lines actually won’t print to the response. They prints to the stdout which may be the IDE console or the server’s logfile. Further, this kind of logic doesn’t belong in a JSP file. Do it in a real Java class and forward to JSP for display.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jsp page which needs a java code. This java code inside
I have a JSP page which includes an iframe , Iframe contains the image
I have a jsp page which contains ten variables which I have to forward
I have started a simple servlet 3.0 project which contains now a jsp page
I have a login.jsp page which contains a login form. Once logged in the
I have a problem. I have used a jsp page which contains multiple frames.In
I have a config.properties file which my JSP page uses to get configurations from.
I have a jsp page which simply prints {result : success} and there is
I have a jsp page which contains a form which calls a servlet, after
I have a simple Jsp page which contains HTML elements,as follows, <tr id=tr-username> <td>Username</td>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.