0

I am very new to this field. I am sorry if this is stupid.

I have a HttpServlet code as follow:

@WebServlet("file")
public class FileResource extends HttpServlet {

    private static final long serialVersionUID = -7698183933607633414L;

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {

        resp.setContentType("application/pdf");
        resp.setHeader("Content-Disposition", "inline");

        File file = new File("/home/nabin/12403.pdf");
        long fileSize = file.length();
        resp.setContentLengthLong(fileSize);

        try {
            InputStream in = new BufferedInputStream(new FileInputStream("/home/nabin/12403.pdf"));
            OutputStream out = new BufferedOutputStream(resp.getOutputStream());

            for (;;) {
                byte[] buffer = new byte[4096];
                int n = in.read(buffer);
                if (n == -1)
                    break;
                out.write(buffer, 0, n);
            }

            out.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And I have a xhtml file where I want to display that pdf file. I want to display it as embedded.

<embed src="../questions/#{FileResource.get}.pdf" width="105%"
                            height="825px" />

Right now I can access the pdf file using localhost:8080/myapp/file How should I proceed?

Nabin
  • 11,216
  • 8
  • 63
  • 98

2 Answers2

1

pdf.js can be used to display pdf file https://github.com/mozilla/pdf.js

Nabin
  • 11,216
  • 8
  • 63
  • 98
Swanand Keskar
  • 1,023
  • 1
  • 13
  • 27
0

I am now being able to see the response inside iframe.

Also now I am sending request query as following:

<iframe src="/myapp/file?id=#{param.id}"/>

And in servlet class, I am retrieving the id using getParameter("id"); method

Nabin
  • 11,216
  • 8
  • 63
  • 98