Проблемы с сервлетом Java и JSP - PullRequest
0 голосов
/ 30 октября 2019

Я начинаю изучать сервлет. У меня была проблема, попробуйте получить URL-адрес из папки (вне моего проекта), которая содержит много изображений, но я не знаю, как отобразить их в jsp (GrassFish Server). Я пытаюсь с некоторым кодом, но он не работает правильно. Просто вывод в HTML, кажется, не работает.

Я не выполнял никакого ручного отображения сервлета и не менял ни один из файлов конфигурации Tomcat.

это мой сервлет


import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author tamgi
 */
public class getServlets extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */



    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        File file = new File("C:\\Users\\tamgi\\Downloads\\Source\\Kyou No Cerberus\\Chap_56");
        File[] files = file.listFiles();


        request.setAttribute("files", files);
        request.getRequestDispatcher("Test.jsp").forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

} 

это мой jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Page</title>
        <% File[] files =(File[]) request.getAttribute("files"); %>
    </head>
    <body>
        <br>

        <% for(File f : files) {  %>

        <img src="<%= f.getCanonicalPath() %> " alt="empty">
        <br>
        <% } %>
    </body>
</html> 

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...