0

I am beginning to learn servlet . I had issue try to get url from a folder(outside my project) which contains many images but i don't know how to display them in jsp (GrassFish Server). i try with some code but it don't working correctly .Simply outputting to HTML does not seem to work.

I've not performed any manual servlet mapping and I haven't changed any of Tomcat's configuration files.

this is my servlet


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>

} 

this is my 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> 

  • I think we might need a bit more information before we can help you. Did you receive any error messages or exceptions? What is the behavior that isn't working exactly? Are you not seeing the images? What is the html output after contacting the servlet? – hooknc Oct 30 '19 at 20:49
  • oh, in this old browser like internet explorer, i can see the image by getting url but in browser such as chrome , opera .. it's not work. it's don't show the image. i think the reason is security of browser but i don't know to fix . i used empty to show image in the local file – Dao Quoc Trung K13_HN Oct 31 '19 at 02:53
  • I think you are right that there is a security issue: https://stackoverflow.com/a/4908257/42962 – hooknc Oct 31 '19 at 16:54

0 Answers0