0

Есть класс Find.java для поиска всех файлов в папках и подпапках. Также имеется две jsp страницы, index и secondpage. На странице index.jsp есть поле, чтобы ввести путь папки. На странице secondpage.jsp должен выводиться список всех файлов. Не понимаю как передать параметр для класса? (И вообще запутался).

Java.class

import java.io.File;
import java.util.List;


public class Find {
  public  File folder = new File("");
  static String temp = "";

  private String text;

  public void setText(String text) {
         this.text = text;
  }

  public List<String> getList() {
         return null;
  }

  public  void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Reading files under the folder "+ folder.getAbsolutePath());
    listFilesForFolder(folder);
  }

  public static void listFilesForFolder(final File folder) {

    for (final File fileEntry : folder.listFiles()) {
      if (fileEntry.isDirectory()) {
        // System.out.println("Поиск в папках "+folder.getAbsolutePath());
        listFilesForFolder(fileEntry);
      } else {
        if (fileEntry.isFile()) {
          temp = fileEntry.getName();
         // if ((temp.substring(temp.lastIndexOf('.') + 1, temp.length()).toLowerCase()).equals("txt"))
            System.out.println("File= " + folder.getAbsolutePath()+ "\\" + fileEntry.getName());
        }

      }
    }
  }
}

index.jsp

!DOCTYPE HTML>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page language="java"
       contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>mainpage</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<center>FileFinder</center>
<br>
       <form action="secondpage.jsp" method="POST" >
       Поиск:  <input type="text" name="text" value="">
       </form>
</body>
</html>

secondpage.jsp

<!DOCTYPE HTML>
<%@page import="ru.getCardInfo.Find"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  <%
Find find = new Find();;
String name = request.getParameter("text");
System.out.print(name);
  %>
<html>
<head>
<title>secondpage</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<jsp:useBean id="Find" scope="session" class="ru.getCardInfo.Find" />
  <jsp:setProperty name="text" property="text" value='<%= name %>' />
       <input type="text" value='<jsp:getProperty name="Find" property="text" />' readonly />

</body>
</html>

1 ответ 1

0

Павел,

Действительно сумбурно у Вас все выглядит. На быструю руку перебил то, что Вы сделали.

index.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"/>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <title>mainpage</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<center>FileFinder</center>
<br/>
<form action="secondpage.jsp" method="POST">
    Поиск: <input type="text" name="text" value=""/>
</form>
</body>
</html>

secondpage.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"/>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="servlets.Find" %>
<%@ page import="java.util.List" %>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<html>
<head>
    <title>secondpage</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
    String path = request.getParameter("text");
    List<String> files = new Find().listFilesForFolder(path);
    request.setAttribute("listOfFiles", files);
%>

<table>
    <c:forEach items="${listOfFiles}" var="file">
        <tr>
            <td><c:out value="${file}"/></td>
        </tr>
    </c:forEach>
</table>
<form action="secondpage.jsp" method="POST">
    Поиск: <input type="text" name="text" value=""/>
</form>

</body>
</html>

Find.java

package servlets;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Find {
    List<String> result = new ArrayList<String>();

    public List<String> listFilesForFolder(String path) {
        File folder = new File(path);
        for (File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry.getAbsolutePath());
            } else {
                if (fileEntry.isFile()) {
                    result.add(folder.getAbsolutePath() + "\\" + fileEntry.getName());
                }
            }
        }

        return result;
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

И не забудьте добавить jstl-1.2.jar в WEB-INF\lib.

Результат будет выглядеть вот так: введите сюда описание изображения

Этого должно хватить, чтобы из этого Вы смогли сделать Ваше идеальное решение. Надеюсь поможет.

1
  • Спасибо, работает 31 авг 2017 в 10:49

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.